Skip to content

Commit d89e9d0

Browse files
committed
Day 1 part 1
1 parent 504961c commit d89e9d0

File tree

5 files changed

+71
-1
lines changed

5 files changed

+71
-1
lines changed

gleam.toml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ version = "1.0.0"
1414

1515
[dependencies]
1616
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
17+
argv = ">= 1.0.2 and < 2.0.0"
18+
simplifile = ">= 2.2.0 and < 3.0.0"
1719

1820
[dev-dependencies]
1921
gleeunit = ">= 1.0.0 and < 2.0.0"

manifest.toml

+5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
# You typically do not need to edit this file
33

44
packages = [
5+
{ name = "argv", version = "1.0.2", build_tools = ["gleam"], requirements = [], otp_app = "argv", source = "hex", outer_checksum = "BA1FF0929525DEBA1CE67256E5ADF77A7CDDFE729E3E3F57A5BDCAA031DED09D" },
6+
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
57
{ name = "gleam_stdlib", version = "0.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" },
68
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
9+
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
710
]
811

912
[requirements]
13+
argv = { version = ">= 1.0.2 and < 2.0.0" }
1014
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
1115
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
16+
simplifile = { version = ">= 2.2.0 and < 3.0.0" }

src/adventofcode2024.gleam

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
import gleam/int
12
import gleam/io
23

4+
import simplifile
5+
6+
import day1
7+
38
pub fn main() {
4-
io.println("Hello from adventofcode2024!")
9+
run(1)
10+
}
11+
12+
fn run(n: Int) -> Nil {
13+
let input = read_input(n)
14+
io.print("Day " <> int.to_string(n) <> " Part 1: ")
15+
io.println(int.to_string(day1.part1(input)))
16+
}
17+
18+
fn read_input(n: Int) -> String {
19+
let filename = "input/day" <> int.to_string(n) <> ".txt"
20+
case simplifile.read(from: filename) {
21+
Ok(data) -> data
22+
Error(_data) ->
23+
panic as { "Input data missing for day " <> int.to_string(n) }
24+
}
525
}

src/day1.gleam

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import gleam/int
2+
import gleam/list
3+
import gleam/string
4+
5+
pub fn part1(input: String) -> Int {
6+
let #(left, right) = parse_input(input)
7+
list.zip(left, right)
8+
|> list.fold(0, fn(acc, pair) { acc + int.absolute_value(pair.0 - pair.1) })
9+
}
10+
11+
fn parse_input(input: String) -> #(List(Int), List(Int)) {
12+
let data =
13+
input
14+
|> string.split("\n")
15+
|> list.filter(fn(line) { line != "" })
16+
|> list.map(fn(line) {
17+
line |> string.split(" ") |> list.filter_map(fn(str) { int.parse(str) })
18+
})
19+
#(
20+
data |> list.filter_map(list.first) |> list.sort(by: int.compare),
21+
data |> list.filter_map(list.last) |> list.sort(by: int.compare),
22+
)
23+
}

test/day1_test.gleam

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import gleeunit
2+
import gleeunit/should
3+
4+
import day1
5+
6+
pub fn main() {
7+
gleeunit.main()
8+
}
9+
10+
const example1 = "3 4
11+
4 3
12+
2 5
13+
1 3
14+
3 9
15+
3 3
16+
"
17+
18+
pub fn part1_test() {
19+
day1.part1(example1) |> should.equal(11)
20+
}

0 commit comments

Comments
 (0)