Paste: AOC Day 1

Author: Kacarott
Mode: factor
Date: Fri, 2 Dec 2022 16:52:50
Plain Text |
! Copyright (C) 2022 Keldan Chapman.
! See http://factorcode.org/license.txt for BSD license.
USING: kernel AOC splitting math math.parser sequences heaps io accessors ;
IN: AOC.2022.1

: calories ( input -- seq )
    "\n\n" split-subseq [ "\n" split harvest [ dec> ] map-sum ] map ;

: part-1 ( input -- result ) calories supremum ;

! Size limited heap sort?
: part-2 ( input -- result ) calories V{ } like <min-heap> swap
    ! Initialise
    3 [ [ pop f spin heap-push ] 2keep ] times
    ! Map values
    [ dupd f spin heap-push dup heap-pop* ] each
    ! Extract result
    0 swap [ nip + ] slurp-heap ;

MAIN: [ 1 read-day-input [ part-1 print ] [ part-2 print ] bi ]

New Annotation

Summary:
Author:
Mode:
Body: