aboutsummaryrefslogtreecommitdiffstats
path: root/day4/part1.clj
blob: b9fccebf7fa1564d95e8ee5125a01ef55c7e569a (plain)
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
(require '[clojure.string :as str])

(def calls
  (->> (read-line)
       (#(str/split % #","))
       (map #(Integer/parseInt %))
       )
  )

(defn read-board []
  (when (some? (read-line))
    (mapv
        (fn [line] (->> line
            (str/join)
            (str/trim)
            (#(str/split % #"\s+"))
            (map #(Integer/parseInt %))
            )
          )
        (repeatedly 5 read-line)
        )
    )
  )

(defn bingo? [board]
  (some?
    (some
      #(every? nil? %)
      (concat board (apply mapv vector board))
      )
    )
  )

(defn bingo-mark [board n]
  (mapv (partial replace {n nil}) board)
  )

(defn get-bingo [init-board]
  (loop [board init-board nums calls turns 1]
    (let [new-board (bingo-mark board (first nums))]
      (if (bingo? new-board)
        [new-board (first nums) turns]
        (recur new-board (rest nums) (inc turns))
        )
      )
    )
  )

(loop [best [999 0] board (read-board)]
  (if (nil? board)
    (println best)
    (let [bingo (get-bingo board)]
      (recur
        (if (< (get bingo 2) (first best))
          [(get bingo 2)
           (->> (first bingo)
                (flatten)
                (filter some?)
                (apply +)
                (* (second bingo))
                )
           ]
          best
          )
        (read-board)
        )
      )
    )
  )