aoc-all

My solutions to all Advent of Code
git clone https://git.instinctive.eu/aoc-all.git
Log | Files | Refs | README | LICENSE

commit 935d2792211c738469ae7995b1406fc689dd736a
parent 6f1ec7fbf642aa9bdeaf1f9640a15ba34dd8053f
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Thu,  1 Dec 2022 13:52:02 +0000

day 1 solutions
Diffstat:
A2022/day01.ps | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 98 insertions(+), 0 deletions(-)

diff --git a/2022/day01.ps b/2022/day01.ps @@ -0,0 +1,98 @@ +%!PS +% Usage: +% gs -q- -sDEVICE=png16m -o- -r300 -dDownScaleFactor=3 day01.ps <day01.txt | display + +/str_to_int { + 0 exch { exch 10 mul add 48 sub } forall +} bind def + +/int_to_str { + % number + 1 10 + { % number length upper-bound + 2 index 1 index lt { exit } if + exch 1 add exch 10 mul } loop + pop dup string exch 1 sub + { % number string index + 2 copy 1 sub 5 1 roll exch + % next_index number string string index + 3 index 10 mod 48 add + % next_index number string string index digit + put + % next_index number next_string + 3 1 roll 10 idiv + % next_string next_index next_number + dup 0 eq { pop pop exit } if + % next_string next_index next_number + 3 1 roll + % next_nubmer next_string next_index + } loop +} bind def + +/apush { + % array new_item + exch aload length 1 add array astore +} bind def + +/apop { + aload length 1 sub array astore exch +} bind def + +/datafile (%stdin) (r) file def + +/data [ + [] + { + datafile 100 string readline + not {pop exit} if + dup length 0 eq + { pop [] } + { str_to_int apush } + ifelse + } loop +] def + +/sums [ + data + { 0 exch + { add } forall + } forall +] def + +/Helvetica 20 selectfont + +(First Puzzle: ) +72 700 moveto show +% compute max (sums) +0 sums { + % max-so-far new-value + 2 copy lt { exch } if + % max(max-so-far, new-value) min(max-so-far, new-value) + pop +} forall +int_to_str show + +(Second Puzzle: ) +72 664 moveto show +% compute three largest +0 0 0 sums { + % save3 save2 save1 new + 2 copy gt { exch } if + % save3 save2 rank2(save1,new) rank1(save1,new) + 4 1 roll + % rank1(save1,new) save3 save2 rank2(save1,new) + 2 copy gt { exch } if + % rank1(save1,new) save3 rank3(save2,save1,new) rank2(save2,save1,new) + 4 1 roll + % rank2(save2,save1,new) rank1(save1,new) save3 rank3(save2,save1,new) + 2 copy gt { exch } if + % rank2(save2,save1,new) rank1(save1,new) rank4(all) rank3(all) + 4 1 roll + % rank3(all) rank2(save2,save1,new) rank1(save1,new) rank4(all) + pop +} forall +add add +int_to_str show + +showpage +quit