Last active
December 10, 2023 22:22
-
-
Save JpOnline/4a428a335f361881cd020d7ebc64e628 to your computer and use it in GitHub Desktop.
Advent of Code 2023 Day 10
This file contains hidden or 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
;; ---- Part 1 ---- | |
(def input-1 | |
{:board | |
"..... | |
.F-7. | |
.|.|. | |
.L-J. | |
....." | |
:current [1 1] | |
:prev [2 1] | |
:distance 0}) | |
(def input-2 | |
{:board (slurp "input-2") | |
:current [2 0] | |
:distance 0}) | |
(def up [-1 0]) | |
(def right [0 1]) | |
(def down [1 0]) | |
(def left [0 -1]) | |
(def move {"F" {down right | |
right down} | |
"-" {left right | |
right left} | |
"7" {left down | |
down left} | |
"|" {up down | |
down up} | |
"J" {up left | |
left up} | |
"L" {up right | |
right up}}) | |
(def side {"F" {right {:in-dir [left up]} | |
down {:out-dir [left up]}} | |
"-" {left {:in-dir [down] | |
:out-dir [up]} | |
right {:in-dir [up] | |
:out-dir [down]}} | |
"7" {left {:out-dir [up right]} | |
down {:in-dir [up right]}} | |
"|" {up {:in-dir [left] | |
:out-dir [right]} | |
down {:in-dir [right] | |
:out-dir [left]}} | |
"J" {up {:out-dir [right down]} | |
left {:in-dir [right down]}} | |
"L" {up {:in-dir [left down]} | |
right {:out-dir [left down]}}}) | |
(defn next-state [{:keys [board current prev] :as state}] | |
(let [current-pipe (-> board (clojure.string/split-lines) (get-in current) str) | |
coming-from (map - prev current) | |
going-to (get-in move [current-pipe coming-from]) | |
{:keys [in-dir out-dir]} (get-in side [current-pipe coming-from]) | |
inner-pos (map #(map + current %) in-dir) | |
outter-pos (map #(map + current %) out-dir) | |
next-pos (map + current going-to)] | |
(-> state | |
(assoc :current-pipe current-pipe) | |
(assoc :prev current) | |
(update :distance inc) | |
(assoc :current next-pos) | |
(update :path conj current) | |
(update :inner into inner-pos) | |
(update :outter into outter-pos)))) | |
(next-state input) | |
(nth (iterate next-state input) 2) | |
(defn find-greate-distance [state-dir-1 state-dir-2] | |
(if (= (:current state-dir-1) | |
(:current state-dir-2)) | |
(:distance state-dir-1) | |
(recur (next-state state-dir-1) | |
(next-state state-dir-2)))) | |
(find-greate-distance (next-state input) | |
(next-state (assoc input :prev [1 2]))) | |
(find-greate-distance (next-state (assoc input-2 :prev [2 1])) | |
(next-state (assoc input-2 :prev [3 0]))) | |
(def day-input | |
{:board (slurp "day-input") | |
:current [16 37] | |
:prev [16 36] | |
:distance 0 | |
:inner #{} | |
:outter #{} | |
:path #{} | |
:max-bottom 139 | |
:max-right 139}) | |
(find-greate-distance (next-state (assoc day-input :prev (map + (:current day-input) right))) | |
(next-state (assoc day-input :prev (map + (:current day-input) left)))) | |
;; debugging | |
(->> day-input | |
:current | |
(#(map + % right)) | |
(assoc day-input :prev) | |
(iterate next-state) | |
(#(nth % 1)) | |
(#(dissoc % :board))) | |
;; ---- Part 2 ---- | |
(def input-3 | |
{:board "........... | |
.F-------7. | |
.|F-----7|. | |
.||.....||. | |
.||.....||. | |
.|L-7.F-J|. | |
.|..|.|..|. | |
.L--J.L--J. | |
..........." | |
:current [1 1] | |
:prev [2 1] | |
:max-bottom 8 | |
:max-right 10 | |
:distance 0 | |
:inner #{} | |
:outter #{} | |
:path #{}}) | |
(def input-4 | |
{:board "FF7F7F7F7F7F7F7F---7 | |
L|LJ||||||||||||F--J | |
FL-7LJLJ||||||LJL-77 | |
F--JF--7||LJLJ7F7FJ- | |
L---JF-JLJ.||-FJLJJ7 | |
|F|F-JF---7F7-L7L|7| | |
|FFJF7L7F-JF7|JL---7 | |
7-L-JL7||F7|L7F-7F7| | |
L.L7LFJ|||||FJL7||LJ | |
L7JLJL-JLJLJL--JLJ.L" | |
:current [0 4] | |
:prev [0 3] | |
:max-bottom 9 | |
:max-right 19 | |
:distance 0 | |
:inner #{} | |
:outter #{} | |
:path #{}}) | |
;; debugging | |
(next-state input) | |
(->> input | |
(iterate next-state) | |
(#(nth % 3)) | |
(#(dissoc % :board))) | |
(defn take-tour [{:keys [current] :as state} initial-pos] | |
(if (= initial-pos current) | |
(next-state state) | |
(recur (next-state state) initial-pos))) | |
(defn expand-region [{:keys [path max-bottom max-right] :as state} to-process] | |
(let [expanded (reduce | |
#(conj %1 | |
(map + %2 up) | |
(map + %2 right) | |
(map + %2 down) | |
(map + %2 left)) | |
#{} to-process) | |
expanded+to-process (clojure.set/union expanded to-process) | |
minus-path (clojure.set/difference expanded+to-process path) | |
minus-boundaries (->> minus-path | |
(remove #(> (first %) max-bottom)) | |
(remove #(> (second %) max-right)) | |
(remove #(< (first %) 0)) | |
(remove #(< (second %) 0)))] | |
(if (= to-process minus-boundaries) | |
minus-boundaries | |
(recur state minus-boundaries)))) | |
(as-> input $ | |
(take-tour $ (:prev $)) | |
(expand-region $ (clojure.set/difference (:outter $) (:path $))) | |
(count $)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment