Created
December 4, 2021 19:54
-
-
Save stuartstein777/74765018745c8a500dbf4b2ad9405451 to your computer and use it in GitHub Desktop.
AOC 2021 Day 4 Pat 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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