|
| 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 | + |
| 10 | +pub fn main() { |
| 11 | + let filename = "inputs/day08.txt" |
| 12 | + |
| 13 | + let lines_result = aoc_utils.read_lines(from: filename) |
| 14 | + case lines_result { |
| 15 | + Ok(lines) -> { |
| 16 | + // If the file was converting into a list of lines |
| 17 | + // successfully then run each part of the problem |
| 18 | + aoc_utils.run_part_and_print("Part 1", fn() { solve_p1(lines, 1000) }) |
| 19 | + aoc_utils.run_part_and_print("Part 2", fn() { solve_p2(lines) }) |
| 20 | + } |
| 21 | + Error(_) -> io.println("Error reading file") |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// Part 1 |
| 26 | +pub fn solve_p1(lines: List(String), connections: Int) -> Result(String, String) { |
| 27 | + let boxes = |
| 28 | + lines |
| 29 | + |> list.map(parse_line) |
| 30 | + |
| 31 | + let closest_boxes = |
| 32 | + boxes |
| 33 | + |> list.combinations(2) |
| 34 | + |> list.map(fn(pair) { |
| 35 | + case pair { |
| 36 | + [b1, b2] -> #(#(b1, b2), dsq_boxes(b1, b2)) |
| 37 | + _ -> panic as "expected a pair of junctions" |
| 38 | + } |
| 39 | + }) |
| 40 | + |> list.sort(fn(v1, v2) { int.compare(v1.1, v2.1) }) |
| 41 | + |> list.take(connections) |
| 42 | + |> list.map(fn(t) { t.0 }) |
| 43 | + |
| 44 | + // This table has junction boxes as keys and the circuits they make |
| 45 | + // as a set of junction boxes as a value. They all start with just |
| 46 | + // themselves. |
| 47 | + let circuit_table = |
| 48 | + boxes |
| 49 | + |> list.map(fn(box) { #(box, set.new() |> set.insert(box)) }) |
| 50 | + |> dict.from_list |
| 51 | + |
| 52 | + wire(circuit_table, closest_boxes) |
| 53 | + |> dict.to_list |
| 54 | + |> list.map(fn(dtup) { dtup.1 }) |
| 55 | + |> list.filter(fn(circuit) { set.size(circuit) > 1 }) |
| 56 | + |> set.from_list |
| 57 | + |> set.to_list |
| 58 | + |> list.map(set.size) |
| 59 | + |> list.sort(int.compare) |
| 60 | + |> list.reverse |
| 61 | + |> list.take(3) |
| 62 | + |> int.product |
| 63 | + |> int.to_string |
| 64 | + |> Ok |
| 65 | +} |
| 66 | + |
| 67 | +// Part 2 |
| 68 | +pub fn solve_p2(lines: List(String)) -> Result(String, String) { |
| 69 | + let boxes = |
| 70 | + lines |
| 71 | + |> list.map(parse_line) |
| 72 | + |
| 73 | + let closest_boxes = |
| 74 | + boxes |
| 75 | + |> list.combinations(2) |
| 76 | + |> list.map(fn(pair) { |
| 77 | + case pair { |
| 78 | + [b1, b2] -> #(#(b1, b2), dsq_boxes(b1, b2)) |
| 79 | + _ -> panic as "expected a pair of junctions" |
| 80 | + } |
| 81 | + }) |
| 82 | + |> list.sort(fn(v1, v2) { int.compare(v1.1, v2.1) }) |
| 83 | + |> list.map(fn(t) { t.0 }) |
| 84 | + |
| 85 | + // This table has junction boxes as keys and the circuits they make |
| 86 | + // as a set of junction boxes as a value. They all start with just |
| 87 | + // themselves. |
| 88 | + let circuit_table = |
| 89 | + boxes |
| 90 | + |> list.map(fn(box) { #(box, set.new() |> set.insert(box)) }) |
| 91 | + |> dict.from_list |
| 92 | + |
| 93 | + build_circuit_sized(dict.size(circuit_table), circuit_table, closest_boxes) |
| 94 | + |> fn(v) { { v.0 }.x * { v.1 }.x } |
| 95 | + |> int.to_string |
| 96 | + |> Ok |
| 97 | +} |
| 98 | + |
| 99 | +type Junction { |
| 100 | + Junction(x: Int, y: Int, z: Int) |
| 101 | +} |
| 102 | + |
| 103 | +fn dsq_boxes(b1: Junction, b2: Junction) -> Int { |
| 104 | + [b1.x - b2.x, b1.y - b2.y, b1.z - b2.z] |
| 105 | + |> list.map(fn(v) { v * v }) |
| 106 | + |> int.sum |
| 107 | +} |
| 108 | + |
| 109 | +fn parse_line(line: String) -> Junction { |
| 110 | + let assert [x, y, z] = |
| 111 | + line |> string.split(",") |> list.map(int.parse) |> result.values |
| 112 | + Junction(x:, y:, z:) |
| 113 | +} |
| 114 | + |
| 115 | +// End up making a dictionary of all the junction boxes with the circuit |
| 116 | +// they are part of. The circuit is a set of all involved junction boxes. |
| 117 | +// When I combine two boxes I have to update the circuit of all involed |
| 118 | +// junctions. |
| 119 | +fn wire( |
| 120 | + circuit_table: dict.Dict(Junction, set.Set(Junction)), |
| 121 | + connections: List(#(Junction, Junction)), |
| 122 | +) -> dict.Dict(Junction, set.Set(Junction)) { |
| 123 | + list.fold(connections, circuit_table, fn(acc, conn) { |
| 124 | + let assert Ok(s1) = dict.get(acc, conn.0) |
| 125 | + let assert Ok(s2) = dict.get(acc, conn.1) |
| 126 | + |
| 127 | + let combined = set.union(s1, s2) |
| 128 | + |
| 129 | + set.fold(combined, acc, fn(acc_inner, jb) { |
| 130 | + dict.insert(acc_inner, jb, combined) |
| 131 | + }) |
| 132 | + }) |
| 133 | +} |
| 134 | + |
| 135 | +// Just keep combining until the set representing the circuit |
| 136 | +// has the desired size. Then output the junction boxes of the |
| 137 | +// connection that achieved that size. |
| 138 | +fn build_circuit_sized( |
| 139 | + size: Int, |
| 140 | + circuit_table: dict.Dict(Junction, set.Set(Junction)), |
| 141 | + connections: List(#(Junction, Junction)), |
| 142 | +) -> #(Junction, Junction) { |
| 143 | + case connections { |
| 144 | + [] -> panic as "unable to build requested circuit size" |
| 145 | + [first, ..rest] -> { |
| 146 | + let assert Ok(s1) = dict.get(circuit_table, first.0) |
| 147 | + let assert Ok(s2) = dict.get(circuit_table, first.1) |
| 148 | + |
| 149 | + let combined = set.union(s1, s2) |
| 150 | + |
| 151 | + case set.size(combined) { |
| 152 | + s if s == size -> first |
| 153 | + _ -> { |
| 154 | + let new_table = |
| 155 | + set.fold(combined, circuit_table, fn(acc, jb) { |
| 156 | + dict.insert(acc, jb, combined) |
| 157 | + }) |
| 158 | + build_circuit_sized(size, new_table, rest) |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments