This commit is contained in:
Hamcha 2024-12-03 20:07:34 +01:00
commit 1710799225
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
7 changed files with 116 additions and 0 deletions

5
.gitignore vendored Normal file
View file

@ -0,0 +1,5 @@
*.beam
*.ez
/build
erl_crash.dump
/ex/

1
README.md Normal file
View file

@ -0,0 +1 @@
Advent of Code stuff

10
gleam.toml Normal file
View file

@ -0,0 +1,10 @@
name = "aoc"
version = "1.0.0"
[dependencies]
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
stdin = ">= 1.1.4 and < 2.0.0"
gleam_yielder = ">= 1.1.0 and < 2.0.0"
[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"

15
manifest.toml Normal file
View file

@ -0,0 +1,15 @@
# This file was generated by Gleam
# You typically do not need to edit this file
packages = [
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
{ name = "stdin", version = "1.1.4", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "stdin", source = "hex", outer_checksum = "04C04035F2A4CEEFB023837249649CD25F9A9CF5A45F9947C4D0462428A4677D" },
]
[requirements]
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
gleam_yielder = { version = ">= 1.1.0 and < 2.0.0" }
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
stdin = { version = ">= 1.1.4 and < 2.0.0" }

7
sample/day1-sample.txt Normal file
View file

@ -0,0 +1,7 @@
3 4
4 3
2 5
1 3
3 9
3 3

42
src/day1_p1.gleam Normal file
View file

@ -0,0 +1,42 @@
import gleam/int
import gleam/io
import gleam/iterator
import gleam/list.{filter_map}
import gleam/string
import stdin.{stdin}
pub fn main() {
stdin()
|> parse
|> sort
|> calculate_distance
|> io.debug
}
fn calculate_distance(list: #(List(Int), List(Int))) -> Int {
let #(first, second) = list
list.map2(first, second, fn(first, second) {
int.absolute_value(second - first)
})
|> list.fold(0, int.add)
}
fn sort(list: #(List(Int), List(Int))) -> #(List(Int), List(Int)) {
let #(first, second) = list
#(list.sort(first, by: int.compare), list.sort(second, by: int.compare))
}
fn parse(it: iterator.Iterator(String)) -> #(List(Int), List(Int)) {
it
|> iterator.fold(#([], []), fn(acc, line) {
case string.trim(line) {
"" -> acc
str -> {
let #(first_list, second_list) = acc
let assert [first, second] =
str |> string.split(" ") |> filter_map(int.parse)
#([first, ..first_list], [second, ..second_list])
}
}
})
}

36
src/day1_p2.gleam Normal file
View file

@ -0,0 +1,36 @@
import gleam/int
import gleam/io
import gleam/iterator
import gleam/list.{filter_map}
import gleam/string
import stdin.{stdin}
pub fn main() {
stdin()
|> parse
|> calculate_similarity
|> io.debug
}
fn calculate_similarity(list: #(List(Int), List(Int))) -> Int {
let #(first, second) = list
first
|> list.fold(0, fn(acc, elem) {
acc + elem * list.count(second, fn(candidate) { candidate == elem })
})
}
fn parse(it: iterator.Iterator(String)) -> #(List(Int), List(Int)) {
it
|> iterator.fold(#([], []), fn(acc, line) {
case string.trim(line) {
"" -> acc
str -> {
let #(first_list, second_list) = acc
let assert [first, second] =
str |> string.split(" ") |> filter_map(int.parse)
#([first, ..first_list], [second, ..second_list])
}
}
})
}