diff --git a/src/mull_it_over.gleam b/src/mull_it_over.gleam index 52a0c95..e5c3944 100644 --- a/src/mull_it_over.gleam +++ b/src/mull_it_over.gleam @@ -8,23 +8,18 @@ pub fn process_isr(instr: String) -> Int { let assert Ok(args_rex) = regexp.from_string("\\d{1,3}") regexp.scan(args_rex, instr) |> list.map(fn(x) { x.content }) - |> list.map(fn(x) { result.unwrap(int.parse(x), 0) }) + |> list.map(fn(x) { result.unwrap(int.parse(x), -1) }) |> list.fold_right(1, int.multiply) } pub fn process_cond_isr(state: #(Bool, Int), instr: String) -> #(Bool, Int) { - case state { - #(True, x) -> - case instr { - "do()" -> #(True, x) - "don't()" -> #(False, x) - _ -> #(True, int.add(process_isr(instr), x)) - } - #(False, x) -> - case instr { - "do()" -> #(True, x) - "don't()" -> #(False, x) - _ -> #(False, x) + case instr { + "do()" -> #(True, pair.second(state)) + "don't()" -> #(False, pair.second(state)) + _ -> + case pair.first(state) { + True -> #(True, int.add(process_isr(instr), pair.second(state))) + False -> #(False, pair.second(state)) } } } @@ -34,7 +29,7 @@ pub fn solve_a(input: String) -> Int { regexp.scan(rex, input) |> list.map(fn(match) { match.content }) - |> list.map(process_isr(_)) + |> list.map(process_isr) |> list.reduce(int.add) |> result.unwrap(0) }