Skip to content

Commit

Permalink
day 03: improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sreedevk committed Dec 3, 2024
1 parent 956dbf0 commit abf0832
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/mull_it_over.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> state
}
}
}
Expand All @@ -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)
}
Expand Down

0 comments on commit abf0832

Please sign in to comment.