Skip to content

Instantly share code, notes, and snippets.

@alexander-yakushev
Created December 6, 2023 09:15
Show Gist options
  • Save alexander-yakushev/6ad2ec7bfc16fd9d68c3d133636c59bc to your computer and use it in GitHub Desktop.
Save alexander-yakushev/6ad2ec7bfc16fd9d68c3d133636c59bc to your computer and use it in GitHub Desktop.
Advent of Code 2023, day 6
(ns day6
(:require [clojure.string :as str]))
(defn parse [task]
(->> (line-seq (io/reader "2023/day6.txt"))
(map #(map parse-long
(re-seq #"\d+" (case task
1 %
2 (str/replace % #" " "")))))))
(defn winning-options [time distance]
;; a^2 - Ta + D = 0
(let [d (- (* time time) (* 4 distance))
x1 (long (/ (- time (Math/sqrt d)) 2))
;; Second root is symmetrical
x2 (- time x1)]
(dec (- x2 x1))))
(defn solve [task]
(reduce * (apply map winning-options (parse task))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment