Last active
December 9, 2022 15:38
-
-
Save maacl/992c94a6000ae1ae206a926eccd34294 to your computer and use it in GitHub Desktop.
AoC 2022 - Day 9
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 aoc2022.day9 | |
(:require | |
[clojure.string :as str])) | |
(def pos {0 0 1 1 2 1 -1 -1 -2 -1}) | |
(def dirs {"U" [1 0] "R" [0 1] "D" [-1 0] "L" [0 -1]}) | |
(defn move [pos move] (mapv + pos move)) | |
(defn diff [pos1 pos2] (mapv - pos2 pos1)) | |
(defn touching? [d] (#{[0 1] [1 0] [1 1] [0 0]} (mapv abs d))) | |
(defn positions [f moves] (reductions f [0 0] moves)) | |
(defn moves->1-step-moves | |
[moves] | |
(mapcat (fn [[d n]] (repeat (parse-long n) (dirs d))) moves)) | |
(defn H-pos->T-pos | |
[T-pos H-pos] | |
(let [d (diff T-pos H-pos)] | |
(if (touching? d) | |
T-pos | |
(move T-pos (mapv pos d))))) | |
(def common | |
(->> | |
(slurp "resources/input9.txt") | |
str/split-lines | |
(map #(str/split % #" ")) | |
moves->1-step-moves | |
(positions move))) | |
(def end (comp count set)) | |
;; Part 1 | |
(->> common (positions H-pos->T-pos) end) | |
;; Part 2 | |
(->> common (iterate (partial positions H-pos->T-pos)) (take 10) last end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment