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

day08.ps (7191B)


      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- day08.ps <day4.txt | display
     19 
     20 % array new-item -> new-array
     21 /append {
     22   exch aload length
     23   % new-item item-0 item-1 ... item-n-1 n
     24   dup dup 2 add exch 1 add roll
     25   % item-0 item-1 ... item-n-1 n new-item
     26   exch
     27   % item-0 item-1 ... item-n-1 new-item n
     28   1 add array astore
     29 } bind def
     30 
     31 % line-count col-count data-array dx dy x y -> is-visible?
     32 /is-visible {
     33   % line-count col-count data-array dx dy x y
     34   dup 6 index mul 2 index add
     35   % line-count col-count data-array dx dy x y offset
     36   5 index exch get
     37   % line-count col-count data-array dx dy x y ref-height
     38   5 1 roll
     39   % line-count col-count data-array ref-height dx dy x y
     40   {
     41     2 index add
     42     exch
     43     3 index add
     44     exch
     45     % line-count col-count data-array ref-height dx dy updated-x updated-y
     46     false
     47     % line-count col-count data-array ref-height dx dy x y loop-terminated?
     48     2 index 0 lt or  % x < 0
     49     1 index 0 lt or  % y < 0
     50     2 index 8 index ge or % x >= col-count
     51     1 index 9 index ge or % y >= line-count
     52     % line-count col-count data-array ref-height dx dy x y loop-terminated?
     53     { pop pop pop pop pop pop pop pop true exit } if
     54     % line-count col-count data-array ref-height dx dy x y
     55     dup 7 index mul 2 index add
     56     % line-count col-count data-array ref-height dx dy x y offset
     57     6 index exch get
     58     % line-count col-count data-array ref-height dx dy x y cur-height
     59     5 index ge
     60     % line-count col-count data-array ref-height dx dy x y is-hiding?
     61     { pop pop pop pop pop pop pop pop false exit } if
     62   } loop
     63 } bind def
     64 
     65 % line-count col-count data-array x y -> is-visible?
     66 /is-visible-4 {
     67   false
     68   % line-count col-count data-array x y false
     69   6 copy pop  0 -1  4 2 roll  is-visible or
     70   % line-count col-count data-array x y visible-from-top?
     71   6 copy pop  0  1  4 2 roll  is-visible or
     72   % line-count col-count data-array x y visible-from-top-or-bottom?
     73   6 copy pop  -1 0  4 2 roll  is-visible or
     74   % line-count col-count data-array x y visible-from-top-or-bottom-or-left?
     75   6 copy pop   1 0  4 2 roll  is-visible or
     76   % line-count col-count data-array x y is-visible?
     77   6 1 roll
     78   % is-visible? line-count col-count data-array x y
     79   pop pop pop pop pop
     80   % is-visible?
     81 } bind def
     82 
     83 % line-count col-count data-array dx dy x y -> visible-count
     84 /visible-count {
     85   % line-count col-count data-array dx dy x y
     86   dup 6 index mul 2 index add
     87   % line-count col-count data-array dx dy x y offset
     88   5 index exch get
     89   % line-count col-count data-array dx dy x y ref-height
     90   0 6 2 roll
     91   % line-count col-count data-array ref-height counter dx dy x y
     92   {
     93     % line-count col-count data-array ref-height count dx dy prev-x prev-y
     94     2 index add
     95     exch
     96     3 index add
     97     exch
     98     % line-count col-count data-array ref-height count dx dy x y
     99     false
    100     % line-count col-count data-array ref-height count dx dy x y loop-finished?
    101     2 index 0 lt or  % x < 0
    102     1 index 0 lt or  % y < 0
    103     2 index 9 index ge or % x >= col-count
    104     1 index 10 index ge or % y >= line-count
    105     % line-count col-count data-array ref-height count dx dy x y loop-finished?
    106     { pop pop pop pop
    107       % line-count col-count data-array ref-height count
    108       5 1 roll
    109       % count line-count col-count data-array ref-height
    110       pop pop pop pop
    111       % count
    112       exit
    113     } if
    114     % line-count col-count data-array ref-height prev-count dx dy x y
    115     5 4 roll 1 add 5 1 roll
    116     % line-count col-count data-array ref-height count dx dy x y
    117     dup 8 index mul 2 index add
    118     % line-count col-count data-array ref-height count dx dy x y offset
    119     7 index exch get
    120     % line-count col-count data-array ref-height count dx dy x y cur-height
    121     6 index ge
    122     % line-count col-count data-array ref-height count dx dy x y is-hiding?
    123     { pop pop pop pop
    124       % line-count col-count data-array ref-height count
    125       5 1 roll
    126       % count line-count col-count data-array ref-height
    127       pop pop pop pop
    128       % count
    129       exit
    130     } if
    131   } loop
    132 } bind def
    133 
    134 % line-count col-count data-array x y -> scenic-score
    135 /scenic-score {
    136   1
    137   % line-count col-count data-array x y 1
    138   6 copy pop  0 -1  4 2 roll  visible-count mul
    139   % line-count col-count data-array x y top
    140   6 copy pop  0  1  4 2 roll  visible-count mul
    141   % line-count col-count data-array x y top*bottom
    142   6 copy pop  -1 0  4 2 roll  visible-count mul
    143   % line-count col-count data-array x y top*bottom*left
    144   6 copy pop   1 0  4 2 roll  visible-count mul
    145   % line-count col-count data-array x y score
    146   6 1 roll
    147   % score line-count col-count data-array x y
    148   pop pop pop pop pop
    149   % score
    150 } bind def
    151 
    152 
    153 /datafile (%stdin) (r) file def
    154 /stderr (%stderr) (w) file def
    155 
    156 0 0 array {
    157   % line-count data-array
    158   datafile 120 string readline
    159   not { pop exit } if
    160   % line-count data-array line
    161   {
    162     % line-count data-array char
    163     48 sub
    164     append
    165   } forall
    166   % line-count updated-data-array
    167   exch 1 add exch
    168   % updated-line-count updated-data-array
    169 } loop
    170 
    171 % line-count data-array
    172 dup length 2 index idiv exch
    173 % line-count col-count data-array
    174 
    175 /Helvetica 20 selectfont
    176 
    177 (First Puzzle: )
    178 72 700 moveto show
    179 0
    180 % init-line-count col-count data-array 0
    181 0 1 5 index 1 sub {
    182   % line-count col-count data-array cur-count y
    183   0 1 5 index 1 sub {
    184     % line-count col-count data-array cur-count y x
    185     6 copy exch 3 2 roll pop
    186     % [save] line-count col-count data-array x y
    187     is-visible-4
    188     % line-count col-count data-array cur-count y x is-visible?
    189     { 3 2 roll 1 add 3 1 roll } if
    190     % line-count col-count data-array updated-count y x
    191     pop
    192   } for
    193   pop
    194 } for
    195 % line-count col-count data-array visible-count
    196 15 string cvs show
    197 
    198 (Second Puzzle: )
    199 72 664 moveto show
    200 0 4 1 roll
    201 % init-best-score line-count col-count data-array
    202 1 1 4 index 2 sub {
    203   % best-score line-count col-count data-array y
    204   1 1 4 index 2 sub {
    205     % best-score line-count col-count data-array y x
    206     5 copy exch
    207     % [save] line-count col-count data-array x y
    208     scenic-score
    209 %dup 15 string cvs stderr exch writestring
    210 %stderr 9 write
    211     % best-score line-count col-count data-array y x cur-score
    212     dup 7 index gt
    213     % best-score line-count col-count data-array y x cur-score is-better?
    214     { 7 6 roll pop 6 1 roll }
    215     { pop }
    216     ifelse
    217     % updated-best-score line-count col-count data-array y x
    218     pop
    219   } for
    220 %stderr 10 write
    221   pop
    222 } for
    223 % best-score line-count col-count data-array
    224 4 3 roll
    225 15 string cvs show
    226 
    227 showpage
    228 quit