this post was submitted on 04 Dec 2023
6 points (100.0% liked)

Advent of Code

283 readers
1 users here now

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

https://adventofcode.com

founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 1 points 11 months ago* (last edited 10 months ago)

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  swap '[ _ follow-card ]
  map-sum .
;