Skip to content

Instantly share code, notes, and snippets.

@stuartstein777
Created December 4, 2021 19:54
Show Gist options
  • Save stuartstein777/74765018745c8a500dbf4b2ad9405451 to your computer and use it in GitHub Desktop.
Save stuartstein777/74765018745c8a500dbf4b2ad9405451 to your computer and use it in GitHub Desktop.
AOC 2021 Day 4 Pat 2
(ns stuartstein777.2021.day4
(:require [clojure.string :as str]
[clojure.set :as set]
[stuartstein777.file :as f]
[stuartstein777.utils :as u]))
(defn winning-board? [board]
(or (row-matched? board)
(column-matched? board)))
(defn remove-all-winners [boards]
(remove winning-board? boards))
(defn row-matched? [board]
(->> board
(map :called)
(partition 5)
(map #(every? true? %))
(some #{true})))
(defn column-matched? [board]
(->> board
(map :called)
(partition 5)
(apply map vector)
(map #(every? true? %))
(some #{true})))
(defn score-board [board number]
(let [not-called (reduce + (map :n (filter #(false? (:called %)) board)))]
(* number not-called)))
(defn mark-number-called [number boards]
(mapv (fn [board]
(mapv (fn [{:keys [n called]}]
(if (= n number)
{:n n :called true}
{:n n :called called})) board)) boards))
(defn play-part2 [boards numbers]
(when (seq numbers)
(let [next-num (first numbers)
updated-boards (mark-number-called next-num boards)]
(if (and (= 1 (count updated-boards)) (winning-board? (first updated-boards)))
(score-board (first updated-boards) (first numbers))
(recur (remove-all-winners updated-boards) (rest numbers))))))
;; part 2
(let [input (-> (slurp "puzzle-inputs/2021/day4")
(str/replace " " " ")
(str/split-lines))
numbers (->> input
first
(u/str-split #",")
(mapv #(Integer/parseInt %)))
boards (->> input
(drop 1)
(mapcat #(re-seq #"(\d+)" %))
(remove nil?)
(mapv first)
(mapv #(Integer/parseInt %))
(partition 25)
(mapv #(mapv (fn [b] {:n b :called false}) %)))]
(play-part2 boards numbers))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment