Skip to content

Commit ab1b08d

Browse files
committed
Day 1 part 2
1 parent d89e9d0 commit ab1b08d

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

gleam.toml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ version = "1.0.0"
1616
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
1717
argv = ">= 1.0.2 and < 2.0.0"
1818
simplifile = ">= 2.2.0 and < 3.0.0"
19+
tote = ">= 1.0.2 and < 2.0.0"
1920

2021
[dev-dependencies]
2122
gleeunit = ">= 1.0.0 and < 2.0.0"

manifest.toml

+2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ packages = [
77
{ name = "gleam_stdlib", version = "0.51.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "14AFA8D3DDD7045203D422715DBB822D1725992A31DF35A08D97389014B74B68" },
88
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
99
{ name = "simplifile", version = "2.2.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0DFABEF7DC7A9E2FF4BB27B108034E60C81BEBFCB7AB816B9E7E18ED4503ACD8" },
10+
{ name = "tote", version = "1.0.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "tote", source = "hex", outer_checksum = "A249892E26A53C668897F8D47845B0007EEE07707A1A03437487F0CD5A452CA5" },
1011
]
1112

1213
[requirements]
1314
argv = { version = ">= 1.0.2 and < 2.0.0" }
1415
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
1516
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
1617
simplifile = { version = ">= 2.2.0 and < 3.0.0" }
18+
tote = { version = ">= 1.0.2 and < 2.0.0" }

src/adventofcode2024.gleam

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ fn run(n: Int) -> Nil {
1313
let input = read_input(n)
1414
io.print("Day " <> int.to_string(n) <> " Part 1: ")
1515
io.println(int.to_string(day1.part1(input)))
16+
io.print("Day " <> int.to_string(n) <> " Part 2: ")
17+
io.println(int.to_string(day1.part2(input)))
1618
}
1719

1820
fn read_input(n: Int) -> String {

src/day1.gleam

+10
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@ import gleam/int
22
import gleam/list
33
import gleam/string
44

5+
import tote/bag
6+
57
pub fn part1(input: String) -> Int {
68
let #(left, right) = parse_input(input)
79
list.zip(left, right)
810
|> list.fold(0, fn(acc, pair) { acc + int.absolute_value(pair.0 - pair.1) })
911
}
1012

13+
pub fn part2(input: String) -> Int {
14+
let #(left, right) = parse_input(input)
15+
let left_count = bag.from_list(left)
16+
right
17+
|> list.map(fn(r) { r * bag.copies(left_count, r) })
18+
|> list.fold(0, fn(acc, r) { acc + r })
19+
}
20+
1121
fn parse_input(input: String) -> #(List(Int), List(Int)) {
1222
let data =
1323
input

test/day1_test.gleam

+4
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,7 @@ const example1 = "3 4
1818
pub fn part1_test() {
1919
day1.part1(example1) |> should.equal(11)
2020
}
21+
22+
pub fn part2_test() {
23+
day1.part2(example1) |> should.equal(31)
24+
}

0 commit comments

Comments
 (0)