Paste: ac2023 day 1
Author: | Darkey |
Mode: | factor |
Date: | Fri, 1 Dec 2023 17:59:09 |
Plain Text |
USING: arrays formatting io.encodings.utf8 io.files kernel math
math.parser scratchpad sequences splitting unicode ;
IN: ac2023.day1
: first+last ( str -- 2arr ) [ first ] [ last ] bi 2array ;
: digit-names ( -- arr ) { "one" "two" "three" "four" "five"
"six" "seven" "eight" "nine" } [ 1 + 2array ] map-index ;
: names>digits ( str -- str ) digit-names [
[ first ] [ last number>string ] bi replace
] each ;
: scrap-numbers ( str -- str ) [ digit? ] filter ;
: get-numbers ( str -- n ) scrap-numbers first+last string>number ;
: show ( n n -- ) "%d\n%d\n" printf ;
: read-file ( -- arr ) "/home/dwolf/Свалени/ac2023_input.txt" utf8 file-lines ;
: part1 ( arr -- n ) [ get-numbers ] map sum ;
: part2 ( arr -- n ) [ names>digits get-numbers ] map sum ;
: main ( -- ) read-file [ part1 ] [ part2 ] bi show ;
MAIN: main
New Annotation