Skip to content

Instantly share code, notes, and snippets.

@alexander-yakushev
Created December 4, 2023 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexander-yakushev/51fd7c47d9b6e18de64ec5a324f9f35c to your computer and use it in GitHub Desktop.
Save alexander-yakushev/51fd7c47d9b6e18de64ec5a324f9f35c to your computer and use it in GitHub Desktop.
Advent of Code 2023, day 4
(ns day4
(:require [clojure.set :as set]
[clojure.java.io :as io]))
(def lines (vec (line-seq (io/reader "2023/day4.txt"))))
(defn parse []
(for [line lines]
(let [[winning guessed] (str/split line #"\|")
[game & winning] (map parse-long (re-seq #"\d+" winning))
guessed (map parse-long (re-seq #"\d+" guessed))]
{:game game
:winning (set winning)
:guessed (set guessed)})))
(defn correct [winning guessed]
(count (set/intersection winning guessed)))
(defn task1 []
(transduce (map (fn [{:keys [winning guessed]}]
(let [corr (correct winning guessed)]
(if (pos? corr)
(Math/pow 2 (dec corr))
0))))
+ 0 (parse)))
(defn task2 []
(->> (parse)
(reduce (fn [all-cards {:keys [game winning guessed]}]
(let [all-cards (update all-cards game (fnil inc 0))
corr (correct winning guessed)
cards (get all-cards game 0)]
(reduce #(update %1 %2 (fnil + 0) cards)
all-cards
(range (inc game) (+ game corr 1)))))
{})
vals (reduce +)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment