about summary refs log tree commit diff
path: root/users/tazjin/aoc2023/day2.el
blob: 9374d7862c64fd35e8cb30207d946b335575176e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
(require 'dash)
(require 's)
(require 'f)

(defvar aoc23-day2-example

  "Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green")

;; part 1

(cl-defstruct aoc23d2-set red green blue)

(defun aoc23d2-parse-set (input)
  (let ((set (make-aoc23d2-set :red 0 :green 0 :blue 0))
        (colours (-map #'s-trim (s-split "," input))))
    (cl-loop for colour in colours
             do (pcase (s-split " " colour t)
                  (`(,num "red") (setf (aoc23d2-set-red set) (string-to-number num)))
                  (`(,num "green") (setf (aoc23d2-set-green set) (string-to-number num)))
                  (`(,num "blue") (setf (aoc23d2-set-blue set) (string-to-number num)))))
    set))

(cl-defstruct aoc23d2-game id sets)

(defun aoc23d2-parse-game (input)
  (pcase-let* ((`(,id-str ,sets-str) (s-split-up-to ":" input 1 t))
               (game-id (string-to-number (s-chop-left (length "Game ") id-str)))
               (sets (-map #'aoc23d2-parse-set (s-split ";" sets-str t))))
    (make-aoc23d2-game :id game-id :sets sets)))

(defun aoc23d2-game-possible-p (game r g b)
  (cl-every (lambda (set)
              (and (<= (aoc23d2-set-red set) r)
                   (<= (aoc23d2-set-green set) g)
                   (<= (aoc23d2-set-blue set) b)))
            (aoc23d2-game-sets game)))

(let ((input (f-read "~/Downloads/input.txt")))
  (-sum
   (-map #'aoc23d2-game-id
         (-filter (lambda (g) (aoc23d2-game-possible-p g 12 13 14))
                  (-map #'aoc23d2-parse-game (s-lines (s-trim input)))))))

;; part 2

(defun aoc23d2-game-min-cubes-power (game)
  (let ((r 0)
        (g 0)
        (b 0))
    (-each (aoc23d2-game-sets game)
      (lambda (set)
        (setq r (max r (aoc23d2-set-red set)))
        (setq g (max g (aoc23d2-set-green set)))
        (setq b (max b (aoc23d2-set-blue set)))))
    (* (max 1 r) (max 1 g) (max 1 b))))

(let ((input (f-read "~/Downloads/input.txt")))
  (-sum
   (-map #'aoc23d2-game-min-cubes-power
         (-map #'aoc23d2-parse-game (s-lines (s-trim input))))))