diff options
author | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-03-02 21:40:59 +0100 |
---|---|---|
committer | Tomasz Chojnacki <tomaszchojnacki2001@gmail.com> | 2023-03-02 21:40:59 +0100 |
commit | 6c661db5b3e167d45554f7c5e4838dbc3bffa63a (patch) | |
tree | 6ba5eebddc1713f5bd4ee9fe740937a8031d151e /aoc-2020-gleam/src/days/day08.gleam | |
parent | 2ba7501d516afa4ab240faccefb6a3f72223598e (diff) | |
download | gleam_aoc2020-6c661db5b3e167d45554f7c5e4838dbc3bffa63a.tar.gz gleam_aoc2020-6c661db5b3e167d45554f7c5e4838dbc3bffa63a.zip |
Refactor using constructs from Gleam v0.27
Diffstat (limited to 'aoc-2020-gleam/src/days/day08.gleam')
-rw-r--r-- | aoc-2020-gleam/src/days/day08.gleam | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/aoc-2020-gleam/src/days/day08.gleam b/aoc-2020-gleam/src/days/day08.gleam index 6f074cf..9ef430b 100644 --- a/aoc-2020-gleam/src/days/day08.gleam +++ b/aoc-2020-gleam/src/days/day08.gleam @@ -1,5 +1,7 @@ import gleam/io +import gleam/int import gleam/list +import gleam/bool import gleam/set.{Set} import gleam/iterator.{Iterator} as iter import gleam/option.{None, Option, Some} as opt @@ -35,7 +37,7 @@ fn parse_program(lines: List(String)) -> Program { p.replace(p.literal("-"), with: -1), ]) |> p.then(p.int()) - |> p.map2(with: fn(sign, magnitude) { sign * magnitude }) + |> p.map2(with: int.multiply) let instr_parser = p.any(of: [ @@ -77,10 +79,14 @@ fn execution_result_helper( cpu: Cpu, visited: Set(Int), ) -> ExecutionResult { - case set.contains(visited, cpu.pc), fetch(from: program, with: cpu) { - True, _ -> InfiniteLoop(acc_before_second: cpu.acc) - _, None -> Termination(acc_after: cpu.acc) - _, Some(instr) -> + use <- bool.guard( + when: set.contains(visited, cpu.pc), + return: InfiniteLoop(acc_before_second: cpu.acc), + ) + + case fetch(from: program, with: cpu) { + None -> Termination(acc_after: cpu.acc) + Some(instr) -> execution_result_helper( program, execute(instr, on: cpu), |