advent-of-code

advent of code attempts
git clone git://bvnf.space/advent-of-code.git
Log | Files | Refs

a.ft (668B)


      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
\ A B C are the opponent's moves; X Y Z are ours.

1 constant ROCK
2 constant PAPER
3 constant SCISSORS

ROCK constant A
PAPER constant B
SCISSORS constant C

variable total
0 total !

: add_to_total ( n -- ) total @ + total ! ;
: win  ( -- ) 6 add_to_total ;
: draw ( -- ) 3 add_to_total ;

: X ( n -- ) DUP ROCK
    DUP add_to_total
    = IF draw DROP
        ELSE SCISSORS = IF ( rock beats scissors ) win THEN
    THEN ;
: Y DUP PAPER
    DUP add_to_total
    = IF draw DROP
        ELSE ROCK = IF ( paper beats rock) win THEN
    THEN ;
: Z DUP SCISSORS
    DUP add_to_total
    = IF draw DROP
        ELSE PAPER = IF ( scissors beats paper ) win THEN
    THEN ;