-1
2025/6/gleam/gleam.toml
-1
2025/6/gleam/gleam.toml
-2
2025/6/gleam/manifest.toml
-2
2025/6/gleam/manifest.toml
···
3
4
packages = [
5
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
6
-
{ name = "gleam_regexp", version = "1.1.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_regexp", source = "hex", outer_checksum = "9C215C6CA84A5B35BB934A9B61A9A306EC743153BE2B0425A0D032E477B062A9" },
7
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
8
{ name = "gleeunit", version = "1.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "CD701726CBCE5588B375D157B4391CFD0F2F134CD12D9B6998A395484DE05C58" },
9
{ name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" },
10
]
11
12
[requirements]
13
-
gleam_regexp = { version = ">= 1.1.1 and < 2.0.0" }
14
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
15
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
16
simplifile = { version = ">= 2.3.0 and < 3.0.0" }
···
3
4
packages = [
5
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
6
{ name = "gleam_stdlib", version = "0.65.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "7C69C71D8C493AE11A5184828A77110EB05A7786EBF8B25B36A72F879C3EE107" },
7
{ name = "gleeunit", version = "1.7.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "CD701726CBCE5588B375D157B4391CFD0F2F134CD12D9B6998A395484DE05C58" },
8
{ name = "simplifile", version = "2.3.0", build_tools = ["gleam"], requirements = ["filepath", "gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "0A868DAC6063D9E983477981839810DC2E553285AB4588B87E3E9C96A7FB4CB4" },
9
]
10
11
[requirements]
12
gleam_stdlib = { version = ">= 0.44.0 and < 2.0.0" }
13
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
14
simplifile = { version = ">= 2.3.0 and < 3.0.0" }
+86
-90
2025/6/gleam/src/main.gleam
+86
-90
2025/6/gleam/src/main.gleam
···
2
import gleam/int
3
import gleam/io
4
import gleam/list
5
-
import gleam/regexp
6
import gleam/result
7
import gleam/string
8
import simplifile as file
···
33
acc |> string.replace(" ", " ")
34
})
35
36
-
let part_1 =
37
-
input_pt_1
38
-
|> string.split("\n")
39
-
|> list.map(fn(i) { string.trim(i) |> string.split(" ") })
40
-
|> list.transpose
41
-
|> list.map(fn(i) {
42
-
let i = list.reverse(i)
43
-
let assert Ok(s) = list.first(i)
44
-
let i =
45
-
list.drop(i, 1) |> list.map(fn(i) { int.parse(i) |> result.unwrap(0) })
46
-
let r = case s {
47
-
"+" -> int.sum(i)
48
-
"*" -> list.reduce(i, int.multiply) |> result.unwrap(0)
49
-
_ -> panic as "invalid"
50
-
}
51
-
r
52
-
})
53
-
|> int.sum
54
-
echo part_1
55
56
let lines =
57
input
···
68
_ -> acc
69
}
70
})
71
-
let input_pt_2 =
72
-
bounds
73
-
|> list.index_fold(dict.new(), fn(d, bound, i) {
74
-
let numbers =
75
-
list.map(lines, fn(line) {
76
-
string.slice(line, bound.0, bound.1 - bound.0)
77
-
})
78
-
let align =
79
-
numbers
80
-
|> list.drop(1)
81
-
|> list.fold_until(Left, fn(res, number) {
82
-
case
83
-
string.trim(number) == number,
84
-
string.trim_start(number) == number
85
-
{
86
-
True, _ -> list.Continue(res)
87
-
_, True -> list.Stop(Left)
88
-
_, _ -> list.Stop(Right)
89
}
90
-
})
91
-
let assert Ok(sign) = list.first(numbers)
92
-
let sign = case string.trim(sign) {
93
-
"*" -> Mul
94
-
"+" -> Sum
95
-
_ -> panic as sign
96
-
}
97
-
dict.insert(
98
-
d,
99
-
i,
100
-
Part2Line(
101
-
align,
102
-
sign,
103
-
numbers |> list.drop(1) |> list.map(string.trim) |> list.reverse,
104
-
),
105
-
)
106
-
})
107
-
let part_2 =
108
-
input_pt_2
109
-
|> dict.to_list
110
-
|> list.map(fn(i) { i.1 })
111
-
|> list.map(fn(line) {
112
-
let d: Part2Dict = dict.new()
113
-
let d =
114
-
line.numbers
115
-
|> list.fold(d, fn(d, number) {
116
-
let number_len = string.length(number)
117
-
string.to_graphemes(number)
118
-
|> list.index_fold(d, fn(d, digit, index) {
119
-
let assert Ok(digit) = digit |> int.parse
120
-
let pos = case line.align {
121
-
Right -> number_len - index
122
-
Left -> index
123
-
}
124
-
dict.insert(
125
-
d,
126
-
pos,
127
-
{ dict.get(d, pos) |> result.unwrap(0) } * 10 + digit,
128
-
)
129
-
})
130
})
131
-
echo #(d, line)
132
-
let numbers =
133
-
dict.to_list(d)
134
-
|> list.map(fn(n) { n.1 })
135
136
-
let r = case line.op {
137
-
Sum -> int.sum(numbers)
138
-
Mul -> list.reduce(numbers, int.multiply) |> result.unwrap(0)
139
-
}
140
-
r
141
-
})
142
-
|> int.sum
143
-
echo part_2
144
}
···
2
import gleam/int
3
import gleam/io
4
import gleam/list
5
import gleam/result
6
import gleam/string
7
import simplifile as file
···
32
acc |> string.replace(" ", " ")
33
})
34
35
+
input_pt_1
36
+
|> string.split("\n")
37
+
|> list.map(fn(i) { string.trim(i) |> string.split(" ") })
38
+
|> list.transpose
39
+
|> list.map(fn(i) {
40
+
let i = list.reverse(i)
41
+
let assert Ok(s) = list.first(i)
42
+
let i =
43
+
list.drop(i, 1) |> list.map(fn(i) { int.parse(i) |> result.unwrap(0) })
44
+
let r = case s {
45
+
"+" -> int.sum(i)
46
+
"*" -> list.reduce(i, int.multiply) |> result.unwrap(0)
47
+
_ -> panic as "invalid"
48
+
}
49
+
r
50
+
})
51
+
|> int.sum
52
+
|> int.to_string
53
+
|> io.println
54
55
let lines =
56
input
···
67
_ -> acc
68
}
69
})
70
+
bounds
71
+
|> list.index_fold(dict.new(), fn(d, bound, i) {
72
+
let numbers =
73
+
list.map(lines, fn(line) {
74
+
string.slice(line, bound.0, bound.1 - bound.0)
75
+
})
76
+
let align =
77
+
numbers
78
+
|> list.drop(1)
79
+
|> list.fold_until(Left, fn(res, number) {
80
+
case
81
+
string.trim(number) == number,
82
+
string.trim_start(number) == number
83
+
{
84
+
True, _ -> list.Continue(res)
85
+
_, True -> list.Stop(Left)
86
+
_, _ -> list.Stop(Right)
87
+
}
88
+
})
89
+
let assert Ok(sign) = list.first(numbers)
90
+
let sign = case string.trim(sign) {
91
+
"*" -> Mul
92
+
"+" -> Sum
93
+
_ -> panic as sign
94
+
}
95
+
dict.insert(
96
+
d,
97
+
i,
98
+
Part2Line(
99
+
align,
100
+
sign,
101
+
numbers |> list.drop(1) |> list.map(string.trim) |> list.reverse,
102
+
),
103
+
)
104
+
})
105
+
|> dict.to_list
106
+
|> list.map(fn(i) { i.1 })
107
+
|> list.map(fn(line) {
108
+
let d: Part2Dict = dict.new()
109
+
let d =
110
+
line.numbers
111
+
|> list.fold(d, fn(d, number) {
112
+
let number_len = string.length(number)
113
+
string.to_graphemes(number)
114
+
|> list.index_fold(d, fn(d, digit, index) {
115
+
let assert Ok(digit) = digit |> int.parse
116
+
let pos = case line.align {
117
+
Right -> number_len - index
118
+
Left -> index
119
}
120
+
dict.insert(
121
+
d,
122
+
pos,
123
+
{ dict.get(d, pos) |> result.unwrap(0) } * 10 + digit,
124
+
)
125
})
126
+
})
127
+
let numbers =
128
+
dict.to_list(d)
129
+
|> list.map(fn(n) { n.1 })
130
131
+
let r = case line.op {
132
+
Sum -> int.sum(numbers)
133
+
Mul -> list.reduce(numbers, int.multiply) |> result.unwrap(0)
134
+
}
135
+
r
136
+
})
137
+
|> int.sum
138
+
|> int.to_string
139
+
|> io.println
140
}