Created
December 18, 2015 16:03
-
-
Save sbenhaim/098723cca61984b7e545 to your computer and use it in GitHub Desktop.
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 advent.a14 | |
(:require [clojure.string :as str])) | |
(def rules "Rudolph can fly 22 km/s for 8 seconds, but then must rest for 165 seconds. | |
Cupid can fly 8 km/s for 17 seconds, but then must rest for 114 seconds. | |
Prancer can fly 18 km/s for 6 seconds, but then must rest for 103 seconds. | |
Donner can fly 25 km/s for 6 seconds, but then must rest for 145 seconds. | |
Dasher can fly 11 km/s for 12 seconds, but then must rest for 125 seconds. | |
Comet can fly 21 km/s for 6 seconds, but then must rest for 121 seconds. | |
Blitzen can fly 18 km/s for 3 seconds, but then must rest for 50 seconds. | |
Vixen can fly 20 km/s for 4 seconds, but then must rest for 75 seconds. | |
Dancer can fly 7 km/s for 20 seconds, but then must rest for 119 seconds.") | |
(defn parse-int [s] | |
(Integer/parseInt s)) | |
(defn parse [rule] | |
(let [[_ vel time rest] (re-find #".*?(\d+) km/s for (\d+) seconds.*?(\d+) seconds." rule)] | |
(map parse-int [vel time rest]))) | |
(defn vel-at-second [[vel time rest]] | |
(cycle (concat (repeat time vel) (repeat rest 0)))) | |
(defn distance-at [time vs] | |
(reduce + (take time (vel-at-second vs))))o | |
(defn rolling-sum [is] | |
(reduce (fn [r i] (conj r (+ (or (last r) 0) i))) [] is)) | |
(defn transpose [vss] | |
(for [i (range (count (first vss)))] | |
(for [vs vss] | |
(nth vs i)))) | |
(defn score-race [vss] | |
(transpose | |
(for [vs (transpose vss)] | |
(let [fstst (apply max vs)] | |
(for [v vs] | |
(if (== v fstst) 1 0)))))) | |
(let [time 2503] | |
(->> (str/split-lines rules) | |
(map parse) | |
(map vel-at-second) | |
(map (partial take time)) | |
(map rolling-sum) | |
score-race | |
(map #(reduce + %)) | |
(apply max))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment