Last active
June 17, 2025 18:24
-
-
Save hovsater/e7a6a9b1800cfbad4da087822310ecb2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import gleam/int | |
import gleam/list | |
import gleam/option.{Some} | |
import gleam/regexp | |
type State { | |
State(sum: Int, multiplier: Int) | |
} | |
pub fn part_one(input: String) -> Int { | |
let assert Ok(re) = regexp.from_string("mul\\((\\d+),(\\d+)\\)") | |
list.fold(regexp.scan(with: re, content: input), from: 0, with: fn(sum, m) { | |
let assert [Some(a), Some(b)] = m.submatches | |
let assert Ok(x) = int.parse(a) | |
let assert Ok(y) = int.parse(b) | |
sum + x * y | |
}) | |
} | |
pub fn part_two(input: String) -> Int { | |
let assert Ok(re) = | |
regexp.from_string("mul\\((\\d+),(\\d+)\\)|do\\(\\)|don't\\(\\)") | |
let State(sum, multiplier: _) = | |
list.fold( | |
regexp.scan(with: re, content: input), | |
from: State(sum: 0, multiplier: 1), | |
with: fn(state, m) { | |
case m.content { | |
"don't" <> _ -> State(..state, multiplier: 0) | |
"do" <> _ -> State(..state, multiplier: 1) | |
_ -> { | |
let assert [Some(a), Some(b)] = m.submatches | |
let assert Ok(x) = int.parse(a) | |
let assert Ok(y) = int.parse(b) | |
State(..state, sum: state.sum + x * y * state.multiplier) | |
} | |
} | |
}, | |
) | |
sum | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment