Paste: AOC day 2 over-engineered
Author: | chunes |
Mode: | factor |
Date: | Wed, 2 Dec 2020 13:00:12 |
Plain Text |
USING: accessors classes.tuple combinators io.encodings.ascii
io.files kernel literals math math.order parser-combinators
parser-combinators.simple prettyprint sequences strings unicode ;
IN: aoc.2020.02
<<
ALIAS: int integer-parser
: range ( -- p ) int "-" token <& int <&> ;
: char ( -- p ) [ letter? ] satisfy ;
: password ( -- p ) char <+> [ >string ] <@ ;
: `policy` ( -- p )
range " " token <& char <:&> ": " token <& password <:&> ;
TUPLE: policy low high char password ;
: <policy> ( str -- obj ) `policy` parse-1 policy slots>tuple ;
>>
CONSTANT: input
$[
"resource:work/aoc/2020/02/input.txt" ascii file-lines
[ <policy> ] map
]
: valid1? ( policy -- ? )
{
[ password>> ]
[ char>> [ = ] curry count ]
[ low>> ]
[ high>> between? ]
} cleave ;
: valid2? ( policy -- ? )
{
[ low>> 1 - ]
[ password>> nth ]
[ char>> = ]
[ high>> 1 - ]
[ password>> nth ]
[ char>> = xor ]
} cleave ;
: part1 ( -- ) input [ valid1? ] count . ;
: part2 ( -- ) input [ valid2? ] count . ;
New Annotation