Paste: The fixed version of day 1

Author: Darkey
Mode: factor
Date: Sun, 3 Dec 2023 17:23:07
Plain Text |
! Copyright (C) 2023 Nayden Pendov.
! See https://factorcode.org/license.txt for BSD license.
USING: arrays formatting io.encodings.utf8 io.files kernel math
math.parser 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 ;

: place-digits ( str -- str ) digit-names [
                              [ first dup ] [ last number>string ] bi
                              over 3array concat ] 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 ) [ place-digits get-numbers ] map sum ;

: main ( -- ) read-file [ part1 ] [ part2 ] bi show ;

MAIN: main

New Annotation

Summary:
Author:
Mode:
Body: