Skip to content

Instantly share code, notes, and snippets.

@schmalz
Last active August 4, 2023 16:01
Show Gist options
  • Save schmalz/600bbfc5865a8edb926fe7df2e3e89fb to your computer and use it in GitHub Desktop.
Save schmalz/600bbfc5865a8edb926fe7df2e3e89fb to your computer and use it in GitHub Desktop.
Advent of Code 2022, day 1
(ns day-1.core
(:require [clojure.string :as string]))
(defn- calories-per-elf
"The amount of calories each elf is carrying, greatest value first."
[input-file-name]
(sort >
(map #(reduce +
(map parse-long
(re-seq #"[0-9]+" %)))
(string/split (slurp input-file-name)
#"\n\n"))))
(defn- sum-n-greatest-calorie-counts
"Sum the calories carried by the N elves carrying the greatest amount of
calories."
([input-file-name]
( sum-n-greatest-calorie-counts input-file-name 1))
([input-file-name n]
(apply +
(take n
(calories-per-elf input-file-name)))))
;; Testing.
(comment
(let [input-file-name "test-input.txt"]
(assert (= 24000
(sum-n-greatest-calorie-counts input-file-name)))
(assert (= 45000
(sum-n-greatest-calorie-counts input-file-name 3))))
(let [input-file-name "input.txt"]
(assert (= 69289
(sum-n-greatest-calorie-counts input-file-name)))
(assert (= 205615
(sum-n-greatest-calorie-counts input-file-name 3))))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment