Paste: aoc2

Author: erg
Mode: factor
Date: Fri, 2 Dec 2022 17:51:26
Plain Text |
! Copyright (C) 2022 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs combinators kernel math sequences splitting ;
IN: 2022-aoc2

: parse-round ( string -- them us )
    " " split1
    [ first ] bi@ [ CHAR: A - ] [ CHAR: X - ] bi* ;

: we-win? ( them us -- ? )
    2array { { 0 1 } { 1 2 } { 2 0 } } member? ;

: score-win-loss ( them us -- pts )
    2dup = [ 2drop 3 ] [ we-win? 6 0 ? ] if ;

: score-choice ( us -- pts ) 1 + ;

: aoc2-1 ( string -- answer )
    split-lines 0 [
        parse-round
        [ score-win-loss ] [ nip score-choice ] 2bi + +
    ] reduce ;

: choose-us ( them condition -- them us )
    {
        { 0 [ dup H{ { 0 2 } { 1 0 } { 2 1 } } at ] }
        { 1 [ dup ] }
        { 2 [ dup H{ { 0 1 } { 1 2 } { 2 0 } } at ] }
    } case ;

: aoc2-2 ( string -- answer )
    split-lines 0 [
        parse-round
        choose-us
        [ score-win-loss ] [ nip score-choice ] 2bi + +
    ] reduce ;

New Annotation

Summary:
Author:
Mode:
Body: