Skip to content

Commit 7d267a1

Browse files
committed
Switch from graphemes to utf codepoints
Per recommendation of LittleLily on Gleam Discord to_utf_codepoints and from_utf_codepoints is faster than to_graphemes and join.
1 parent 07a4474 commit 7d267a1

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/day06/solution.gleam

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ pub fn solve_p1(lines: List(String)) -> Result(String, String) {
3939
pub fn solve_p2(lines: List(String)) -> Result(String, String) {
4040
lines
4141
|> list.map(fn(line) {
42-
string.to_graphemes(line)
42+
string.to_utf_codepoints(line)
4343
|> list.reverse
4444
})
4545
|> list.transpose
4646
|> list.filter_map(fn(column) {
4747
let l = list.length(column)
4848
let #(numbers, operation) = list.split(column, l - 1)
4949
use value <- result.map(
50-
string.join(numbers, "") |> string.trim |> int.parse,
50+
string.from_utf_codepoints(numbers) |> string.trim |> int.parse,
5151
)
52-
#(value, operation)
52+
#(value, string.from_utf_codepoints(operation))
5353
})
5454
|> accumulate_operation(0, [])
5555
|> int.to_string
@@ -76,20 +76,20 @@ fn perform_calculation(parts: List(String)) -> Int {
7676
}
7777

7878
fn accumulate_operation(
79-
tuples: List(#(Int, List(String))),
79+
tuples: List(#(Int, String)),
8080
sum: Int,
8181
values: List(Int),
8282
) -> Int {
8383
case tuples {
8484
[] -> sum
8585
[first, ..rest] -> {
8686
case first {
87-
#(n, [" "]) -> accumulate_operation(rest, sum, [n, ..values])
88-
#(n, ["*"]) -> {
87+
#(n, " ") -> accumulate_operation(rest, sum, [n, ..values])
88+
#(n, "*") -> {
8989
let product = list.fold([n, ..values], 1, fn(p, v) { p * v })
9090
accumulate_operation(rest, sum + product, [])
9191
}
92-
#(n, ["+"]) ->
92+
#(n, "+") ->
9393
accumulate_operation(rest, sum + int.sum([n, ..values]), [])
9494
_ -> panic as "unexpected tuple"
9595
}

0 commit comments

Comments
 (0)