mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
do day2
This commit is contained in:
parent
6ac09bd75a
commit
f8d81983cd
42
2023/src/day02.clj
Normal file
42
2023/src/day02.clj
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
(ns ^{:doc "Day 2"
|
||||||
|
:author "Adam Jeniski"}
|
||||||
|
day02 (:require [core :refer [get-puzzle-input]]
|
||||||
|
[clojure.string :as string]))
|
||||||
|
|
||||||
|
(defn parse-line [line]
|
||||||
|
(let [[_ id data] (re-find #"Game (\d+): (.*)" line)
|
||||||
|
states (->> (string/split data #";")
|
||||||
|
(map #(map string/trim (string/split % #",")))
|
||||||
|
(map #(map (fn [s]
|
||||||
|
(let [[num color] (string/split s #" ")]
|
||||||
|
[(Integer/parseInt num) color])) %)))]
|
||||||
|
[id states]))
|
||||||
|
|
||||||
|
(def lines (->> (get-puzzle-input 2)
|
||||||
|
(map parse-line)))
|
||||||
|
|
||||||
|
(def cube-counts {"red" 12, "green" 13, "blue" 14})
|
||||||
|
|
||||||
|
;; part 1
|
||||||
|
(->> lines
|
||||||
|
(filter (fn [[_id states]]
|
||||||
|
(not (some
|
||||||
|
#(some (fn [[cnt color]]
|
||||||
|
(< (cube-counts color) cnt)) %)
|
||||||
|
states))))
|
||||||
|
(map (fn [[id]] (Integer/parseInt id)))
|
||||||
|
(reduce +))
|
||||||
|
|
||||||
|
;; part 2
|
||||||
|
(->> lines
|
||||||
|
(map (fn [[_id states]]
|
||||||
|
(reduce #(reduce (fn [acc [cnt color]]
|
||||||
|
(update acc color max cnt))
|
||||||
|
%1
|
||||||
|
%2)
|
||||||
|
{"green" 0, "red" 0, "blue" 0}
|
||||||
|
states)))
|
||||||
|
(map #(* (% "green")
|
||||||
|
(% "red")
|
||||||
|
(% "blue")))
|
||||||
|
(reduce +))
|
Loading…
x
Reference in New Issue
Block a user