diff --git a/src/red_nosed_reports.gleam b/src/red_nosed_reports.gleam index 249e1ae..febb032 100644 --- a/src/red_nosed_reports.gleam +++ b/src/red_nosed_reports.gleam @@ -1,5 +1,4 @@ import gleam/int -import gleam/io import gleam/list import gleam/pair import gleam/result @@ -26,26 +25,19 @@ fn all_decr(report: List(Int)) -> Bool { } } -fn valid(report: List(Int)) -> Bool { - all_incr(report) || all_decr(report) +fn valid(report: List(Int), d: Bool) -> Bool { + case d { + False -> all_incr(report) || all_decr(report) + True -> valid(report, False) || list.any(variants(report), valid(_, False)) + } } -fn valid_after_dampener(report) -> Bool { - case valid(report) { - True -> True - False -> { - let indexed_list = list.index_map(report, fn(x, i) { #(i, x) }) - let range = list.range(0, list.length(indexed_list) - 1) - let final_list = - list.map(range, fn(rmi) { - use #(_, flist) <- result.map(list.key_pop(indexed_list, rmi)) - list.map(flist, pair.second) - }) - - let ys = list.map(final_list, fn(y) { result.unwrap(y, []) }) - list.any(ys, fn(y) { valid(y) }) - } - } +fn variants(report: List(Int)) -> List(List(Int)) { + list.range(0, list.length(report) - 1) + |> list.map(list.key_pop(list.index_map(report, fn(x, i) { #(i, x) }), _)) + |> list.map(result.map(_, pair.second)) + |> list.map(result.unwrap(_, [])) + |> list.map(list.map(_, pair.second)) } pub fn solve_a(input: String) -> Int { @@ -53,7 +45,7 @@ pub fn solve_a(input: String) -> Int { |> string.trim() |> string.split("\n") |> list.map(parse_line) - |> list.count(valid(_)) + |> list.count(valid(_, False)) } pub fn solve_b(input: String) -> Int { @@ -61,6 +53,6 @@ pub fn solve_b(input: String) -> Int { |> string.trim() |> string.split("\n") |> list.map(parse_line) - |> list.filter(valid_after_dampener(_)) + |> list.filter(valid(_, True)) |> list.length() }