this post was submitted on 02 Dec 2023
5 points (100.0% liked)

Advent of Code

279 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
[–] Andy 1 points 9 months ago* (last edited 9 months ago)

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 .
;