Paste: AoC 2022 Day 2

Author: nomennescio
Mode: factor
Date: Fri, 2 Dec 2022 10:07:49
Plain Text |
! 2022 nomennescio
USING: arrays io io.encodings.utf8 io.files kernel math math.parser math.vectors prettyprint sequences ;
IN: aoc2022

! rock     0
! paper    1
! scissors 2

! lose     0
! draw     1
! win      2

CONSTANT: win-draw-lose
{
  { 1 0 2 } ! you play rock
  { 2 1 0 } ! you play paper
  { 0 2 1 } ! you play scissors
}

CONSTANT: move
{
  { 2 0 1 } ! you need to lose
  { 0 1 2 } ! you need to draw
  { 1 2 0 } ! you need to win
}

: 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

Summary:
Author:
Mode:
Body: