aoc-2022

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

commit 23a05686a952a94a21dc894edb263566559e527e
parent aba3e077ee55671ccb59dd81281cdfb4d639be60
Author: Natasha Kerensikova <natacha@instinctive.eu>
Date:   Tue,  6 Dec 2022 13:13:13 +0100

Add day 6 solutions

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

diff --git a/day06.ps b/day06.ps @@ -0,0 +1,119 @@ +%!PS +% +% Copyright (c) 2022, Natacha Porté +% +% Permission to use, copy, modify, and distribute this software for any +% purpose with or without fee is hereby granted, provided that the above +% copyright notice and this permission notice appear in all copies. +% +% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +% +% Usage: +% gs -q- -sDEVICE=png16m -o- day06.ps <day06.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 + +/datafile (%stdin) (r) file def + +/data + datafile 4100 string readline + not { quit } if + def + +% array -> bool +/are-all-different { + % array + dup length 1 sub dup 1 sub + % array high-index low-index + { + % array high-index low-index + 3 copy pop get + % array high-index low-index high-value + 3 index 2 index get + % array high-index low-index high-value low-value + eq { pop pop pop false exit } if + % array high-index low-index + dup 0 eq + { % when low-index wraps around + pop dup 1 eq + { pop pop true exit } if % loop termination + 1 sub dup % index update + } if + % array updated-high-index low-index + 1 sub + % array updated-high-index updated-low-index + } loop + % result +} bind def + +% input-array marker-length -> count +/find-marker { + % input-array marker-length + [ exch { 0 } repeat ] + 1 exch + % input-array first-index backlog-array + 3 2 roll { + % index backlog-array cur-char + exch aload + % index cur-char backlog-n ... backlog-1 backlog-array + dup length dup 2 add exch roll + % index backlog-(n-1) ... backlog-1 backlog-array cur-char backlog-n + pop exch + % index backlog-(n-1) ... backlog-1 cur-char backlog-array + astore + % index updated-backlog-array + 2 copy length ge { + dup are-all-different { pop exit } if + } if + % index updated-backlog-array + exch 1 add exch + % updated-index updated-backlog-array + } forall + % count +} bind def + +/Helvetica 20 selectfont + +(First Puzzle: ) +72 700 moveto show +data 4 find-marker +int_to_str show + +(Second Puzzle: ) +72 664 moveto show +data 14 find-marker +int_to_str show + +showpage +quit