This commit is contained in:
Aleh Suprunovich 2024-12-11 23:04:42 +03:00
parent 5e61eb43fc
commit 93266bfca3
3 changed files with 87 additions and 1 deletions

View File

@ -1,3 +1,4 @@
{:paths ["src" "cookies"]
: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"}}}

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

@ -71,3 +71,8 @@ ijkl")
(test #'h)
(test #'w)
(test #'mapped?)
(defn mapped-area-indexed-seq [a]
(for [x (range (w a))
y (range (h a))]
(vector [x y] (get-in a [x y]))))