day02.ps (2875B)
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- -r300 -dDownScaleFactor=3 day02.ps <day02.txt | display 19 20 /str_to_int { 21 0 exch { exch 10 mul add 48 sub } forall 22 } bind def 23 24 /int_to_str { 25 % number 26 1 10 27 { % number length upper-bound 28 2 index 1 index lt { exit } if 29 exch 1 add exch 10 mul } loop 30 pop dup string exch 1 sub 31 { % number string index 32 2 copy 1 sub 5 1 roll exch 33 % next_index number string string index 34 3 index 10 mod 48 add 35 % next_index number string string index digit 36 put 37 % next_index number next_string 38 3 1 roll 10 idiv 39 % next_string next_index next_number 40 dup 0 eq { pop pop exit } if 41 % next_string next_index next_number 42 3 1 roll 43 % next_nubmer next_string next_index 44 } loop 45 } bind def 46 47 /apush { 48 % array new_item 49 exch aload length 1 add array astore 50 } bind def 51 52 /apop { 53 aload length 1 sub array astore exch 54 } bind def 55 56 /datafile (%stdin) (r) file def 57 58 /data [ 59 { 60 datafile 100 string readline 61 not {pop exit} if 62 % line 63 dup 0 get 64 sub exch 64 % abc-mapped-to-123 line 65 2 get 87 sub 66 % abc-mapped-to-123 xyz-mapped-to-123 67 2 array astore 68 } loop 69 ] def 70 71 /Helvetica 20 selectfont 72 73 (First Puzzle: ) 74 72 700 moveto show 75 0 data { 76 % prev-score cur-item 77 aload pop 78 % prev-score other-choice my-choice 79 0 80 3 copy pop eq { 3 add } if 81 3 copy pop sub 3 add 3 mod 2 eq { 6 add } if 82 % prev-score other-choice my-choice outcome-score 83 add exch pop 84 % prev-score cur-total-score 85 add 86 } forall 87 int_to_str show 88 89 (Second Puzzle: ) 90 72 664 moveto show 91 0 data { 92 % prev-score cur-item 93 aload pop exch 94 % prev-score outcome-type other-choice 95 % Here we use that win is +1 in choice numeric values and losing is -1, 96 % wrapping around in 1 .. 3 97 % So my-choice = wrap(other-choice + outcome-type - 2) 98 % with wrap(x) = ((x - 1) mod 3) + 1 99 % so my-choice = (other-choice + outcome-type mod 3) + 1 100 1 index add 3 mod 1 add 101 % prev-score outcome-type my-choice 102 exch 1 sub 3 mul 103 % prev-score my-choice outcome-score 104 add 105 % prev-score cur-total-score 106 add 107 } forall 108 int_to_str show 109 110 showpage 111 quit