Compare commits

..

12 Commits

Author SHA1 Message Date
Aleh Suprunovich 334a2012e3 cleanup dead wip code 2024-12-10 23:32:17 +03:00
Aleh Suprunovich 3930558de3 day 9 2024-12-10 11:41:41 +03:00
br 09df1d724c add helper module to work with mapped areas 2024-12-08 21:54:54 +03:00
Aleh Suprunovich ff7fc15880 day 7 2024-12-08 15:01:54 +03:00
Aleh Suprunovich 76a18ab740 day 6 2024-12-06 14:21:44 +03:00
br f3fd8a8ff5 day 5 2024-12-06 11:56:11 +03:00
Aleh Suprunovich e0509be163 day 4 2024-12-04 16:02:23 +03:00
Aleh Suprunovich eb82538b78 get inputs from directly from site 2024-12-04 14:12:40 +03:00
br 3904efae96 day 3 2024-12-03 10:41:13 +03:00
br a7b6626cae day 2 2024-12-03 00:27:50 +03:00
Aleh Suprunovich 17a3b8befb add readme 2024-12-01 12:48:19 +03:00
Aleh Suprunovich b15a212927 day 1 2024-12-01 12:45:47 +03:00
5 changed files with 1 additions and 215 deletions
+1 -2
View File
@@ -1,4 +1,3 @@
{:paths ["src" "cookies"]
:deps {org.clojure/clojure {:mvn/version "1.12.0"}
clj-http/clj-http {:mvn/version "3.13.0"}
org.clojure/math.combinatorics {:mvn/version "0.3.0"}}}
clj-http/clj-http {:mvn/version "3.13.0"}}}
-66
View File
@@ -1,66 +0,0 @@
(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
View File
@@ -1,41 +0,0 @@
(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
View File
@@ -1,80 +0,0 @@
(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))
-26
View File
@@ -67,33 +67,7 @@ ijkl")
(< x (w 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 #'h)
(test #'w)
(test #'mapped?)
(test #'parse-elements)