Created
December 6, 2023 09:15
-
-
Save alexander-yakushev/6ad2ec7bfc16fd9d68c3d133636c59bc to your computer and use it in GitHub Desktop.
Advent of Code 2023, day 6
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 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