|
| 1 | +import gleam/dict |
| 2 | +import gleam/int |
| 3 | +import gleam/io |
| 4 | +import gleam/list |
| 5 | +import gleam/result |
| 6 | +import gleam/set |
| 7 | +import gleam/string |
| 8 | +import internal/aoc_utils |
| 9 | +import internal/point |
| 10 | + |
| 11 | +pub fn main() { |
| 12 | + let filename = "inputs/day12.txt" |
| 13 | + |
| 14 | + let lines_result = aoc_utils.read_lines(from: filename) |
| 15 | + case lines_result { |
| 16 | + Ok(lines) -> { |
| 17 | + // If the file was converting into a list of lines |
| 18 | + // successfully then run each part of the problem |
| 19 | + aoc_utils.run_part_and_print("Part 1", fn() { solve_p1(lines) }) |
| 20 | + } |
| 21 | + Error(_) -> io.println("Error reading file") |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Part 1 |
| 26 | +pub fn solve_p1(lines: List(String)) -> Result(String, String) { |
| 27 | + let sections = aoc_utils.chunk_around_empty_strings(lines) |
| 28 | + |
| 29 | + let #(present_input, requirements_input) = |
| 30 | + list.split(sections, list.length(sections) - 1) |
| 31 | + |
| 32 | + let presents = |
| 33 | + present_input |
| 34 | + |> list.map(parse_present) |
| 35 | + |> dict.from_list |
| 36 | + |
| 37 | + let requirements = |
| 38 | + requirements_input |
| 39 | + |> list.flatten |
| 40 | + |> list.map(parse_requirements) |
| 41 | + |
| 42 | + requirements |
| 43 | + |> list.fold_until(Ok(0), fn(fit_count, r) { |
| 44 | + case fit_count { |
| 45 | + Ok(c) -> |
| 46 | + case definite_fit(r), definite_notfit(r, presents) { |
| 47 | + True, _ -> list.Continue(Ok(c + 1)) |
| 48 | + _, True -> list.Continue(Ok(c)) |
| 49 | + _, _ -> list.Stop(Error("Marginal fitting case detected")) |
| 50 | + } |
| 51 | + _ -> list.Stop(Error("Marginal fitting case detected")) |
| 52 | + } |
| 53 | + }) |
| 54 | + |> result.map(int.to_string) |
| 55 | +} |
| 56 | + |
| 57 | +fn parse_present(lines: List(String)) -> #(Int, set.Set(point.Point)) { |
| 58 | + let assert [index_line, ..shape_lines] = lines |
| 59 | + |
| 60 | + let assert Ok(present_index) = |
| 61 | + string.replace(index_line, ":", "") |> int.parse |
| 62 | + |
| 63 | + let present = |
| 64 | + shape_lines |
| 65 | + |> list.index_fold(set.new(), fn(acc, line, y) { |
| 66 | + string.to_graphemes(line) |
| 67 | + |> list.index_fold(acc, fn(acc, character, x) { |
| 68 | + case character { |
| 69 | + "#" -> set.insert(acc, #(x, y)) |
| 70 | + _ -> acc |
| 71 | + } |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + #(present_index, present) |
| 76 | +} |
| 77 | + |
| 78 | +fn parse_requirements(line: String) -> #(point.Point, List(Int)) { |
| 79 | + let assert Ok(#(dimensions, present_counts)) = string.split_once(line, ": ") |
| 80 | + let assert Ok(#(x_string, y_string)) = string.split_once(dimensions, "x") |
| 81 | + let counts = |
| 82 | + string.split(present_counts, " ") |> list.map(int.parse) |> result.values |
| 83 | + |
| 84 | + let assert Ok(x) = int.parse(x_string) |
| 85 | + let assert Ok(y) = int.parse(y_string) |
| 86 | + #(#(x, y), counts) |
| 87 | +} |
| 88 | + |
| 89 | +fn definite_fit(requirements: #(point.Point, List(Int))) -> Bool { |
| 90 | + let area = requirements.0.0 * requirements.0.1 |
| 91 | + |
| 92 | + int.sum(requirements.1) * 9 <= area |
| 93 | +} |
| 94 | + |
| 95 | +fn definite_notfit( |
| 96 | + requirements: #(point.Point, List(Int)), |
| 97 | + presents: dict.Dict(Int, set.Set(point.Point)), |
| 98 | +) -> Bool { |
| 99 | + let area = requirements.0.0 * requirements.0.1 |
| 100 | + |
| 101 | + requirements.1 |
| 102 | + |> list.index_fold(0, fn(present_area, present_count, idx) { |
| 103 | + let assert Ok(p) = dict.get(presents, idx) |
| 104 | + present_area + { present_count * set.size(p) } |
| 105 | + }) |
| 106 | + > area |
| 107 | +} |
0 commit comments