]> code.bitgloo.com Git - clyne/advent-of-code.git/commitdiff
day2: cleaner approach
authorClyne Sullivan <clyne@bitgloo.com>
Fri, 2 Dec 2022 12:20:02 +0000 (07:20 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Fri, 2 Dec 2022 12:20:02 +0000 (07:20 -0500)
day2/both-clean.forth [new file with mode: 0644]

diff --git a/day2/both-clean.forth b/day2/both-clean.forth
new file mode 100644 (file)
index 0000000..38f27b7
--- /dev/null
@@ -0,0 +1,35 @@
+1 constant rock
+2 constant paper
+3 constant scissors
+
+: is-win ( them us -- yes ) - dup <0 swap 2 = or ;
+
+: get-score ( them us -- score )
+  2dup is-win if 6 + else
+  2dup = if 3 +
+  then then nip ;
+
+: get-loser ( them -- us ) 2 - 3 mod 1+ ;
+: get-winner ( them -- us ) 3 mod 1+ ;
+
+: A rock ;
+: B paper ;
+: C scissors ;
+: X rock get-score + ;
+: Y paper get-score + ;
+: Z scissors get-score + ;
+
+0
+include input
+. cr
+
+: X dup get-loser get-score + ;
+: Y dup get-score + ;
+: Z dup get-winner get-score + ;
+
+0
+include input
+. cr
+
+bye
+