-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday-22.clj
72 lines (61 loc) · 2 KB
/
day-22.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
(ns day-22
(:require [hashp.core]))
(def parse-int #(Integer/parseInt %))
(def queue (partial into clojure.lang.PersistentQueue/EMPTY))
(defn play1 [deck1 deck2]
(let [head1 (peek deck1)
head2 (peek deck2)
rest1 (pop deck1)
rest2 (pop deck2)]
(cond (empty? deck1) {:winner 2 :deck deck2}
(empty? deck2) {:winner 1 :deck deck1}
(> head1 head2) (recur (conj rest1 head1 head2) rest2)
(< head1 head2) (recur rest1 (conj rest2 head2 head1)))))
(defn play2 [seen deck1 deck2]
(let [new-seen (conj seen [deck1 deck2])
head1 (peek deck1)
head2 (peek deck2)
rest1 (pop deck1)
rest2 (pop deck2)]
(cond
; prevent infinite games
(= seen new-seen) {:winner 1 :deck deck1}
; player who has all the cards wins
(empty? deck1) {:winner 2 :deck deck2}
(empty? deck2) {:winner 1 :deck deck1}
; recursive game determines winner of round
(and (>= (count rest1) head1)
(>= (count rest2) head2))
(case
(:winner (play2 #{}
(queue (take head1 rest1))
(queue (take head2 rest2))))
1 (recur new-seen (conj rest1 head1 head2) rest2)
2 (recur new-seen rest1 (conj rest2 head2 head1)))
; not enough cards, higher card wins
(> head1 head2) (recur new-seen (conj rest1 head1 head2) rest2)
(< head1 head2) (recur new-seen rest1 (conj rest2 head2 head1)))))
(defn parse [in]
(->> (clojure.string/split-lines in)
(partition-by empty?)
(map rest)
(remove empty?)
(map #(map parse-int %))
(map queue)))
(defn puzzle1 [in]
(->> (parse in)
(apply play1)
(:deck)
(reverse)
(map-indexed #(* (inc %1) %2))
(apply +)))
(defn puzzle2 [in]
(->> (parse in)
(apply play2 #{})
(:deck)
(reverse)
(map-indexed #(* (inc %1) %2))
(apply +)))
(def input (->> (slurp "input22.txt")))
(time (puzzle1 input))
(time (puzzle2 input))