Paste: AoC #1
Author: | Capital |
Mode: | factor |
Date: | Sun, 3 Dec 2023 03:49:23 |
Plain Text |
USING: assocs io io.encodings.utf8 io.files kernel math.parser
multiline pair-rocket peg.ebnf prettyprint sequences splitting
strings unicode ;
IN: aoc.2023.1
CONSTANT: lookup H{
"one" => "1" "two" => "2" "three" => "3"
"four" => "4" "five" => "5" "six" => "6"
"seven" => "7" "eight" => "8" "nine" => "9"
"oneight" => "18" "twone" => "21"
"threeight" => "38" "fiveight" => "58"
"sevenine" => "78" "eightwo" => "82"
"eighthree" => "83" "nineight" => "98"
}
EBNF: calibration-parser [=[
Spelt = ("one"|"two"|"three"|"four"|"five"|"six"|"seven"|"eight"|"nine") => [[ lookup at ]]
Bullshit = ("oneight"|"twone"|"threeight"|"fiveight"|"sevenine"|"eightwo"|"eighthree"|"nineight") => [[ lookup at ]]
Digit = . ?[ digit? ]? => [[ 1string ]]
Junk = . => [[ drop f ]]
Rules = (Digit|Bullshit|Spelt|Junk)*
]=]
: input ( -- string )
"vocab:aoc/2023/1/input.txt" utf8 file-contents ;
: find-calibration ( line -- string )
[ 1 head ] [ 1 tail* ] bi "" glue dup print dec> ; inline
: find-calibrations-by ( lines quot -- calibrations )
'[ @ find-calibration ] map ; inline
: part-one-solver ( -- quot )
[ [ digit? ] filter ] ; inline
: part-two-solver ( -- quot )
[ calibration-parser sift concat ] ; inline
: run-solver ( quot -- )
[ input split-lines ] dip find-calibrations-by sum . ; inline
: solve-part-one ( -- ) part-one-solver run-solver ;
: solve-part-two ( -- ) part-two-solver run-solver ;
New Annotation