Paste: AoC day 2

Author: Garklein
Mode: factor
Date: Sat, 3 Dec 2022 02:27:22
Plain Text |
USING: io.encodings.utf8 io.files kernel math sequences
splitting ;
IN: aoc.2

: to-move ( str -- n ) { "A" "X" "B" "Y" "C" "Z" } index 2/ ;

: input ( -- i ) "~/factor/aoc/2/2.in" utf8 file-lines
  [ " " split [ to-move ] map ] map ;
  
! first level is first player's move, next is second player's
! rock = 0, paper = 1, scissors = 2
! 0 = first player loses, 1 = tie, 2 = first player wins
: table ( -- t ) { { 1 0 2 } { 2 1 0 } { 0 2 1 } } ;
  
: score ( opp you -- n ) [ table nth nth 3 * ] [ nip 1 + ] 2bi + ;

: part1 ( -- n ) input [ first2 score ] map-sum ;

: part2 ( -- n ) input [ first2 2 swap - dupd swap table nth index score ] map-sum ;

New Annotation

Summary:
Author:
Mode:
Body: