aoc-all

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

commit 05bebfbe6bc4d0515ac6be4330908cd960f43772
parent f6066fd700569b70d3f75ae1270b45ec28d05b85
Author: Natasha Kerensikova <natgh@instinctive.eu>
Date:   Sun,  4 Dec 2022 10:50:11 +0000

Add day 4 reference input and solutions
Diffstat:
A2022/day04.ps | 136+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A2022/day04.txt | 6++++++
2 files changed, 142 insertions(+), 0 deletions(-)

diff --git a/2022/day04.ps b/2022/day04.ps @@ -0,0 +1,136 @@ +%!PS +% Usage: +% gs -q- -sDEVICE=png16m -o- day04.ps <day04.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 + +% (X-Y) -> X Y +/parse-pair { + (-) search + not { stop } if + % (Y) (-) (X) + exch pop + % (Y) (X) + str_to_int + exch + str_to_int + % X Y +} bind def + +% (I-J,K-L) -> I J K L +/parse-line { + (,) search + not { stop } if + % (K-L) (,) (I-J) + exch pop + % (K-L) (I-J) + parse-pair + 3 2 roll + % I J (K-L) + parse-pair + % I J K L +} bind def + +/data [ + { + datafile 100 string readline + not {pop exit} if + % line + parse-line + % I J K L + 4 array astore + % [I J K L] + } loop +] def + +/stderr (%stderr) (w) file def + +/Helvetica 20 selectfont + +(First Puzzle: ) +72 700 moveto show +0 data { + % accumulator cur-array + aload pop + % accumulator first1 last1 first2 last2 +% 3 index int_to_str stderr exch writestring stderr 32 write +% 2 index int_to_str stderr exch writestring stderr 32 write +% 1 index int_to_str stderr exch writestring stderr 32 write +% dup int_to_str stderr exch writestring stderr 32 write + % force first1 <= first2 + 3 index 2 index gt { + 4 2 roll + } if + % accumulator first1 last1 first2 last2 + 2 index 1 index ge + % accumulator first1 last1 first2 last2 last1>=last2? + 4 index 3 index eq or + % accumulator first1 last1 first2 last2 last1>=last2||first1==first2? + 5 1 roll pop pop pop pop + % accumulator last1>=last2||first1==first2? + { 1 add } if + % updated-accumulator +% dup int_to_str stderr exch writestring stderr 10 write +} forall +int_to_str show + + +(Second Puzzle: ) +72 664 moveto show +0 data { + % accumulator cur-array + aload pop + % accumulator first1 last1 first2 last2 + % force first1 <= first2 + 3 index 2 index gt { + 4 2 roll + } if + % accumulator first1 last1 first2 last2 + 3 copy pop ge + % accumulator first1 last1 first2 last2 last1>=first2? + 5 1 roll pop pop pop pop + % accumulator last1>=first2? + { 1 add } if + % updated-accumulator +} forall +int_to_str show + +showpage +quit + diff --git a/2022/day04.txt b/2022/day04.txt @@ -0,0 +1,6 @@ +2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8