Paste: AoC Day 3

Author: CapitalEx
Mode: factor
Date: Sat, 3 Dec 2022 05:25:50
Plain Text |
USING: grouping hash-sets io.backend io.encodings.utf8 io.files
kernel math sequences sets unicode ;
IN: advent-of-code.day-03

: get-input-one ( -- seq )
    "vocab:advent-of-code/day-03/_input/one.txt" 
        normalize-path utf8 file-lines ;

: find-duplicate ( string -- duplicate )
    dup length 2/ group [ >hash-set ] map first2 intersect members first ;

: >priority ( fixnum -- fixnum )
    dup letter? [ 96 - ] [ 38 - ] if ;

: solve-part-one ( -- solution )
    get-input-one [ find-duplicate >priority ] map sum ;


: find-bagde-item ( seq -- badge )
    [ >hash-set ] map first3 intersect intersect members first ;

: solve-part-two ( -- solution )
    get-input-one 3 group [ find-bagde-item >priority ] map sum ;

Annotation: Refactor

Author: CapitalEx
Mode: factor
Date: Sat, 3 Dec 2022 05:51:03
Plain Text |
USING: grouping hash-sets io.backend io.encodings.utf8 io.files
kernel math sequences sets unicode ;
IN: advent-of-code.day-03

: get-input-one ( -- seq )
    "vocab:advent-of-code/day-03/_input/one.txt" 
        normalize-path utf8 file-lines ;

: >priority ( fixnum -- fixnum )
    dup letter? [ 96 - ] [ 38 - ] if ;

: result ( seq -- item )
    members first ;

: priority-sum-with ( sew quot -- fixnum ) 
    [ >priority ] compose [ call( x -- y ) ] curry map-sum ;

: find-duplicate ( string -- duplicate )
    halves intersect result ;

: solve-part-one ( -- solution )
    get-input-one [ find-duplicate ] priority-sum-with ;

: find-common-item ( seq -- item )
    intersect-all result ;

: solve-part-two ( -- solution )
    get-input-one 3 group [ find-common-item ] priority-sum-with ;

New Annotation

Summary:
Author:
Mode:
Body: