Created
December 4, 2021 20:14
-
-
Save Solaxun/2aa3633017f73e36bd1506a21b937d06 to your computer and use it in GitHub Desktop.
AoC 2021: Clojure Day 4
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 aoc-clj-2021.day4 | |
(:require [clojure.string :as str] | |
[clojure.set :as set] | |
[clojure.java.io :as io])) | |
(def data | |
(->> (io/resource "day4.txt") | |
slurp | |
str/split-lines | |
(remove #(= "" %)))) | |
(def hand (->> (-> data first (str/split #",")) | |
(map parse-long))) | |
(def boards | |
(->> (for [row (rest data) | |
:let [parsed (->> (str/split row #" ") | |
(remove #(= "" %)) | |
(map parse-long))]] | |
(vec parsed)) | |
(partition 5) | |
(map vec))) | |
(defn place-piece [piece board] | |
(vec (for [[r row] (map-indexed vector board)] | |
(vec (for [[c col] (map-indexed vector row)] | |
(if (= col piece) | |
"*" | |
col)))))) | |
(defn row-full? [row] | |
(every? #(= % "*" ) row)) | |
(defn winner? [board] | |
(or (some row-full? board) | |
(some row-full? (apply map vector board)))) | |
(let [b1 (first boards)] | |
(place-piece "*" b1)) | |
(defn play-round [piece boards] | |
(let [bs (map #(place-piece piece %) boards) | |
winner (filter winner? bs)] | |
(when (seq winner) winner))) | |
(defn score [num-called board] | |
(* num-called | |
(->> board | |
flatten | |
(remove #(= % "*")) | |
(reduce +)))) | |
;; part 1 | |
(reduce (fn [boards piece] | |
(let [bs (map #(place-piece piece %) boards) | |
w (filter winner? bs)] | |
(if (seq w) | |
(reduced (score piece (first w))) | |
bs))) | |
boards | |
hand) | |
;; part 2 | |
(-> (reduce (fn [{:keys [boards winners] :as state} piece] | |
(let [bs (map #(place-piece piece %) boards) | |
w (filter winner? bs)] | |
(if (seq w) | |
(-> (update state :winners conj (score piece w)) | |
(assoc :boards (remove winner? bs))) | |
(assoc state :boards (remove winner? bs))))) | |
{:boards boards :winners []} | |
hand) | |
:winners | |
last) |
Author
Solaxun
commented
Dec 21, 2021
via email
Newly added in in clojure 1.11
<https://github.com/clojure/clojure/blob/e21bbfe1d1b06ae703e939951e6c8f199969d70b/src/clj/clojure/core.clj#L7983>,
basically a convenience function to parse strings to integers. Previously
you could do that using java interp e.g. `Integer/parseInt` or Clojure's
`read-string` which was a bit of a hack.
…On Mon, Dec 20, 2021 at 7:30 PM diego ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
What is parse-long?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/2aa3633017f73e36bd1506a21b937d06#gistcomment-4002503>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AB7P43F5FJSIO72F45TYA2TUR7KC7ANCNFSM5KPAFQIA>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you authored the thread.Message ID:
***@***.***>
Great. Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment