Skip to content

Instantly share code, notes, and snippets.

@Solaxun
Created December 4, 2021 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Solaxun/bf10e00a371648cef1feda096859e6e1 to your computer and use it in GitHub Desktop.
Save Solaxun/bf10e00a371648cef1feda096859e6e1 to your computer and use it in GitHub Desktop.
AoC 2021: Clojure Day 2
(ns aoc-clj-2021.day2
(:require [clojure.string :as str]
[clojure.set :as set]
[clojure.java.io :as io]))
(def data
(->> (io/resource "day2.txt")
slurp
str/split-lines
(map #(str/split % #" "))
(map (fn [[dir amt]] [dir (parse-long amt)]))))
(let [{:strs [forward up down]}
(->> (group-by first data)
(map (fn [[dir amts]] [dir (reduce + (map second amts))]))
(into {}))]
(* forward (- down up)))
;; part 2
(let [{:keys [aim horizontal depth]}
(reduce (fn [{:keys [aim horizontal depth] :as state} [dir amt]]
(case dir
"forward" (-> (update state :horizontal + amt)
(update :depth + (* aim amt)))
"up" (update state :aim - amt)
"down" (update state :aim + amt)))
{:aim 0 :horizontal 0 :depth 0}
data)]
(* depth horizontal))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment