Skip to content

Commit c45d037

Browse files
committedDec 2, 2024·
day2: that one was hard
1 parent b7a72b9 commit c45d037

File tree

7 files changed

+1102
-2
lines changed

7 files changed

+1102
-2
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+
gleam_erlang = ">= 0.32.0 and < 1.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 = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
6+
{ name = "gleam_erlang", version = "0.32.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "B18643083A0117AC5CFD0C1AEEBE5469071895ECFA426DCC26517A07F6AD9948" },
57
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
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+
gleam_erlang = { version = ">= 0.32.0 and < 1.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/aoc_2024.gleam

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import day1
2+
import day2
23
import gleam/io
34

45
pub fn main() {
56
io.println("===== Day 1 ======")
67
day1.main()
8+
io.println("===== Day 2 ======")
9+
day2.main()
710
}

‎src/d2input.txt

+1,000
Large diffs are not rendered by default.

‎src/day1.gleam

-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ pub fn impl_p2(in: String) {
5656
}
5757

5858
pub fn main() {
59-
io.print("Part 1: ")
6059
io.debug(impl_p1(d1input.get_input()))
61-
io.print("Part 2: ")
6260
io.debug(impl_p2(d1input.get_input()))
6361
}

‎src/day2.gleam

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import gleam/int
2+
import gleam/io
3+
import gleam/list
4+
import gleam/option
5+
import gleam/string
6+
import simplifile
7+
8+
fn is_safe(x1: Int, x2: Int, inc: Bool) -> Bool {
9+
let sum = x1 - x2
10+
{ inc && sum > 0 && sum <= 3 } || { !inc && sum < 0 && sum >= -3 }
11+
}
12+
13+
fn solve(l: List(Int), inc: option.Option(Bool)) -> Bool {
14+
case l {
15+
[x1, x2, ..xs] -> {
16+
let actual_inc = case inc {
17+
option.None -> x1 - x2 > 0
18+
option.Some(i) -> i
19+
}
20+
case is_safe(x1, x2, actual_inc) {
21+
True -> solve([x2, ..xs], option.Some(actual_inc))
22+
False -> False
23+
}
24+
}
25+
_ -> True
26+
}
27+
}
28+
29+
pub fn impl_p1(in: String) -> Int {
30+
string.split(in, on: "\n")
31+
|> list.filter(fn(l) {
32+
string.split(l, on: " ")
33+
|> list.filter_map(int.parse)
34+
|> solve(option.None)
35+
})
36+
|> list.count(fn(x) { !string.is_empty(x) })
37+
}
38+
39+
fn dampen_loop(
40+
l: List(Int),
41+
acc: List(Int),
42+
inc: option.Option(Bool),
43+
) -> List(Int) {
44+
case l {
45+
[x, ..xs] -> {
46+
let newlst = list.append(list.reverse(acc), xs)
47+
case solve(newlst, inc) {
48+
True -> newlst
49+
False -> dampen_loop(xs, [x, ..acc], inc)
50+
}
51+
}
52+
[] -> list.reverse(acc)
53+
}
54+
}
55+
56+
fn dampen(l: List(Int), inc: option.Option(Bool)) -> List(Int) {
57+
case solve(l, inc) {
58+
True -> l
59+
False -> dampen_loop(l, [], inc)
60+
}
61+
}
62+
63+
pub fn impl_p2(in: String) -> Int {
64+
string.split(in, on: "\n")
65+
|> list.filter(fn(l) {
66+
string.split(l, on: " ")
67+
|> list.filter_map(int.parse)
68+
|> dampen(option.None)
69+
|> solve(option.None)
70+
})
71+
|> list.count(fn(x) { !string.is_empty(x) })
72+
}
73+
74+
pub fn main() {
75+
let assert Ok(input) = simplifile.read("./src/d2input.txt")
76+
io.debug(impl_p1(string.trim_end(input)))
77+
io.debug(impl_p2(string.trim_end(input)))
78+
}

‎test/aoc_2024_test.gleam

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import gleeunit
22
import gleeunit/should
33

44
import day1
5+
import day2
56

67
pub fn main() {
78
gleeunit.main()
@@ -19,3 +20,16 @@ pub fn day1_test() {
1920
day1.impl_p1(input) |> should.equal(11)
2021
day1.impl_p2(input) |> should.equal(31)
2122
}
23+
24+
pub fn day2_test() {
25+
let input =
26+
"7 6 4 2 1
27+
1 2 7 8 9
28+
9 7 6 2 1
29+
1 3 2 4 5
30+
8 6 4 4 1
31+
3 5 8 11 11 15
32+
1 3 6 7 9"
33+
day2.impl_p1(input) |> should.equal(2)
34+
day2.impl_p2(input) |> should.equal(4)
35+
}

0 commit comments

Comments
 (0)
Please sign in to comment.