-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathday22.clj
More file actions
58 lines (47 loc) · 1.44 KB
/
Copy pathday22.clj
File metadata and controls
58 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(ns day22
(:require aoc
[clojure.string :as str]))
(defprotocol Cuboidal
(intersect [this other])
(is-init? [this])
(volume [this]))
(defrecord Cuboid [on? x1 x2 y1 y2 z1 z2]
Cuboidal
(volume [_]
(* (inc (- x2 x1))
(inc (- y2 y1))
(inc (- z2 z1))))
(is-init? [_]
(every? #(<= -50 % 50) [x1 x2 y1 y2 z1 z1]))
(intersect [_ other]
(let [nx1 (max x1 (:x1 other))
nx2 (min x2 (:x2 other))
ny1 (max y1 (:y1 other))
ny2 (min y2 (:y2 other))
nz1 (max z1 (:z1 other))
nz2 (min z2 (:z2 other))]
(when (and (<= nx1 nx2)
(<= ny1 ny2)
(<= nz1 nz2))
(->Cuboid true nx1 nx2 ny1 ny2 nz1 nz2)))))
(defn- parse-line [line]
(let [[onoff coords] (str/split line #" ")
coords (aoc/integers coords)]
(apply ->Cuboid (= "on" onoff) coords)))
(defn- calc [[head & tail :as cubes]]
(cond
(not (seq cubes)) 0
(not (:on? head)) (recur tail)
(nil? tail) (volume head)
:else (let [intersections (->> tail
(map #(intersect % head))
(filter some?))]
(- (+ (volume head) (calc tail))
(calc intersections)))))
(defn solve [filename]
(let [cubes (-> filename
aoc/read-input
(aoc/parse-lines parse-line))]
[(calc (filterv is-init? cubes))
(calc cubes)]))
(solve 22)