-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#!/usr/bin/env bb | ||
|
||
(ns y2024.d01 | ||
(:require [babashka.cli :as cli])) | ||
|
||
|
||
(def cli-options {:input {:default "../../inputs/2024/01.input" :type :string}}) | ||
|
||
(def opts (cli/parse-opts *command-line-args* {:spec cli-options})) | ||
|
||
(def input | ||
(->> (slurp (get opts :input)) | ||
(re-seq #"\d+") | ||
(map parse-long) | ||
(partition 2) | ||
(apply map vector))) | ||
|
||
(defn part1 [[lhs rhs]] | ||
(->> (map - (sort lhs) (sort rhs)) | ||
(map abs) | ||
(reduce +))) | ||
|
||
(defn part2 [[lhs rhs]] | ||
(let [freqs (frequencies rhs)] | ||
(->> (map #(* % (freqs % 0)) lhs) | ||
(reduce +)))) | ||
|
||
(prn {:part1 (part1 input) | ||
:part2 (part2 input)}) |