Created
December 4, 2021 20:17
-
-
Save Solaxun/bf10e00a371648cef1feda096859e6e1 to your computer and use it in GitHub Desktop.
AoC 2021: Clojure Day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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