Skip to content

Commit a5e0e55

Browse files
committed
day 1
1 parent 761797d commit a5e0e55

12 files changed

+185
-37
lines changed

.editorconfig

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[*.cr]
22
indent_style = space
3-
indent_size = 2
3+
indent_size = 2
4+
5+
[*.gleam]
6+
indent_style = space
7+
indent_size = 2

.gitignore

+3-25
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,4 @@
1-
# The directory Mix will write compiled artifacts to.
2-
/_build/
3-
4-
# If you run "mix test --cover", coverage assets end up here.
5-
/cover/
6-
7-
# The directory Mix downloads your dependencies sources to.
8-
/deps/
9-
10-
# Where third-party dependencies like ExDoc output generated docs.
11-
/doc/
12-
13-
# Ignore .fetch files in case you like to edit your project deps locally.
14-
/.fetch
15-
16-
# If the VM crashes, it generates a dump, let's ignore it too.
17-
erl_crash.dump
18-
19-
# Also ignore archive artifacts (built via "mix archive.build").
1+
*.beam
202
*.ez
21-
22-
# Ignore package tarball (built via "mix hex.build").
23-
adventofcode-*.tar
24-
25-
# Temporary files, for example, from tests.
26-
/tmp/
3+
/build
4+
erl_crash.dump

.tool-versions

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
erlang 27.1.2

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Advent of Code 2024
2+
3+
Code written in Gleam
4+
5+
## Development
6+
7+
```sh
8+
gleam run # Run the project
9+
gleam test # Run the tests
10+
```

gleam.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name = "adventofcode"
2+
version = "1.0.0"
3+
4+
# Fill out these fields if you intend to generate HTML documentation or publish
5+
# your project to the Hex package manager.
6+
#
7+
# description = ""
8+
# licences = ["Apache-2.0"]
9+
# repository = { type = "github", user = "", repo = "" }
10+
# links = [{ title = "Website", href = "" }]
11+
#
12+
# For a full reference of all the available options, you can have a look at
13+
# https://gleam.run/writing-gleam/gleam-toml/.
14+
15+
[dependencies]
16+
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
17+
simplifile = ">= 2.2.0 and < 3.0.0"
18+
gtempo = ">= 5.0.2 and < 6.0.0"
19+
20+
[dev-dependencies]
21+
gleeunit = ">= 1.0.0 and < 2.0.0"

manifest.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This file was generated by Gleam
2+
# You typically do not need to edit this file
3+
4+
packages = [
5+
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
6+
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
7+
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
8+
{ name = "gtempo", version = "5.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gtempo", source = "hex", outer_checksum = "E45DC6838FDA4A6BB57A02090738B2266B7AE16D1012CBBB7FA4EC5C741EA83D" },
9+
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
10+
]
11+
12+
[requirements]
13+
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
14+
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
15+
gtempo = { version = ">= 5.0.2 and < 6.0.0" }
16+
simplifile = { version = ">= 2.2.0 and < 3.0.0" }

readme.md

-5
This file was deleted.

shard.yml

-6
This file was deleted.

src/adventofcode.gleam

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import gleam/io
2+
import historian_hysteria
3+
import tempo/duration
4+
5+
pub fn main() {
6+
timed(historian_hysteria.part_1)
7+
timed(historian_hysteria.part_2)
8+
}
9+
10+
pub fn timed(func) {
11+
let start = duration.start_monotonic()
12+
func() |> io.debug
13+
io.println("This took " <> duration.since(start) <> " to run!")
14+
}

src/historian_hysteria.gleam

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import gleam/dict
2+
import gleam/int
3+
import gleam/list
4+
import gleam/option
5+
import gleam/string
6+
import simplifile as file
7+
8+
pub fn part_1() {
9+
let assert Ok(input) = file.read("./input/day1.txt")
10+
11+
input
12+
|> setup()
13+
|> list_processor()
14+
}
15+
16+
pub fn part_2() {
17+
let assert Ok(input) = file.read("./input/day1.txt")
18+
19+
input
20+
|> setup()
21+
|> similarity_scores()
22+
}
23+
24+
pub fn list_processor(lists) {
25+
let #(left, right) = lists
26+
27+
let left = list.sort(left, by: int.compare)
28+
let right = list.sort(right, by: int.compare)
29+
30+
left
31+
|> list.map2(right, fn(x, y) { int.absolute_value(x - y) })
32+
|> list.fold(0, int.add)
33+
|> int.absolute_value
34+
}
35+
36+
pub fn similarity_scores(lists) {
37+
let #(left, right) = lists
38+
let new = dict.new()
39+
40+
let counts = list.fold(right, new, similarity_fold)
41+
42+
list.fold(left, 0, fn(acc, x) {
43+
let key = int.to_string(x)
44+
45+
let sim_score = case dict.get(counts, key) {
46+
Ok(i) -> x * i
47+
Error(Nil) -> 0
48+
}
49+
acc + sim_score
50+
})
51+
}
52+
53+
fn increment(key) {
54+
case key {
55+
option.Some(i) -> i + 1
56+
option.None -> 1
57+
}
58+
}
59+
60+
fn similarity_fold(acc, x) {
61+
let key = int.to_string(x)
62+
dict.upsert(acc, key, increment)
63+
}
64+
65+
fn folder(acc, item) {
66+
let #(left_rest, right_rest) = acc
67+
68+
let assert [left, right] = string.split(item, " ")
69+
let assert Ok(left) = int.parse(left)
70+
let assert Ok(right) = int.parse(right)
71+
72+
#([left, ..left_rest], [right, ..right_rest])
73+
}
74+
75+
fn setup(input) {
76+
input
77+
|> string.trim()
78+
|> string.split("\n")
79+
|> list.fold(#([], []), folder)
80+
}

test/adventofcode_test.gleam

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import gleeunit
2+
import gleeunit/should
3+
4+
pub fn main() {
5+
gleeunit.main()
6+
}
7+
8+
// gleeunit test functions end in `_test`
9+
pub fn hello_world_test() {
10+
1
11+
|> should.equal(1)
12+
}

test/historian_hysteria_test.gleam

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import gleeunit
2+
import gleeunit/should
3+
import historian_hysteria
4+
5+
pub fn main() {
6+
gleeunit.main()
7+
}
8+
9+
pub fn part_1_test() {
10+
let left = [3, 4, 2, 1, 3, 3]
11+
let right = [4, 3, 5, 3, 9, 3]
12+
13+
historian_hysteria.list_processor(#(left, right))
14+
|> should.equal(11)
15+
}
16+
17+
pub fn part_2_test() {
18+
let left = [3, 4, 2, 1, 3, 3]
19+
let right = [4, 3, 5, 3, 9, 3]
20+
21+
historian_hysteria.similarity_scores(#(left, right))
22+
|> should.equal(31)
23+
}

0 commit comments

Comments
 (0)