Paste: AoC 2022 day 2

Author: xr
Mode: factor
Date: Fri, 2 Dec 2022 09:34:48
Plain Text |
! Copyright (C) 2022 Your name.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators io.files splitting math kernel sequences io.encodings.utf8 ;
IN: aoc2022-2

: outcome-score ( x y -- n ) {
  { "X" [ {
    { "A" [ 3 ] }
    { "B" [ 0 ] }
    { "C" [ 6 ] }
    } case ] }
  { "Y" [ {
    { "A" [ 6 ] }
    { "B" [ 3 ] }
    { "C" [ 0 ] }
    } case ] }
  { "Z" [ {
    { "A" [ 0 ] }
    { "B" [ 6 ] }
    { "C" [ 3 ] }
    } case ] }
} case ;

: shape-score ( x -- n ) {
 { "X" [ 1 ] }
 { "Y" [ 2 ] }
 { "Z" [ 3 ] } } case ;

: row-score ( x y -- n ) dup shape-score [ outcome-score ] dip + ;

: load-file ( path -- seq ) utf8 file-lines [ " " split ] map ;

: part1 ( -- n ) "resource:/work/aoc2022-2/input.txt" load-file [ first2 row-score ] map sum ;

: outcome-score2 ( x -- n ) {
  { "X" [ 0 ] }
  { "Y" [ 3 ] }
  { "Z" [ 6 ] } } case ;

: choose-shape ( x y -- n ) {
  { "X" [ {
    { "A" [ "Z" ] }
    { "B" [ "X" ] }
    { "C" [ "Y" ] }
    } case ] }
  { "Y" [ {
    { "A" [ "X" ] }
    { "B" [ "Y" ] }
    { "C" [ "Z" ] }
    } case ] }
  { "Z" [ {
    { "A" [ "Y" ] }
    { "B" [ "Z" ] }
    { "C" [ "X" ] }
    } case ] }
} case ;

: row-score2 ( x y -- n ) dup outcome-score2 [ choose-shape shape-score ] dip + ;

: part2 ( -- n ) "resource:/work/aoc2022-2/input.txt" load-file [ first2 row-score2 ] map sum ;

New Annotation

Summary:
Author:
Mode:
Body: