-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday07.clj
50 lines (38 loc) · 1.37 KB
/
day07.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
(ns day07
(:require aoc
[clojure.string :as str]
[clojure.core.match :refer [match]]))
(defn change-cards-representation [hand jokers?]
(let [replacements {"T" "A"
"J" (if jokers? "0" "B")
"Q" "C"
"K" "D"
"A" "E"}]
(str/replace hand #"T|J|Q|K|A" replacements)))
(defn parse-hand [jokers? [cards bid]]
(let [cards' (change-cards-representation cards jokers?)
jokers (aoc/count-if #{\0} cards')
card-counts (->> (str/replace cards' "0" "")
frequencies
vals
(sort >)
vec)
hand-type (match card-counts
[] [jokers 0]
[x] [(+ jokers x) 0]
[x y & _] [(+ jokers x) y])]
[hand-type cards' (parse-long bid)]))
(defn calc-score [hands]
(aoc/sum-map (fn [[rank [_ _ bid]]] (* rank bid))
(map-indexed vector hands)))
(defn total-winnings [hands jokers?]
(->> hands
(map #(parse-hand jokers? %))
sort
(into [[0 "" 0]]) ; to have ranks start at 1
calc-score))
(defn solve [input]
(let [hands (aoc/parse-input input :words)]
[(total-winnings hands false)
(total-winnings hands true)]))
(solve (aoc/read-file 7))