USING: combinators io.backend io.encodings.utf8 io.files kernel math sequences ; IN: advent-of-code.day-02 SYMBOLS: +win+ +tie+ +lose+ ; : get-input-one ( -- file ) "vocab:advent-of-code/day-02/_input/one.txt" normalize-path utf8 file-lines ; : result>fixnum ( result -- fixnum ) { { +win+ [ 6 ] } { +tie+ [ 3 ] } { +lose+ [ 0 ] } } case ; ! Needless factoring : solve-with ( quot -- x ) [ get-input-one ] dip [ call( x -- x ) ] curry map sum ; ! Solution for Part One : result ( round -- result ) { ! Rock vs... { "A X" [ +tie+ ] } { "A Y" [ +win+ ] } { "A Z" [ +lose+ ] } ! Paper vs... { "B X" [ +lose+ ] } { "B Y" [ +tie+ ] } { "B Z" [ +win+ ] } ! Scissors vs... { "C X" [ +win+ ] } { "C Y" [ +lose+ ] } { "C Z" [ +tie+ ] } } case ; : move>fixnum ( move -- fixnum ) last { { CHAR: X [ 1 ] } { CHAR: Y [ 2 ] } { CHAR: Z [ 3 ] } } case ; : round-value ( round -- fixnum ) [ result result>fixnum ] [ move>fixnum ] bi + ; : solve-problem-one ( -- solution ) [ round-value ] solve-with ; ! Solution for Part Two : result* ( round -- result ) move>fixnum 1 - { +lose+ +tie+ +win+ } nth ; : move>fixnum* ( move -- fixnum ) { ! Rock vs... { "A X" [ 3 ] } { "A Y" [ 1 ] } { "A Z" [ 2 ] } ! Paper vs... { "B X" [ 1 ] } { "B Y" [ 2 ] } { "B Z" [ 3 ] } ! Scissors vs... { "C X" [ 2 ] } { "C Y" [ 3 ] } { "C Z" [ 1 ] } } case ; : round-value* ( round -- fixnum ) [ result* result>fixnum ] [ move>fixnum* ] bi + ; : solve-problem-two ( -- solution ) [ round-value* ] solve-with ;