commit f234fbc91972acc73391470620c60bcb77939bbc parent 8c16d9132fad1240a21f8b40bc202f5e8bc7210f Author: alex wennerberg <alex@alexwennerberg.com> Date: Thu, 1 Dec 2022 23:56:41 -0800 squeaky clean day 2 solution Diffstat:
M | 2022/02.fs | | | 41 | +++++++++++++++++------------------------ |
D | 2022/02b.fs | | | 48 | ------------------------------------------------ |
2 files changed, 17 insertions(+), 72 deletions(-)
diff --git a/2022/02.fs b/2022/02.fs @@ -1,42 +1,35 @@ -\ A rock B paper C scissors -\ X rock Y paper Z scissors - variable score -variable ct - -: barray create allot does> swap + ; -: combine 2 lshift or ; -: points-of 1+ ; 0 constant rock 1 constant paper 2 constant scissors -11 barray matchups +: points-of 1+ ; + +: victim ( u1 -- u2 ) 1- dup 0< if drop 2 exit then ; +: foil ( u1 -- u2 ) 1+ 3 mod ; -\ them us (manual scores, awk TODO simplify ) -3 rock rock combine matchups c! -0 paper rock combine matchups c! -6 scissors rock combine matchups c! -3 paper paper combine matchups c! -0 scissors paper combine matchups c! -6 rock paper combine matchups c! -3 scissors scissors combine matchups c! -0 rock scissors combine matchups c! -6 paper scissors combine matchups c! +: game-score ( u1 u2 -- u ) + 2dup swap foil = if 2drop 6 exit then + 2dup swap victim = if 2drop 0 exit then + 2drop 3 ; -: add-score 1 ct +! dup>r combine matchups c@ dup u. r> points-of + score +! ; +: add-score dup>r game-score r> points-of + score +! ; \ data is code : A rock ; : B paper ; : C scissors ; - -\ us : X rock add-score ; : Y paper add-score ; : Z scissors add-score ; - include input.txt - score @ u. cr \ part 1 + +0 score ! +: X dup victim add-score ; +: Y dup add-score ; +: Z dup foil add-score ; +include input.txt +score @ u. cr \ part 2 +bye diff --git a/2022/02b.fs b/2022/02b.fs @@ -1,48 +0,0 @@ -\ A rock B paper C scissors -\ X rock Y paper Z scissors - -variable score -variable ct - -: barray create allot does> swap + ; -: combine 2 lshift or ; -: points-of 1+ ; - -0 constant rock -1 constant paper -2 constant scissors - -11 barray matchups - -\ them us (manual scores, awk TODO simplify ) -3 rock rock combine matchups c! -0 paper rock combine matchups c! -6 scissors rock combine matchups c! -3 paper paper combine matchups c! -0 scissors paper combine matchups c! -6 rock paper combine matchups c! -3 scissors scissors combine matchups c! -0 rock scissors combine matchups c! -6 paper scissors combine matchups c! - -: add-score 1 ct +! dup>r combine matchups c@ r> points-of + .s cr score +! ; - -\ data is code -: A rock ; -: B paper ; -: C scissors ; - -: get-loser dup dup dup rock = if 2drop scissors exit then - paper = if drop rock exit then - scissors = if paper ; -: get-winner dup dup dup rock = if 2drop paper exit then - paper = if drop scissors exit then - scissors = if rock then ; - -: X get-loser add-score ; -: Y dup add-score ; \ draw -: Z get-winner add-score ; - -include input.txt - -score @ u. cr \ part 1