From d40e6cfcd8f4fac971ce844a594eea2dd8fecf4b Mon Sep 17 00:00:00 2001 From: Adam Date: Fri, 6 Dec 2024 09:33:17 -0500 Subject: [PATCH] init day 6 --- 2024/input/6.txt | 130 +++++++++++++++++++++++++++++++++++++++++++++ 2024/src/day06.clj | 66 +++++++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 2024/input/6.txt create mode 100644 2024/src/day06.clj diff --git a/2024/input/6.txt b/2024/input/6.txt new file mode 100644 index 0000000..1f20b72 --- /dev/null +++ b/2024/input/6.txt @@ -0,0 +1,130 @@ +..........#.............................#...............#...........#....................#...........#............................ +..............................#.......#...#.....#.....#...............#.#..#.......................#............#................. +.............#...........#.#........#............#..........................#.........#.......#......#............................ +......................................................#....#..#.............#................................#...#............##.. +...............#.....#....................................#......................................................#................ +..................##............#......#...............................................................#...................#...... +.#......#...........................................................#............................#................................ +#..#.............#...............................#.....#....#.............................#....#..............................#... +#...##..........#.........................#.................................................#............#...............#........ +............#...............#..............................................#.................................#..........##........ +..............#...................#......##...#.....#....................#.......................................................# +.#.....#....#......................#...#........................................#.#.............#.........#....................... +#..#...............#........##.........#.....#..........#.....##.........................#.....#.#................................ +...............#...............................#.....#.................................................#..##...................... +......#...................#..........#...........................................#....#........................................... +...............................................#...#....##.................................................#.....#................ +.................#.........#..............................#........#...#...#...................................#.................. +...........#..#.....#...#.........................#..........#.#.............#....#...................##.......................... +.........................................#.........................#.#............#...#...##............................#......... +.....#......................................#....................................................#..............#................. +..................................................#..................#................#.............#.#..#..................#..... +............................................#.....#......................#........#..........................................#.... +.......#..........................#...#...................................#.........................#...#..........#......#....... +............#...........##........................................................................#......................#........ +#.#...............................................#........#..........#.#..#...#................................#................. +..#.........................#.#........................#....##..................................................#............#.... +.....................................................................................#...............#...........#..........#..... +....................#..........#................................#...............................................#................. +#..............#.#...........#.........#..........#..........................#......................##........#.........#......... +....#....#...............................................................................#...#...........#............#...#...#... +.......................................................##......................#...#................................#............. +.......#...........##...........................#..............#..............................#...................#......#........ +......#.........#..............................................................#........#..#..........#.......#..#...#.....#....#. +....#.............................................#........#....#..................................#.............................. +..........#......#...........#....................................#..............#.......#...#............#....................... +...........................................................................................#.......#.............................. +.#..................................................#.............................#..................#...................#........ +......#..#.#................#.....................#..................................#.............#...............#.............. +...............................#..................#........#...................................................................... +......................#..................................................................................#.........#.............. +...............................................................................................................................#.. +.......#..............#...........................................................#......................#.......................# +#...............................#..............................#.......#..............................#...#...#........#.......... +............................#....#...#.........#....#................................................................#............ +.....................#.................................................................#..#....#..................#............... +......................#.............#.............................#...................................................#........... +....#.................#.#.........##................#.....#....................................................#.............#.... +...............................#...#...................#.......................................#......#........................... +....#............................................................................................................................. +...#.......................................................................................................#............#.......#. +..#.............##.............................................................#...#............................#............#.... +......................#.......#............#........................................................#..................#......#... +...................#.............#..................#..............................#............#........#...................#.... +....................#...........#.........................................................#......#.......#...........#.....##..... +...........#.........................................#.............................#..............#..................#............ +......................#.........#...................#......#...........................................#.......................... +..#....#...#.............................#..............#.....................#............#...................................#.. +.....#.............................#....#..................................#...................#...#.............#................ +...................#...#...........#........................#.............#........................#...#.............#..........#. +...#...........#......#.......#..............#......#..........#.....#............................................................ +..#......................................................#......#...........................#...........#......#..#............... +........#............................#.#....................#...#...#......#.....#...............................................# +.............#....#........#.........................................................................................#.........#.. +....................................................................................................#............................. +..#........................#....#............................................................................#.................... +.......................................#.....#.#.......................................#......#........#.#...................#.... +.................#........#....................................#...#..#................................#.......................... +...........#.................................................................................................#.................... +......................#..................#...#.............#................##................#.......#...........#.........#..... +............#..#................#.............................................#............##..................##..........#...... +......#.#.......................#............#...........................................................#........................ +#......................................#.#......^...............................................#................................. +........#...........................................................#...............#............................................. +...#.............................................#.................#...............#.............................#...#.#.......... +....................#........#...........##.......................#.................#..................#................#......... +#................................................................................................................................. +............#.......................................................................................#.#.........#..............#.. +...............#....#............#..................................................#......##.................................#... +.............................#.........#...............#......#.......#.................#........................#........#....... +......................................................#........................................................#.................# +.................#.#...............#......................................................................................#....#.. +........#.............#......................#...........#...........#......................................#..............#...... +......................#......................#...........#.....#.....##............................................#....#......... +.............#................................................................................#..........#.....#.................. +............##...........................#.................................#..#.#....................................#............ +..............................................................................................#............................#...... +...#.............................#..#..#....................................#..............#....#................................. +.....#..........................................................#.....#......#.........#.......................................... +....##......#.....................#..........#..............#..................#.#..............#........#...........#....#....... +.....#...#.........#............#............#.............................................................#...................... +.........#...................................#...........#.....................................##.....#............#.............. +.....##.#............##.....................#..........................#.................................#........#............... +.......................#..............##...#...................................................#.........#........................ +..#........#.......#...#..##............#....................................................................#...........#.....#.. +..#.....................................................................#..............#.............#...................#........ +...........#..........#..........#.....#.................................................................#..#..................... +..........#................#........................................................##.#...............................#.#......#. +#............................................#...........................##...#.......................#.............#............. +..#............#........................................................................................................#......... +....................................................#....................#............#...............#..........................# +........................................................................#.......#.....................................#.#......... +................#..#......................#...........................................#........................................... +........................##............................................#.............##..........................#.......#....#.... +.........#.......#..............................#......#....#............#..................#...#................................. +#.........#............................#....#.#..#.........................................................##..................... +.#..##...............#....#.............................#...............#........#.......................................#......#. +...#............................................................#.....................#.................#.....#................... +...#...........#..#.............................#.....#.............................................................#............. +...#.........................#..............................................................................................##.... +.........................#...........................#.......................#...........#........................................ +.#..................................#............................................................................................. +....................................................................#...........##.....#.#.........#.....#........................ +................#...............................#........#........................................................................ +......#....#.................................#...................................#.#.........#.....#....................#......... +................#..............................#.......................#...#.....#.......#................#....................... +....................#......................................#..#...........#.#.................#...#.......................#....... +.............#..#...#............................#.#...........................................#.......................#.......... +.........#..........#.................#....................................#.#..........................................#......... +....#.............#......................................................#...........#...........................#................ +........#.#............................#...........................................#.................#..............#..........#.. +..............#..........................#............#.........#....#...#.....#.........................#....#...............##.. +...................#.........................................................#.......#........#..............#..........#......... +.#........#....#..................................#..#.#...............#............#...........#................................. +...............................................................................................#.....#....#.............#......... +...........#..........#...........................................#.........#..............#................#..........#.......... +............#.................................###..............#.#...............................................................# +......#...................................................................................#............#...#..........#.....#..... +.............#..................#...................#......#.........#..#.......................#....#................#..........# +..#....##................................#.................#...........................#.......#....#...#..........#.............. +...............................#......................................#.........#.............#......................#..........#. diff --git a/2024/src/day06.clj b/2024/src/day06.clj new file mode 100644 index 0000000..3204937 --- /dev/null +++ b/2024/src/day06.clj @@ -0,0 +1,66 @@ +(ns day06 + (:require [input-manager :refer [get-input]] + [core :as c])) + +(def input (mapv vec (get-input 6))) + +(defn get-char [[row col]] + (get (get input row) col)) + +(def start (->> (c/get-coords input) + (filter #(= \^ (get-char %))) + first)) + +(def num-cols (count (first input))) +(def num-rows (count input)) + +(defn move [dir [row col]] + (condp = dir + :up [(dec row) col] + :down [(inc row) col] + :left [row (dec col)] + :right [row (inc col)])) + +(defn turn-right [dir] + (condp = dir + :up :right + :right :down + :down :left + :left :up)) + +(defn in-bounds? [[row col]] + (and (>= row 0) (>= col 0) (< row num-rows) (< col num-cols))) + +(defn get-path [] + (loop [pos start + dir :up + path #{}] + (if (not (in-bounds? pos)) + path + (if (path [dir pos]) + :cycle + (let [pos' (move dir pos)] + (if (= (get-char pos') \#) + (recur pos (turn-right dir) (conj path [dir pos])) + (recur pos' + dir + (conj path [dir pos])))))))) + +(defn add-obstacle [[row col]] + (update input row assoc col \#)) + +;; part 1 +(->> (get-path) + (map second) + distinct + count) + +;; part 2 +(->> (for [pos (->> (get-path) + (map second) + distinct + (filter #(not= % start)))] + (with-redefs [input (add-obstacle pos)] (get-path))) + (filter #(= % :cycle)) + count) +