<commit_before>(ns adventofcode.day14
  (:require [clojure.java.io :as io]
            [clojure.string :as str]))

(defn parse-line [line]
    (let [[_ name speed fly rest] (re-find #"^(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds")]
        (list name (parse-srting speed) (parse-string fly) (parse-string rest))))

(def reindeers (->> "day14.txt"
                io/resource
                io/file
                slurp
                str/split-lines
                (reduce
                    (fn [res [name speed fly rest]
                         (assoc-in res name {:speed speed
                                             :fly fly
                                             :rest rest})])
                    {})))<commit_msg>day 14 working<commit_after>(ns adventofcode.day14
  (:require [clojure.java.io :as io]
            [clojure.string :as str]))

(defn line->list [line]
  (let [[_ name speed fly rest] (re-find #"^(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds" line)]
    (list name (read-string speed) (read-string fly) (read-string rest))))

(def input-reindeers (->> "day14.txt"
                          io/resource
                          io/file
                          slurp
                          str/split-lines
                          (map line->list)
                          (reduce
                           (fn [m [name speed fly rest]]
                             (assoc m name {:speed speed
                                            :fly fly
                                            :rest rest}))
                           {})))

(defn distance [reindeers name time]
  (let [{:keys [speed fly rest]} (get reindeers name)
        intervals-count (quot time (+ fly rest))
        time-remain (rem time (+ fly rest))]
    (+
     (* speed fly intervals-count)
     (* speed (min time-remain fly)))))

(defn winner [reindeers time]
  (->> (keys reindeers)
       (reduce #(assoc %1 %2 (distance reindeers %2 time))
               {})
       (sort-by second >)
       first
       first))

(def part-one (->> (keys input-reindeers) 
                   (map #(distance input-reindeers % 2503))
                   (apply max)))

(def part-two (->> (for [i (range 1 2504)] (winner input-reindeers i))
                   frequencies
                   (apply max-key val)
                   second))
{'fraction_non_alphanumeric': 0.1267080745341615, 'fraction_numerical': 0.009109730848861283, 'mean_word_length': 1.2621722846441947, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6857147', 'n_tokens_mistral': 697, 'n_tokens_neox': 658, 'n_words': 206}