Skip to content

Commit 74bbe7b

Browse files
committed
Refactor part 1
1 parent 260a177 commit 74bbe7b

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/day3.gleam

+8-11
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@ import gleam/regexp
55
import gleam/result
66

77
pub fn part1(input: String) -> Int {
8-
let assert Ok(re) = regexp.from_string("mul\\((\\d+),(\\d+)\\)")
9-
regexp.scan(re, input)
10-
|> list.map(fn(match) {
11-
match.submatches
12-
|> list.map(fn(submatch) { option.unwrap(submatch, "") })
13-
|> list.filter_map(int.parse)
14-
})
15-
|> list.map(fn(mul) {
16-
mul |> list.reduce(fn(acc, m) { acc * m }) |> result.unwrap(0)
8+
input
9+
|> parse_input
10+
|> list.map(fn(token) {
11+
case token {
12+
Mul(a, b) -> a * b
13+
_ -> 0
14+
}
1715
})
18-
|> list.reduce(fn(acc, v) { acc + v })
19-
|> result.unwrap(0)
16+
|> list.fold(0, fn(acc, v) { acc + v })
2017
}
2118

2219
pub fn part2(input: String) -> Int {

0 commit comments

Comments
 (0)