Skip to content

Instantly share code, notes, and snippets.

@maleghast
Created December 3, 2023 17:37
Show Gist options
  • Save maleghast/22d460542ce8f313fb6db54cd92f2196 to your computer and use it in GitHub Desktop.
Save maleghast/22d460542ce8f313fb6db54cd92f2196 to your computer and use it in GitHub Desktop.
Advent of Code 2023 - Day 2 completed Clojure solution
(ns maleghast.aoc2023.day2
(:require [clojure.string :as str]
[clojure.java.io :as io])
(:gen-class))
(def colour-maximums {:blue 14 :green 13 :red 12})
(defn check-game-valid
[game-data]
(reduce
(fn [acc coll]
(let [blue (if (nil? (:blue coll)) 0 (:blue coll))
green (if (nil? (:green coll)) 0 (:green coll))
red (if (nil? (:red coll)) 0 (:red coll))]
(conj acc (and
(<= blue (:blue colour-maximums))
(<= green (:green colour-maximums))
(<= red (:red colour-maximums))))))
[]
game-data
))
(defn get-max-value
[cube-colour game-data]
(->> game-data
(map (fn [coll] (conj [] [cube-colour (get coll cube-colour)])))
sort
last
first
last))
(defn calculate-game-power
[game-data]
(let [max-blue (get-max-value :blue game-data)
max-green (get-max-value :green game-data)
max-red (get-max-value :red game-data)]
(* max-blue max-green max-red)))
(defn get-total-game-power
[games-data]
(reduce
(fn [acc coll]
(+ acc (calculate-game-power (last coll))))
0
games-data))
(defn get-valid-games
"Get the valid games from the collection of all games"
[games-data]
(filter
#(not (some false? (check-game-valid (last %))))
games-data))
(defn acquire-and-process-data
"Function to acquire and clean up data before doing calculations"
[data-file]
(->> (slurp (io/resource data-file))
str/split-lines
(map #(str/replace % #"Game ", ""))
(map #(str/replace % #"^([\d]+):" "$1"))
(into [] (map #(into [] (str/split % #" "))))
(into []
(map
#(into
[]
(conj
[]
(Integer/parseInt
(first %))
(str/join " " (rest %))))))
(into []
(map
#(into
[]
(conj
[]
(first %)
(into
[]
(str/split (last %) #"; "))))))
(into []
(map
#(into
[]
(conj
[]
(first %)
(into
[]
(map
(fn [coll]
(str/split coll #", "))
(last %)))))))
(into []
(map
#(into
[]
(conj
[]
(first %)
(into
[]
(map
(fn [coll]
(str/join " " coll))
(last %)))))))
(into []
(map
#(into
[]
(conj
[]
(first %)
(into
[]
(map
(fn [coll]
(str/split coll #" "))
(last %)))))))
(into []
(map
#(into
[]
(conj
[]
(first %)
(into
[]
(map
(fn [coll]
(into [] (reverse coll)))
(last %)))))))
(into []
(map
#(into
[]
(conj
[]
(first %)
(into
[]
(map
(fn [coll]
(into
[]
(partition 2 coll)))
(last %)))))))
(into []
(map
#(into
[]
(conj
[]
(first %)
(into
[]
(map
(fn [coll]
(reduce
(fn [a b]
(assoc a (keyword (first b)) (Integer/parseInt (last b))))
{}
coll))
(last %)))))))))
(defn puzzle-1
"Puzzle 1 solver"
[data-file]
(let [prepared-data (acquire-and-process-data data-file)]
(reduce
(fn
[acc coll]
(+ acc (first coll)))
0
(get-valid-games prepared-data))))
(defn puzzle-2
"Puzzle 2 solver"
[data-file]
(let [prepared-data (acquire-and-process-data data-file)]
(get-total-game-power prepared-data)))
(defn -main
"Command Line entrypoint"
[& args]
(println (puzzle-1 "input.txt"))
(println (puzzle-2 "input.txt")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment