aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJJ2025-01-02 02:11:37 +0000
committerJJ2025-01-02 02:11:37 +0000
commit830b65d349ec148df049522a1875a8e3d5ce7697 (patch)
treea73509543bce34fc95ae1f4e3b6a85be11fa2dad
parent0e8d6bbf3e55e7506e1e92b5292833b1f3907d06 (diff)
january adventing... omg...
-rw-r--r--2024/lean/Main.lean12
-rw-r--r--2024/lean/Solutions/Day01.lean17
-rw-r--r--2024/lean/lakefile.toml10
-rw-r--r--2024/racket/day01.rkt16
4 files changed, 55 insertions, 0 deletions
diff --git a/2024/lean/Main.lean b/2024/lean/Main.lean
new file mode 100644
index 0000000..cb319c4
--- /dev/null
+++ b/2024/lean/Main.lean
@@ -0,0 +1,12 @@
+import Solutions.Day01
+
+def main (args : List String) : IO Unit := do
+ let usage := "usage: lake exe aoc Day⟨num⟩"
+ match args with
+ | ["--help"] | ["help"] =>
+ IO.println usage
+ | [day] =>
+ let input <- IO.FS.readFile s!"../input/{day.toLower}.txt"
+ IO.println <| solution input
+ | _ =>
+ IO.println usage
diff --git a/2024/lean/Solutions/Day01.lean b/2024/lean/Solutions/Day01.lean
new file mode 100644
index 0000000..1a109df
--- /dev/null
+++ b/2024/lean/Solutions/Day01.lean
@@ -0,0 +1,17 @@
+-- Day 1: Historian Hysteria
+
+def solution (input : String) : String :=
+ let input := input.stripSuffix "\n"|>.splitOn "\n"
+ let l1 := input.map <| fun x => x.splitOn " " |>.get! 0 |>.toInt!
+ let l2 := input.map <| fun x => x.splitOn " " |>.get! 1 |>.toInt!
+ let p1 :=
+ List.foldl (· + ·) 0 <|
+ List.zipWith (fun a b => (a - b).natAbs) l1.mergeSort l2.mergeSort
+ let p2 :=
+ List.foldl (· + ·) 0 <|
+ List.map (fun a => a * List.count a l2) l1
+ s!"{p1}, {p2}"
+
+-- def main (args : List String) : IO Unit := do
+-- let input <- IO.FS.readFile s!"../input/{args.get! 0|>.toLower}.txt"
+-- IO.println <| solution input
diff --git a/2024/lean/lakefile.toml b/2024/lean/lakefile.toml
new file mode 100644
index 0000000..4030553
--- /dev/null
+++ b/2024/lean/lakefile.toml
@@ -0,0 +1,10 @@
+name = "aoc"
+version = "0.1.0"
+defaultTargets = ["aoc"]
+
+[[lean_exe]]
+name = "aoc"
+root = "Main"
+
+[[lean_lib]]
+name = "Solutions"
diff --git a/2024/racket/day01.rkt b/2024/racket/day01.rkt
new file mode 100644
index 0000000..52aa9cd
--- /dev/null
+++ b/2024/racket/day01.rkt
@@ -0,0 +1,16 @@
+#lang racket
+
+;; Day 1: Historian Hysteria
+
+(command-line #:args (filename)
+(let* ([input (file->lines filename)]
+ [l1 (map (λ (x) (string->number (substring x 0 5))) input)]
+ [l2 (map (λ (x) (string->number (substring x 8 13))) input)])
+
+(foldl
+ (λ (a b acc) (+ acc (abs (- a b))))
+ 0 (sort l1 <) (sort l2 <))
+
+(foldl
+ (λ (a acc) (+ acc (* a (count (λ (x) (= x a)) l2))))
+ 0 l1)))