Paste: AoC 2022 Day 2
Author: | nomennescio |
Mode: | factor |
Date: | Fri, 2 Dec 2022 10:07:49 |
Plain Text |
USING: arrays io io.encodings.utf8 io.files kernel math math.parser math.vectors prettyprint sequences ;
IN: aoc2022
CONSTANT: win-draw-lose
{
{ 1 0 2 }
{ 2 1 0 }
{ 0 2 1 }
}
CONSTANT: move
{
{ 2 0 1 }
{ 0 1 2 }
{ 1 2 0 }
}
: parse-file ( path encoding -- pairs ) file-lines [ [ first CHAR: A - ] [ last CHAR: X - ] bi 2array ] map ;
: outcome ( pair -- win-draw-lose ) first2 win-draw-lose nth nth ;
: score ( pairs -- score ) [ [ second 1 + ] [ outcome 3 * ] bi + ] map-sum ;
: play-towards ( pairs -- pairs' ) [ first2 dupd move nth nth 2array ] map ;
: part1 ( -- ) "input-2.txt" utf8 parse-file score . ;
: part2 ( -- ) "input-2.txt" utf8 parse-file play-towards score . ;
: day2 ( -- ) part1 part2 ;
MAIN: day2
New Annotation