-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday22.clj
73 lines (61 loc) · 2.08 KB
/
day22.clj
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(ns adventofcode2017.day22
(:require [clojure.java.io :as io]
[clojure.string :as str]))
(def input (str/split-lines (slurp (io/resource "2017/day22"))))
(def len (count input))
(def data (into {} (for [x (range len)
y (range len)]
[[x (- y)] (case (get-in input [y x])
\# :infected
:clean)])))
(def center [(quot len 2) (- (quot len 2))])
(defn move [[x y] direction turn]
(case [direction turn]
([:north :left] [:south :right] [:west nil] [:east :back])
[[(dec x) y] :west]
([:north :right] [:south :left] [:east nil] [:west :back])
[[(inc x) y] :east]
([:east :left] [:west :right] [:north nil] [:south :back])
[[x (inc y)] :north]
([:east :right] [:west :left] [:south nil] [:north :back])
[[x (dec y)] :south]))
;; part 1
(loop [[curr-node direction] [center :north]
nodes data
bursts 0
infections 0]
(if (= 10000 bursts)
infections
(case (get nodes curr-node)
:infected
(recur (move curr-node direction :right)
(assoc nodes curr-node :clean)
(inc bursts) infections)
(recur (move curr-node direction :left)
(assoc nodes curr-node :infected)
(inc bursts) (inc infections)))))
; 5406
;; part 2
(loop [[curr-node direction] [center :north]
nodes data
bursts 0
infections 0]
(if (= 10000000 bursts)
infections
(case (get nodes curr-node)
:weakened
(recur (move curr-node direction nil)
(assoc nodes curr-node :infected)
(inc bursts) (inc infections))
:infected
(recur (move curr-node direction :right)
(assoc nodes curr-node :flagged)
(inc bursts) infections)
:flagged
(recur (move curr-node direction :back)
(assoc nodes curr-node :clean)
(inc bursts) infections)
(recur (move curr-node direction :left)
(assoc nodes curr-node :weakened)
(inc bursts) infections))))
; 2511640