hal9001@lemmy.world to Advent of Code@lemmy.worldEnglish · 11 months agoDay 4 Solutionsadventofcode.comexternal-linkmessage-square4fedilinkarrow-up16arrow-down10
arrow-up16arrow-down1external-linkDay 4 Solutionsadventofcode.comhal9001@lemmy.world to Advent of Code@lemmy.worldEnglish · 11 months agomessage-square4fedilink
minus-squareAndy@programming.devlinkfedilinkarrow-up1·edit-211 months agoIn Factor: Here it is on GitHub with comments and imports. : line>cards ( line -- winning-nums player-nums ) ":|" split rest [ [ CHAR: space = ] trim split-words harvest [ string>number ] map ] map first2 ; : points ( winning-nums player-nums -- n ) intersect length dup 0 > [ 1 - 2^ ] when ; : part1 ( -- ) "vocab:aoc-2023/day04/input.txt" utf8 file-lines [ line>cards points ] map-sum . ; : follow-card ( i commons -- n ) [ 1 ] 2dip 2dup nth swapd over + (a..b] [ over follow-card ] map-sum nip + ; : part2 ( -- ) "vocab:aoc-2023/day04/input.txt" utf8 file-lines [ line>cards intersect length ] map dup length <iota> swap '[ _ follow-card ] map-sum . ;
In Factor:
Here it is on GitHub with comments and imports.
: line>cards ( line -- winning-nums player-nums ) ":|" split rest [ [ CHAR: space = ] trim split-words harvest [ string>number ] map ] map first2 ; : points ( winning-nums player-nums -- n ) intersect length dup 0 > [ 1 - 2^ ] when ; : part1 ( -- ) "vocab:aoc-2023/day04/input.txt" utf8 file-lines [ line>cards points ] map-sum . ; : follow-card ( i commons -- n ) [ 1 ] 2dip 2dup nth swapd over + (a..b] [ over follow-card ] map-sum nip + ; : part2 ( -- ) "vocab:aoc-2023/day04/input.txt" utf8 file-lines [ line>cards intersect length ] map dup length <iota> swap '[ _ follow-card ] map-sum . ;