aoc-2022

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

day10.ps (3639B)


      1 %!PS
      2 %
      3 % Copyright (c) 2022, Natacha Porté
      4 %
      5 % Permission to use, copy, modify, and distribute this software for any
      6 % purpose with or without fee is hereby granted, provided that the above
      7 % copyright notice and this permission notice appear in all copies.
      8 %
      9 % THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     10 % WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     11 % MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     12 % ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     13 % WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     14 % ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     15 % OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     16 %
     17 % Usage:
     18 % gs -q- -sDEVICE=png16m -o- day09.ps <day09.txt | display
     19 
     20 /datafile (%stdin) (r) file def
     21 /stderr (%stderr) (w) file def
     22 
     23 % crt reg-dict prev-cycle-count register
     24 %   -> crt reg-dict cycle-count register
     25 /next-cycle {
     26   % crt reg-dict prev-cycle-count register
     27   exch 1 add exch
     28   % crt reg-dict cycle-count register
     29   3 copy pop known
     30   % crt reg-dict cycle-count register save-reg?
     31   { 3 copy put } if
     32   % crt reg-dict cycle-count register
     33   1 index 1 sub 40 mod
     34   % crt reg-dict cycle-count register cur-column
     35   1 index sub abs 1 le
     36   % crt reg-dict cycle-count register sprite-visible?
     37   { 3 index 2 index 1 sub 35 put } if
     38   % crt reg-dict cycle-count register
     39 } bind def
     40 
     41 % crt reg-dict cycle-count register line
     42 %   -> crt reg-dict cycle-count register
     43 /run-line {
     44   % crt reg-dict prev-cycle-count prev-register line
     45   dup (noop) eq {
     46     % crt reg-dict prev-cycle-count prev-register line
     47     pop
     48     % crt reg-dict prev-cycle-count prev-register
     49     next-cycle
     50     % crt reg-dict cycle-count register
     51   }
     52   {
     53     % crt reg-dict prev-cycle-count prev-register line
     54     (addx ) anchorsearch
     55     not { 1.125 pstack quit } if
     56     % crt reg-dict prev-cycle-count prev-register (operand) (addx )
     57     pop cvi
     58     % crt reg-dict prev-cycle-count prev-register operand
     59     5 1 roll
     60     % operand crt reg-dict prev-cycle-count prev-register
     61     next-cycle next-cycle
     62     % operand crt reg-dict cycle-count prev-register
     63     5 4 roll
     64     % crt reg-dict cycle-count prev-register operand
     65     add
     66     % crt reg-dict cycle-count register
     67   }
     68   ifelse
     69   % crt reg-dict cycle-count register
     70 } bind def
     71 
     72 /rawdata [
     73   {
     74     datafile 100 string readline
     75     not { pop exit } if
     76   } loop
     77 ] def
     78 
     79 /Helvetica 20 selectfont
     80 
     81 (................................................................................................................................................................................................................................................)
     82 <<
     83    20 0
     84    60 0
     85   100 0
     86   140 0
     87   180 0
     88   220 0
     89 >>
     90 0 1
     91 % crt reg-dict cycle-count register
     92 rawdata { run-line } forall
     93 % crt reg-dict cycle-count register
     94 
     95 
     96 (First Puzzle: )
     97 72 700 moveto show
     98 % crt reg-dict cycle-count register
     99 0 3 index {
    100   % accumulator key value
    101   mul add
    102   % updated-accumulator
    103 } forall
    104 15 string cvs show
    105 
    106 (Second Puzzle: )
    107 72 664 moveto show
    108 % crt reg-dict cycle-count register
    109 
    110 /Courier-Bold 20 selectfont
    111 0 1 5 index length 40 idiv 1 sub {
    112   % crt reg-dict cycle-count register line-number
    113   dup 20 mul 644 exch sub
    114   % crt reg-dict cycle-count register line-number y
    115   72 exch moveto
    116   % crt reg-dict cycle-count register line-number
    117   dup 40 mul
    118   % crt reg-dict cycle-count register line-number crt-offset
    119   5 index exch 40 getinterval
    120   % crt reg-dict cycle-count register line-number (line)
    121   show
    122   % crt reg-dict cycle-count register line-number
    123   pop
    124 } for
    125 
    126 showpage
    127 quit