|
| 1 | +import gleam/deque |
| 2 | +import gleam/dict |
| 3 | +import gleam/erlang/process |
| 4 | +import gleam/int |
| 5 | +import gleam/io |
| 6 | +import gleam/list |
| 7 | +import gleam/option |
| 8 | +import gleam/result |
| 9 | +import gleam/set |
| 10 | +import gleam/string |
| 11 | +import internal/aoc_utils |
| 12 | +import internal/memoize |
| 13 | + |
| 14 | +pub fn main() { |
| 15 | + let filename = "inputs/day10.txt" |
| 16 | + |
| 17 | + let lines_result = aoc_utils.read_lines(from: filename) |
| 18 | + case lines_result { |
| 19 | + Ok(lines) -> { |
| 20 | + // If the file was converting into a list of lines |
| 21 | + // successfully then run each part of the problem |
| 22 | + aoc_utils.run_part_and_print("Part 1", fn() { solve_p1(lines) }) |
| 23 | + aoc_utils.run_part_and_print("Part 2", fn() { solve_p2(lines) }) |
| 24 | + } |
| 25 | + Error(_) -> io.println("Error reading file") |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +// Part 1 |
| 30 | +pub fn solve_p1(lines: List(String)) -> Result(String, String) { |
| 31 | + lines |
| 32 | + |> list.map(fn(row) { |
| 33 | + let parts = parse(row) |
| 34 | + |
| 35 | + let assert Ok(light_list) = dict.get(parts, "lights") |
| 36 | + let assert Ok(light_string) = list.first(light_list) |
| 37 | + let lights = parse_lights(light_string) |
| 38 | + |
| 39 | + let assert Ok(button_strings) = dict.get(parts, "buttons") |
| 40 | + let buttons = button_strings |> list.map(parse_button) |
| 41 | + |
| 42 | + list.range(1, list.length(buttons)) |
| 43 | + |> list.fold_until(0, fn(count, pushes) { |
| 44 | + let result = |
| 45 | + list.combinations(buttons, pushes) |
| 46 | + |> list.fold_until(count, fn(c, button_combo) { |
| 47 | + case press_buttons(button_combo) == lights { |
| 48 | + True -> list.Stop(pushes) |
| 49 | + False -> list.Continue(c) |
| 50 | + } |
| 51 | + }) |
| 52 | + |
| 53 | + case result { |
| 54 | + 0 -> list.Continue(count) |
| 55 | + n -> list.Stop(n) |
| 56 | + } |
| 57 | + }) |
| 58 | + }) |
| 59 | + |> int.sum |
| 60 | + |> int.to_string |
| 61 | + |> Ok |
| 62 | +} |
| 63 | + |
| 64 | +// Part 2 |
| 65 | +pub fn solve_p2(lines: List(String)) -> Result(String, String) { |
| 66 | + lines |
| 67 | + |> list.map(fn(row) { |
| 68 | + let parts = parse(row) |
| 69 | + |
| 70 | + let assert Ok(count_list) = dict.get(parts, "joltages") |
| 71 | + let assert Ok(count_string) = list.first(count_list) |
| 72 | + let joltages = parse_joltages(count_string) |
| 73 | + |
| 74 | + echo joltages |
| 75 | + let assert Ok(button_strings) = dict.get(parts, "buttons") |
| 76 | + let buttons = |
| 77 | + button_strings |
| 78 | + |> list.map(fn(one_button) { |
| 79 | + parse_button(one_button) |> button_to_list(list.length(joltages)) |
| 80 | + }) |
| 81 | + |
| 82 | + use cache <- memoize.with_cache() |
| 83 | + |
| 84 | + let queue = |
| 85 | + list.fold(buttons, deque.new(), fn(acc, b) { |
| 86 | + deque.push_back(acc, #(b, 1)) |
| 87 | + }) |
| 88 | + |
| 89 | + find_p2_solution(cache, queue, buttons, joltages) |
| 90 | + |> echo |
| 91 | + }) |
| 92 | + |> int.sum |
| 93 | + |> int.to_string |
| 94 | + |> Ok |
| 95 | +} |
| 96 | + |
| 97 | +type Button { |
| 98 | + Button(wires: List(Int)) |
| 99 | +} |
| 100 | + |
| 101 | +fn parse(line: String) -> dict.Dict(String, List(String)) { |
| 102 | + string.split(line, " ") |
| 103 | + |> list.group(fn(part) { |
| 104 | + case string.first(part) { |
| 105 | + Ok("[") -> "lights" |
| 106 | + Ok("(") -> "buttons" |
| 107 | + Ok("{") -> "joltages" |
| 108 | + _ -> "unknown" |
| 109 | + } |
| 110 | + }) |
| 111 | +} |
| 112 | + |
| 113 | +fn parse_lights(part: String) -> set.Set(Int) { |
| 114 | + part |
| 115 | + |> string.drop_start(1) |
| 116 | + |> string.drop_end(1) |
| 117 | + |> string.to_graphemes |
| 118 | + |> list.index_fold([], fn(illuminated, char, i) { |
| 119 | + case char { |
| 120 | + "." -> illuminated |
| 121 | + "#" -> [i, ..illuminated] |
| 122 | + _ -> panic as "unexpected character in lights string" |
| 123 | + } |
| 124 | + }) |
| 125 | + |> set.from_list |
| 126 | +} |
| 127 | + |
| 128 | +fn parse_button(part: String) -> Button { |
| 129 | + part |
| 130 | + |> string.drop_start(1) |
| 131 | + |> string.drop_end(1) |
| 132 | + |> string.split(",") |
| 133 | + |> list.map(int.parse) |
| 134 | + |> result.values |
| 135 | + |> Button |
| 136 | +} |
| 137 | + |
| 138 | +fn parse_joltages(part: String) -> List(Int) { |
| 139 | + part |
| 140 | + |> string.drop_start(1) |
| 141 | + |> string.drop_end(1) |
| 142 | + |> string.split(",") |
| 143 | + |> list.map(int.parse) |
| 144 | + |> result.values |
| 145 | +} |
| 146 | + |
| 147 | +// Press a list of buttons and output which lights are on |
| 148 | +fn press_buttons(buttons: List(Button)) -> set.Set(Int) { |
| 149 | + buttons |
| 150 | + |> list.fold(dict.new(), fn(acc, button) { |
| 151 | + list.fold(button.wires, acc, fn(acc_inner, wire) { |
| 152 | + dict.upsert(acc_inner, wire, fn(v) { |
| 153 | + case v { |
| 154 | + option.None -> 1 |
| 155 | + option.Some(count) -> count + 1 |
| 156 | + } |
| 157 | + }) |
| 158 | + }) |
| 159 | + }) |
| 160 | + |> dict.to_list |
| 161 | + |> list.filter_map(fn(tup) { |
| 162 | + case tup.1 % 2 { |
| 163 | + 0 -> Error(Nil) |
| 164 | + 1 -> Ok(tup.0) |
| 165 | + _ -> panic as "remainder has to be 0 or 1" |
| 166 | + } |
| 167 | + }) |
| 168 | + |> set.from_list |
| 169 | +} |
| 170 | + |
| 171 | +fn button_to_list(button: Button, length: Int) -> List(Int) { |
| 172 | + let wires = set.from_list(button.wires) |
| 173 | + |
| 174 | + list.range(0, length - 1) |
| 175 | + |> list.fold([], fn(acc, v) { |
| 176 | + case set.contains(wires, v) { |
| 177 | + True -> [1, ..acc] |
| 178 | + False -> [0, ..acc] |
| 179 | + } |
| 180 | + }) |
| 181 | + |> list.reverse |
| 182 | +} |
| 183 | + |
| 184 | +fn list_add(left: List(Int), right: List(Int)) -> List(Int) { |
| 185 | + let assert Ok(pairs) = list.strict_zip(left, right) |
| 186 | + list.map(pairs, fn(tup) { tup.0 + tup.1 }) |
| 187 | +} |
| 188 | + |
| 189 | +fn list_subtract(left: List(Int), right: List(Int)) -> List(Int) { |
| 190 | + let assert Ok(pairs) = list.strict_zip(left, right) |
| 191 | + list.map(pairs, fn(tup) { tup.0 - tup.1 }) |
| 192 | +} |
| 193 | + |
| 194 | +fn find_p2_solution( |
| 195 | + cache: memoize.Cache(List(Int), Int), |
| 196 | + queue: deque.Deque(#(List(Int), Int)), |
| 197 | + buttons: List(List(Int)), |
| 198 | + joltages: List(Int), |
| 199 | +) -> Int { |
| 200 | + case deque.pop_front(queue) { |
| 201 | + Error(Nil) -> panic as "no solution found" |
| 202 | + Ok(#(#(current, pushes), new_deque)) -> { |
| 203 | + let new_sums = |
| 204 | + buttons |
| 205 | + |> list.map(list_add(_, current)) |
| 206 | + |
| 207 | + let remains = |
| 208 | + list.map(new_sums, list_subtract(joltages, _)) |
| 209 | + // remove negatives |
| 210 | + |> list.filter(fn(l) { |
| 211 | + list.fold_until(l, True, fn(_, element) { |
| 212 | + case element < 0 { |
| 213 | + True -> list.Stop(False) |
| 214 | + False -> list.Continue(True) |
| 215 | + } |
| 216 | + }) |
| 217 | + }) |
| 218 | + |
| 219 | + // check if you found it |
| 220 | + case list.contains(remains, list.repeat(0, list.length(joltages))) { |
| 221 | + True -> pushes + 1 |
| 222 | + False -> { |
| 223 | + // check if the cache has a way to complete this value |
| 224 | + let cache_result = |
| 225 | + remains |
| 226 | + |> list.map(fn(k) { process.call(cache, 100, memoize.Get(_, k)) }) |
| 227 | + |> list.fold_until(0, fn(_, cache_value) { |
| 228 | + case cache_value { |
| 229 | + Ok(v) -> list.Stop(v + pushes + 1) |
| 230 | + Error(Nil) -> list.Continue(0) |
| 231 | + } |
| 232 | + }) |
| 233 | + |
| 234 | + case cache_result { |
| 235 | + 0 -> { |
| 236 | + // add results to cache, queue, and keep going |
| 237 | + list.each(new_sums, fn(sum) { |
| 238 | + process.send(cache, memoize.Put(sum, pushes + 1)) |
| 239 | + }) |
| 240 | + |
| 241 | + let new_deque = |
| 242 | + list.fold(new_sums, new_deque, fn(acc, sum) { |
| 243 | + deque.push_back(acc, #(sum, pushes + 1)) |
| 244 | + }) |
| 245 | + |
| 246 | + find_p2_solution(cache, new_deque, buttons, joltages) |
| 247 | + } |
| 248 | + v -> v |
| 249 | + } |
| 250 | + } |
| 251 | + } |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments