aoc-2022

My solutions in postscript to Advent of Code 2022
Log | Files | Refs | README | LICENSE

commit 22bd05fb048a8ef17acfc09318682d9cbae583d5
parent e1622012cf71fa04f79b31a0c09647cf8d833918
Author: Natasha Kerensikova <natacha@instinctive.eu>
Date:   Thu,  1 Dec 2022 14:52:02 +0100

day 1 solutions

Diffstat:
Aday01.ps | 98+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 98 insertions(+), 0 deletions(-)

diff --git a/day01.ps b/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