hal9001@lemmy.world to Advent of Code@lemmy.worldEnglish · 1 year agoDay 2 Solutionsadventofcode.comexternal-linkmessage-square4fedilinkarrow-up15arrow-down10 cross-posted to: hackernews@derp.foo
arrow-up15arrow-down1external-linkDay 2 Solutionsadventofcode.comhal9001@lemmy.world to Advent of Code@lemmy.worldEnglish · 1 year agomessage-square4fedilink cross-posted to: hackernews@derp.foo
minus-squareAndy@programming.devlinkfedilinkarrow-up1·edit-21 year agoIn Factor: Here it is on GitHub with comments and imports. : known-color ( color-phrases regexp -- n ) all-matching-subseqs [ 0 ] [ [ split-words first string>number ] map-supremum ] if-empty ; : line>known-rgb ( str -- game-id known-rgb ) ": " split1 [ split-words last string>number ] dip R/ \d+ red/ R/ \d+ green/ R/ \d+ blue/ [ known-color ] tri-curry@ tri 3array ; : possible? ( known-rgb test-rgb -- ? ) v<= [ ] all? ; : part1 ( -- ) "vocab:aoc-2023/day02/input.txt" utf8 file-lines [ line>known-rgb 2array ] [ last { 12 13 14 } possible? ] map-filter [ first ] map-sum . ; : part2 ( -- ) "vocab:aoc-2023/day02/input.txt" utf8 file-lines [ line>known-rgb nip product ] map-sum . ;
In Factor:
Here it is on GitHub with comments and imports.
: known-color ( color-phrases regexp -- n ) all-matching-subseqs [ 0 ] [ [ split-words first string>number ] map-supremum ] if-empty ; : line>known-rgb ( str -- game-id known-rgb ) ": " split1 [ split-words last string>number ] dip R/ \d+ red/ R/ \d+ green/ R/ \d+ blue/ [ known-color ] tri-curry@ tri 3array ; : possible? ( known-rgb test-rgb -- ? ) v<= [ ] all? ; : part1 ( -- ) "vocab:aoc-2023/day02/input.txt" utf8 file-lines [ line>known-rgb 2array ] [ last { 12 13 14 } possible? ] map-filter [ first ] map-sum . ; : part2 ( -- ) "vocab:aoc-2023/day02/input.txt" utf8 file-lines [ line>known-rgb nip product ] map-sum . ;