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

Advent of Code

264 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
top 1 comments
sorted by: hot top controversial new old
[–] [email protected] 1 points 11 months ago* (last edited 10 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 .
;