day 9 WIP

This commit is contained in:
Aleh Suprunovich 2024-12-11 23:01:02 +03:00
parent 5e61eb43fc
commit 3953c4bbca
4 changed files with 74 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"}}}

1
src/day10.clj Normal file
View File

@ -0,0 +1 @@
(ns day10)

66
src/day8.clj Normal file
View File

@ -0,0 +1,66 @@
(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....
..........
..........
..........
..........")
(def input
(->
test-city
;;(get-input 8)
mapped-area/read-mapped-area))
(defn antinodes [a [[x1 y1] [x2 y2]]]
(let [xd (abs (- x1 x2))
yd (abs (- y1 y2))
a1 [(+ (max x1 x2) xd)
(+ (max y1 y2) yd)]
a2 [(- (min x1 x2) xd)
(- (min y1 y2) yd)]]
(filter (partial mapped-area/mapped? a) [a1 a2])))
(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]
(let [a (mapped-area/read-mapped-area m)]
(->> (antenna-pairs a)
(mapcat #(antinodes a (vec %))))))
(def r (find-antinodes test-city2))
(println r)
(println (set r))

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]))))