tangled
alpha
login
or
join now
regnault.dev
/
gleam-aoc-2025
1
fork
atom
this repo has no description
1
fork
atom
overview
issues
pulls
pipelines
Day 2
regnault.dev
3 months ago
ec64cb2a
573c5aef
+159
4 changed files
expand all
collapse all
unified
split
gleam.toml
manifest.toml
src
aoc.gleam
days
day2.gleam
+1
gleam.toml
···
10
10
gleam_otp = ">= 0.12.1 and < 1.0.0"
11
11
birl = ">= 1.7.1 and < 2.0.0"
12
12
gleam_regexp = ">= 1.0.0 and < 2.0.0"
13
13
+
gleam_community_maths = ">= 2.0.0 and < 3.0.0"
13
14
14
15
[dev-dependencies]
15
16
gleeunit = ">= 1.0.0 and < 2.0.0"
+3
manifest.toml
···
4
4
packages = [
5
5
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
6
6
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
7
7
+
{ name = "gleam_community_maths", version = "2.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_yielder"], otp_app = "gleam_community_maths", source = "hex", outer_checksum = "C9EFF6BCF3C9D113EF9837655B0128CA8CAC295131E9F3468F93C1D78EC3E013" },
7
8
{ name = "gleam_erlang", version = "0.32.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "B18643083A0117AC5CFD0C1AEEBE5469071895ECFA426DCC26517A07F6AD9948" },
8
9
{ name = "gleam_otp", version = "0.14.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "5A8CE8DBD01C29403390A7BD5C0A63D26F865C83173CF9708E6E827E53159C65" },
9
10
{ name = "gleam_regexp", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "A3655FDD288571E90EE9C4009B719FEF59FA16AFCDF3952A76A125AF23CF1592" },
10
11
{ name = "gleam_stdlib", version = "0.45.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "206FCE1A76974AECFC55AEBCD0217D59EDE4E408C016E2CFCCC8FF51278F186E" },
12
12
+
{ name = "gleam_yielder", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_yielder", source = "hex", outer_checksum = "8E4E4ECFA7982859F430C57F549200C7749823C106759F4A19A78AEA6687717A" },
11
13
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
12
14
{ name = "nibble", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "nibble", source = "hex", outer_checksum = "67C6BEBC1AB6D771AB893B4A7B3E66C92668C6E7774C335FEFCD545B06435FE5" },
13
15
{ name = "ranger", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "ranger", source = "hex", outer_checksum = "B8F3AFF23A3A5B5D9526B8D18E7C43A7DFD3902B151B97EC65397FE29192B695" },
···
16
18
17
19
[requirements]
18
20
birl = { version = ">= 1.7.1 and < 2.0.0" }
21
21
+
gleam_community_maths = { version = ">= 2.0.0 and < 3.0.0" }
19
22
gleam_erlang = { version = ">= 0.27.0 and < 1.0.0" }
20
23
gleam_otp = { version = ">= 0.12.1 and < 1.0.0" }
21
24
gleam_regexp = { version = ">= 1.0.0 and < 2.0.0" }
+2
src/aoc.gleam
···
1
1
import birl
2
2
import birl/duration
3
3
import days/day1
4
4
+
import days/day2
4
5
import gleam/erlang
5
6
import gleam/int
6
7
import gleam/io
···
21
22
case day {
22
23
0 -> io.println_error("Invalid day")
23
24
1 -> day1.start()
25
25
+
2 -> day2.start()
24
26
_ -> io.println("Tried to run day " <> int.to_string(day))
25
27
}
26
28
birl.now()
+153
src/days/day2.gleam
···
1
1
+
import gleam/float
2
2
+
import gleam/int
3
3
+
import gleam/io
4
4
+
import gleam/list
5
5
+
import gleam/result
6
6
+
import gleam/string
7
7
+
import gleam_community/maths
8
8
+
import simplifile
9
9
+
import utils/utils
10
10
+
11
11
+
type Range =
12
12
+
#(Int, Int)
13
13
+
14
14
+
pub fn start() -> Nil {
15
15
+
let assert Ok(content) = simplifile.read("inputs/day2.txt")
16
16
+
let data = parse(content)
17
17
+
18
18
+
let _ = io.debug(part1(data))
19
19
+
let _ = io.debug(part2(data))
20
20
+
21
21
+
Nil
22
22
+
}
23
23
+
24
24
+
fn parse(data: String) -> List(Range) {
25
25
+
data
26
26
+
|> string.split(",")
27
27
+
|> list.map(fn(x) {
28
28
+
let lst =
29
29
+
x
30
30
+
|> string.split("-")
31
31
+
|> list.map(int.parse)
32
32
+
|> list.map(result.unwrap(_, 0))
33
33
+
34
34
+
#(
35
35
+
list.first(lst) |> result.unwrap(0),
36
36
+
list.drop(lst, 1) |> list.first |> result.unwrap(0),
37
37
+
)
38
38
+
})
39
39
+
}
40
40
+
41
41
+
fn is_made_of_part(x: Int) -> Bool {
42
42
+
let length =
43
43
+
maths.logarithm_10(int.to_float(x))
44
44
+
|> result.unwrap(0.0)
45
45
+
|> float.floor
46
46
+
|> float.add(1.0)
47
47
+
|> float.round
48
48
+
49
49
+
utils.create_int_range(2, length + 1)
50
50
+
|> list.filter(fn(y) {
51
51
+
int.to_float(length) /. int.to_float(y)
52
52
+
== float.floor(int.to_float(length) /. int.to_float(y))
53
53
+
})
54
54
+
|> list.map(fn(l) {
55
55
+
let half_power =
56
56
+
{ int.to_float(length) /. int.to_float(l) }
57
57
+
|> float.floor
58
58
+
|> float.power(10.0, _)
59
59
+
|> result.unwrap(0.0)
60
60
+
|> float.round()
61
61
+
62
62
+
#(utils.create_int_range(0, l), half_power)
63
63
+
})
64
64
+
|> list.map(fn(index) {
65
65
+
let #(idx, half_power) = index
66
66
+
list.map(idx, fn(y) {
67
67
+
{
68
68
+
int.to_float(x)
69
69
+
/. {
70
70
+
int.power(half_power, int.to_float(y))
71
71
+
|> result.unwrap(1.0)
72
72
+
}
73
73
+
|> float.modulo(int.to_float(half_power))
74
74
+
|> result.unwrap(0.0)
75
75
+
|> float.floor
76
76
+
|> float.round
77
77
+
}
78
78
+
})
79
79
+
})
80
80
+
|> list.any(fn(x) {
81
81
+
list.all(x, fn(y) { y == { list.first(x) |> result.unwrap(0) } })
82
82
+
})
83
83
+
}
84
84
+
85
85
+
fn is_symetric(x: Int) -> Bool {
86
86
+
let length =
87
87
+
maths.logarithm_10(int.to_float(x))
88
88
+
|> result.unwrap(0.0)
89
89
+
|> float.floor
90
90
+
|> float.add(1.0)
91
91
+
92
92
+
let half_power =
93
93
+
float.divide(length, 2.0)
94
94
+
|> result.unwrap(0.0)
95
95
+
|> float.floor
96
96
+
|> float.power(10.0, _)
97
97
+
|> result.unwrap(0.0)
98
98
+
|> float.round()
99
99
+
100
100
+
x / half_power == x % half_power
101
101
+
}
102
102
+
103
103
+
fn part1(data: List(Range)) {
104
104
+
data
105
105
+
|> list.map(fn(range: Range) {
106
106
+
let #(start, end) = range
107
107
+
utils.loop(
108
108
+
case is_symetric(start) {
109
109
+
True -> #(start, start)
110
110
+
False -> #(start, 0)
111
111
+
},
112
112
+
fn(x: #(Int, Int)) {
113
113
+
let #(start, _) = x
114
114
+
start <= end
115
115
+
},
116
116
+
fn(x: #(Int, Int)) {
117
117
+
let #(start, count) = x
118
118
+
case is_symetric(start + 1) {
119
119
+
True -> #(start + 1, count + start + 1)
120
120
+
False -> #(start + 1, count)
121
121
+
}
122
122
+
},
123
123
+
fn(x: #(Int, Int)) { x },
124
124
+
)
125
125
+
})
126
126
+
|> list.fold(0, fn(acc, v: #(Int, Int)) { acc + v.1 })
127
127
+
}
128
128
+
129
129
+
fn part2(data) {
130
130
+
data
131
131
+
|> list.map(fn(range: Range) {
132
132
+
let #(start, end) = range
133
133
+
utils.loop(
134
134
+
case is_made_of_part(start) {
135
135
+
True -> #(start, start)
136
136
+
False -> #(start, 0)
137
137
+
},
138
138
+
fn(x: #(Int, Int)) {
139
139
+
let #(start, _) = x
140
140
+
start <= end
141
141
+
},
142
142
+
fn(x: #(Int, Int)) {
143
143
+
let #(start, count) = x
144
144
+
case is_made_of_part(start + 1) {
145
145
+
True -> #(start + 1, count + start + 1)
146
146
+
False -> #(start + 1, count)
147
147
+
}
148
148
+
},
149
149
+
fn(x: #(Int, Int)) { x },
150
150
+
)
151
151
+
})
152
152
+
|> list.fold(0, fn(acc, v: #(Int, Int)) { acc + v.1 })
153
153
+
}