Compare commits

..

4 Commits

Author SHA1 Message Date
bdd2a03230 fixup! day 10 WIP 2024-12-12 21:27:08 +03:00
8695b8b375 day 11 part 1 2024-12-12 21:26:56 +03:00
5ef8f4af1d day 10 WIP 2024-12-12 00:26:53 +03:00
93266bfca3 day 8 2024-12-11 23:41:39 +03:00
5 changed files with 215 additions and 1 deletions

View File

@@ -1,3 +1,4 @@
{:paths ["src" "cookies"] {:paths ["src" "cookies"]
:deps {org.clojure/clojure {:mvn/version "1.12.0"} :deps {org.clojure/clojure {:mvn/version "1.12.0"}
clj-http/clj-http {:mvn/version "3.13.0"}}} clj-http/clj-http {:mvn/version "3.13.0"}
org.clojure/math.combinatorics {:mvn/version "0.3.0"}}}

66
src/day10.clj Normal file
View File

@@ -0,0 +1,66 @@
(ns day10
(:require [mapped-area]
[helpers :refer [get-input]]))
(def test-map
"89010123
78121874
87430965
96549874
45678903
32019012
01329801
10456732")
;(def test-map
;"0123
;1234
;8765
;9876")
(def input
(->
test-map
mapped-area/read-mapped-area
mapped-area/parse-elements))
(defn trailheads [a]
(reduce
(fn [coll [coord value]]
(if (= value 0)
(conj coll coord)
coll))
[]
(mapped-area/mapped-area-indexed-seq a)))
(defn step [a [x y]]
(let [c (get-in a [x y])
even-gradual-uphill?
(fn [[x' y']]
(let [c' (get-in a [x' y'])]
(= 1 (- c' c))))]
(->>
[[(inc x) y] [(dec x) y]
[x (inc y)] [x (dec y)]]
(filter (partial mapped-area/mapped? a))
(filter even-gradual-uphill?))))
(defn walk [a trailhead]
(loop [positions [trailhead]]
(println positions)
(if (== 9 (get-in a (first positions)))
(count positions)
(recur (mapcat (partial step a) (set positions))))))
(loop [i 0 positions (trailheads input)]
(println "iteration" i)
(doall
(map #(println % "-" (get-in input %)) positions))
(if (== i 9)
positions
(recur (inc i)
(mapcat (partial step input) positions))))
(trailheads input)
(mapcat (partial step input) (trailheads input))

41
src/day11.clj Normal file
View File

@@ -0,0 +1,41 @@
(ns day11
(:require [clojure.string :as string]
[helpers :refer [get-input]]))
(def stones
(string/split
;;"125 17"
(string/trim (get-input 11))
#" "))
(defn split-stone [stone]
(let [ss (split-at
(/ (count stone) 2)
stone)
seq->stone
(fn [s]
(str
(parse-long
(apply str s))))]
(mapv seq->stone ss)))
(defn blink [stone]
(cond
(= stone "0")
["1"]
(even? (count stone))
(split-stone stone)
:else
(vector
(str
(* 2024
(parse-long stone))))))
(time
(println "I blinked 25 times, there's"
(count
(loop [i 0 stones stones]
(if (= 25 i)
stones
(recur (inc i) (mapcat blink stones)))))
"stones!"))

80
src/day8.clj Normal file
View File

@@ -0,0 +1,80 @@
(ns day8
(:require [mapped-area]
[helpers :refer [get-input]]
[clojure.math.combinatorics :refer [combinations]]))
(def test-city
"............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............")
(def test-city2
"..........
..........
..........
....a.....
........a.
.....a....
..........
..........
..........
..........")
(defn antinodes [a [[x1 y1] [x2 y2]]]
(let [xd (- x1 x2)
yd (- y1 y2)
an1 [(+ x1 xd) (+ y1 yd)]
an2 [(- x2 xd) (- y2 yd)]]
(filter (partial mapped-area/mapped? a) [an1 an2])))
(defn antennas [a]
(reduce
(fn [m [coord value]]
(if (= value \.)
m
(update m value #(conj % coord))))
{}
(mapped-area/mapped-area-indexed-seq a)))
(defn antenna-pairs [a]
(mapcat (fn [[_ a]] (combinations a 2)) (antennas a)))
(defn find-antinodes [m a-fn]
(let [a (mapped-area/read-mapped-area m)]
(->> (antenna-pairs a)
(mapcat #(a-fn a (vec %))))))
(println "Number of antinodes:"
(->
(get-input 8)
(find-antinodes antinodes)
set
count))
(defn antinodes-resonant [a [[x1 y1] [x2 y2]]]
(let [xd (- x1 x2)
yd (- y1 y2)
nodes-fn
(fn [x y nodes op]
(if (not (mapped-area/mapped? a x y))
nodes
(recur (op x xd) (op y yd) (conj nodes [x y]) op)))]
(concat
(nodes-fn x1 y1 [] +)
(nodes-fn x2 y2 [] -))))
(println "Number of antinodes:"
(->
(get-input 8)
(find-antinodes antinodes-resonant)
set
count))

View File

@@ -67,7 +67,33 @@ ijkl")
(< x (w a)) (< x (w a))
(< y (h a))))) (< y (h a)))))
(defn mapped-area-indexed-seq [a]
(for [x (range (w a))
y (range (h a))]
(vector [x y] (get-in a [x y]))))
(def test-map-num
"1234
5678")
(defn parse-elements
{:test
#(do
(let [a (parse-elements
(read-mapped-area test-map-num))]
(assert (number? (get-in a [0 0])))
(assert (== 5 (get-in a [0 0])))
(assert (== 1 (get-in a [0 1])))))}
([a] (parse-elements a (comp parse-long str)))
([a parse-fn]
(reduce
(fn [coll [coord value]]
(assoc-in coll coord (parse-fn value)))
[[]]
(mapped-area-indexed-seq a))))
(test #'read-mapped-area) (test #'read-mapped-area)
(test #'h) (test #'h)
(test #'w) (test #'w)
(test #'mapped?) (test #'mapped?)
(test #'parse-elements)