Paste: AoC Day 2
Author: | CapitalEx |
Mode: | factor |
Date: | Fri, 2 Dec 2022 13:41:55 |
Plain Text |
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 ;
: solve-with ( quot -- x )
[ get-input-one ] dip [ call( x -- x ) ] curry map sum ;
: result ( round -- result )
{
{ "A X" [ +tie+ ] }
{ "A Y" [ +win+ ] }
{ "A Z" [ +lose+ ] }
{ "B X" [ +lose+ ] }
{ "B Y" [ +tie+ ] }
{ "B Z" [ +win+ ] }
{ "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 ;
: result* ( round -- result )
move>fixnum 1 - { +lose+ +tie+ +win+ } nth ;
: move>fixnum* ( move -- fixnum )
{
{ "A X" [ 3 ] }
{ "A Y" [ 1 ] }
{ "A Z" [ 2 ] }
{ "B X" [ 1 ] }
{ "B Y" [ 2 ] }
{ "B Z" [ 3 ] }
{ "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 ;
New Annotation