day 8
This commit is contained in:
parent
5e61eb43fc
commit
93266bfca3
3
deps.edn
3
deps.edn
@ -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"}}}
|
||||||
|
|||||||
80
src/day8.clj
Normal file
80
src/day8.clj
Normal 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))
|
||||||
@ -71,3 +71,8 @@ ijkl")
|
|||||||
(test #'h)
|
(test #'h)
|
||||||
(test #'w)
|
(test #'w)
|
||||||
(test #'mapped?)
|
(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]))))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user