Commit 55854a4 1 parent 92abe83 commit 55854a4 Copy full SHA for 55854a4
File tree 13 files changed +123
-123
lines changed
13 files changed +123
-123
lines changed Original file line number Diff line number Diff line change 65
65
66
66
67
67
(defn part-1 [seeds maps]
68
- (->> (reduce convert-1 seeds maps)
69
- (reduce min)))
68
+ (reduce min (reduce convert-1 seeds maps)))
70
69
71
70
(defn part-2 [seeds maps]
72
71
(->> (reduce convert-2 seeds maps)
76
75
77
76
(defn solve [input]
78
77
(let [[[seeds] & maps] (aoc/parse-input-paragraphs input)
79
- seeds-1 (aoc/integers seeds)
80
- seeds-2 (seed-ranges seeds-1)
81
- rules (map parse-maps maps)]
78
+ seeds-1 (aoc/integers seeds)
79
+ seeds-2 (seed-ranges seeds-1)
80
+ rules (map parse-maps maps)]
82
81
[(part-1 seeds-1 rules)
83
82
(part-2 seeds-2 rules)]))
84
83
Original file line number Diff line number Diff line change 14
14
15
15
16
16
(defn parse-hand [jokers? [cards bid]]
17
- (let [cards' (change-cards-representation cards jokers?)
18
- jokers (aoc/count-if #{\0 } cards')
17
+ (let [cards' (change-cards-representation cards jokers?)
18
+ jokers (aoc/count-if #{\0 } cards')
19
19
card-counts (->> (str/replace cards' " 0" " " )
20
20
frequencies
21
21
vals
22
22
(sort >)
23
23
vec)
24
- hand-type (match card-counts
25
- [] [jokers 0 ]
26
- [x] [(+ jokers x) 0 ]
27
- [x y & _] [(+ jokers x) y])]
24
+ hand-type (match card-counts
25
+ [] [jokers 0 ]
26
+ [x] [(+ jokers x) 0 ]
27
+ [x y & _] [(+ jokers x) y])]
28
28
[hand-type cards' (parse-long bid)]))
29
29
30
30
38
38
39
39
(defn total-winnings [hands jokers?]
40
40
(->> hands
41
- (map ( partial parse-hand jokers?))
41
+ (map #( parse-hand jokers? % ))
42
42
sort
43
43
(into [[0 " " 0 ]]) ; to have ranks start at 1
44
44
calc-score))
Original file line number Diff line number Diff line change 21
21
22
22
(defn ghost-steps [instrs network starts]
23
23
(transduce
24
- (map ( partial steps instrs network))
24
+ (map #( steps instrs network % ))
25
25
aoc/lcm
26
26
starts))
27
27
28
28
29
29
(defn solve [input]
30
30
(let [[[instrs'] network'] (aoc/parse-input-paragraphs input)
31
31
network (parse-network network')
32
- instrs (mapv (comp keyword str) instrs')
33
- starts (filter #(str/ends-with? % " A" ) (keys network))]
32
+ instrs (mapv (comp keyword str) instrs')
33
+ starts (filter #(str/ends-with? % " A" ) (keys network))]
34
34
[(steps instrs network :AAA )
35
35
(ghost-steps instrs network starts)]))
36
36
Original file line number Diff line number Diff line change 8
8
9
9
(defn traverse [sketch start]
10
10
(loop [[x y :as curr] (aoc/pt+ start [0 1 ])
11
- [px py] start
12
- seen (transient #{})
11
+ [px py] start
12
+ seen (transient #{})
13
13
verticals (transient {})]
14
14
(let [seen' (conj! seen curr)
15
15
dx (- x px)
32
32
curr seen' verticals)))))
33
33
34
34
(defn enclosed [seen verticals h w]
35
- (for [y (range h)
36
- :let [row-verts (verticals y)]
35
+ (for [y (range h)
36
+ :let [row-verts (verticals y)]
37
37
:when row-verts
38
- :let [min-vert (reduce min row-verts)
38
+ :let [min-vert (reduce min row-verts)
39
39
max-vert (reduce max row-verts)]
40
- x (range w)
40
+ x (range w)
41
41
:when (and (< min-vert x max-vert)
42
42
(not (seen [x y]))
43
43
(odd? (aoc/count-if #(< % x) row-verts)))]
44
44
1 ))
45
45
46
46
(defn solve [input]
47
47
(let [sketch (aoc/parse-input input :chars )
48
- start (find-start sketch)
49
- h (count sketch)
50
- w (count (first sketch))
48
+ start (find-start sketch)
49
+ h (count sketch)
50
+ w (count (first sketch))
51
51
{:keys [pipes verts]} (traverse sketch start)]
52
52
[(/ (count pipes) 2 )
53
53
(count (enclosed pipes verts h w))]))
Original file line number Diff line number Diff line change 7
7
8
8
(defn find-galaxies [lines]
9
9
(for [[y line] (map-indexed vector lines)
10
- [x chr] (map-indexed vector line)
11
- :when (= chr \#)]
10
+ [x chr] (map-indexed vector line)
11
+ :when (= chr \#)]
12
12
[x y]))
13
13
14
14
25
25
(-> acc
26
26
(update :sum + (* coeff coord))
27
27
(update :coeff + 2 )))
28
- {:sum 0
28
+ {:sum 0
29
29
:coeff (- 1 (count galaxies))})
30
30
:sum ))
31
31
35
35
(reduce
36
36
(fn [acc coord]
37
37
(let [before (count (take-while #(< % coord) galaxies))
38
- after (- total before)]
38
+ after (- total before)]
39
39
(+ acc (* before after))))
40
40
0
41
41
empties)))
Original file line number Diff line number Diff line change 28
28
(if (empty? groups)
29
29
(if (every? is-operational? pattern) 1 0 )
30
30
(let [[size & tl] groups
31
- post (+ (reduce + tl) (count tl))
32
- score (atom 0 )]
33
- (doseq [pre (range (inc (- (count pattern) post size)))
34
- :let [[before pattern'] (split-at pre pattern)
35
- [current remaining] (split-at size pattern')]
31
+ post (+ (reduce + tl) (count tl))
32
+ score (atom 0 )]
33
+ (doseq [pre (range (inc (- (count pattern) post size)))
34
+ :let [[before pattern'] (split-at pre pattern)
35
+ [current remaining] (split-at size pattern')]
36
36
:while (every? is-operational? before)
37
- :when (every? is-damaged? current)]
37
+ :when (every? is-damaged? current)]
38
38
(cond
39
39
(empty? tl) (when (every? is-operational? remaining)
40
40
(swap! score inc))
45
45
46
46
47
47
(defn solve [input]
48
- (let [lines (aoc/parse-input input parse-line)
48
+ (let [lines (aoc/parse-input input parse-line)
49
49
unfolded (map unfold lines)]
50
50
[(reduce + (map arrangements lines))
51
51
(reduce + (pmap arrangements unfolded))]))
Original file line number Diff line number Diff line change 8
8
9
9
(defn mirror? [part pattern nrettap line]
10
10
(let [before (take-last line nrettap)
11
- after (drop line pattern)
12
- diffs (map differences before after)]
11
+ after (drop line pattern)
12
+ diffs (map differences before after)]
13
13
(case part
14
14
1 (every? zero? diffs)
15
15
2 (= 1 (reduce + diffs)))))
16
16
17
17
18
18
(defn mirror-line [part pattern]
19
19
(aoc/find-first
20
- ( partial mirror? part pattern (rseq pattern))
20
+ #( mirror? part pattern (rseq pattern) % )
21
21
(range 1 (count pattern))))
22
22
23
23
28
28
29
29
30
30
(defn notes-sum [patterns part]
31
- (reduce + (pmap ( partial find-mirror part) patterns)))
31
+ (reduce + (pmap #( find-mirror part % ) patterns)))
32
32
33
33
34
34
(defn solve [input]
Original file line number Diff line number Diff line change 13
13
(->> (str/split line #"#" -1 )
14
14
(map (comp str/join
15
15
(case dir
16
- :left reverse
16
+ :left reverse
17
17
:right identity)
18
18
sort))
19
19
(str/join " #" )))
20
20
21
21
22
22
(defn move-east [platform]
23
- (pmap ( partial move-line :right ) platform))
23
+ (pmap #( move-line :right % ) platform))
24
24
25
25
(defn move-west [platform]
26
- (pmap ( partial move-line :left ) platform))
26
+ (pmap #( move-line :left % ) platform))
27
27
28
28
(defn move-north [platform]
29
29
(-> platform rotate move-west rotate))
37
37
38
38
(defn calc-score [platform]
39
39
(let [platform (vec platform)
40
- size (count platform)]
40
+ size (count platform)]
41
41
(reduce
42
42
(fn [acc n]
43
43
(+ acc (* (- size n)
47
47
48
48
49
49
(defn remaining-shakes [platform prev curr]
50
- (let [r (- 1000000000 prev)
51
- to-spin (mod r (- curr prev))]
50
+ (let [remain (- 1000000000 prev)
51
+ to-spin (mod remain (- curr prev))]
52
52
(nth (iterate spin-cycle platform) to-spin)))
53
53
54
54
Original file line number Diff line number Diff line change 37
37
38
38
39
39
(defn focusing-power [boxes]
40
- (reduce + (for [[i box] (map-indexed vector boxes)
40
+ (reduce + (for [[i box] (map-indexed vector boxes)
41
41
[j [_ focal]] (map-indexed vector box)]
42
42
(* (inc i) (inc j) focal))))
43
43
44
44
45
45
(defn solve [input]
46
- (let [steps (aoc/parse-input-line input :words #"," )
46
+ (let [steps (aoc/parse-input-line input :words #"," )
47
47
instructions (map parse-instruction steps)]
48
48
[(reduce + (map word-hash steps))
49
49
(focusing-power (hashmap instructions))]))
Original file line number Diff line number Diff line change 11
11
12
12
13
13
(defn traverse [city min-straight max-straight]
14
- (let [size (count city)
15
- end (dec size)
14
+ (let [size (count city)
15
+ end (dec size)
16
16
queue (priority-map [0 0 1 0 ] 0
17
17
[0 0 0 1 ] 0 )]
18
- (loop [seen #{}
18
+ (loop [seen #{}
19
19
queue queue]
20
20
(let [[[x y dx dy :as state] heat] (peek queue)
21
- queue' (pop queue)]
22
- (if (= end x y)
23
- heat
24
- (if (seen state)
25
- (recur seen queue')
26
- (recur
27
- (conj seen state)
28
- (reduce
29
- (fn [q [dx' dy']]
30
- (reduce
31
- (fn [q n]
32
- (let [nx (+ x (* n dx'))
33
- ny (+ y (* n dy'))]
34
- (if-not (aoc/inside? size nx ny)
35
- (reduced q)
36
- (let [heat' (+ heat (heat-loss city x y dx' dy' n))
37
- state' [nx ny dx' dy']]
38
- (assoc q state' (min heat' (q state' Integer/MAX_VALUE)))))))
39
- q
40
- (range min-straight (inc max-straight))))
41
- queue'
42
- [[(- dy) dx]
43
- [dy (- dx)]]))))))))
21
+ queue' (pop queue)]
22
+ (cond
23
+ (= end x y) heat
24
+ (seen state) (recur seen queue')
25
+ :else (recur
26
+ (conj seen state)
27
+ (reduce
28
+ (fn [q [dx' dy']]
29
+ (reduce (fn [q n]
30
+ (let [nx (+ x (* n dx'))
31
+ ny (+ y (* n dy'))]
32
+ (if-not (aoc/inside? size nx ny)
33
+ (reduced q)
34
+ (let [heat' (+ heat (heat-loss city x y dx' dy' n))
35
+ state' [nx ny dx' dy']]
36
+ (assoc q state' (min heat' (q state' Integer/MAX_VALUE)))))))
37
+ q
38
+ (range min-straight (inc max-straight))))
39
+ queue'
40
+ [[(- dy) dx]
41
+ [dy (- dx)]])))))))
44
42
45
43
46
44
(defn solve [input]
Original file line number Diff line number Diff line change 8
8
9
9
10
10
(defn parse-line [line]
11
- (let [[d n c] (str/split line #" " )
12
- c (str/replace c #"[(#)]" " " )
11
+ (let [[d n c] (str/split line #" " )
12
+ c (str/replace c #"[(#)]" " " )
13
13
[dist dir] (map str/join (split-at 5 c))
14
- dist (Integer/parseInt dist 16 )]
14
+ dist (Integer/parseInt dist 16 )]
15
15
{:p1 {:dir (deltas d)
16
16
:dist (parse-long n)}
17
17
:p2 {:dir (deltas dir)
18
18
:dist dist}}))
19
19
20
20
21
21
(defn dig-trench [input]
22
- ; Shoelace formula + Pick's theorem
22
+ ; ; Shoelace formula + Pick's theorem
23
23
(->> input
24
24
(reduce (fn [{:keys [total x]}
25
25
{:keys [dir dist]}]
26
26
(let [[dx dy] dir
27
- nx (+ x (* dist dx))
28
- area (* nx dist dy)]
29
- {:x nx
27
+ nx (+ x (* dist dx))
28
+ area (* nx dist dy)]
29
+ {:x nx
30
30
:total (+ total area (/ dist 2 ))}))
31
31
{:total 1
32
- :x 0 })
32
+ :x 0 })
33
33
:total
34
34
long))
35
35
Original file line number Diff line number Diff line change 72
72
workflows (into {} (map parse-workflow wrkfls))
73
73
ratings (map parse-rating rtngs)
74
74
rating-2 (zipmap [:x :m :a :s ] (repeat [1 4000 ]))]
75
- [(reduce + (map ( partial accepted workflows) ratings))
75
+ [(reduce + (map #( accepted workflows % ) ratings))
76
76
(accepted-combinations workflows rating-2)]))
77
77
78
78
You can’t perform that action at this time.
0 commit comments