|
| 1 | +import gleam/bool |
1 | 2 | import gleam/dict.{type Dict}
|
2 | 3 | import gleam/int
|
3 | 4 | import gleam/io
|
@@ -46,8 +47,52 @@ pub fn solve_p1(lines: List(String)) -> Result(String, String) {
|
46 | 47 | // Part 2
|
47 | 48 | // Easiest way to do this was to do some sorting and use editor macros to pull
|
48 | 49 | // together the relevant half adder and full adders into groups.
|
49 |
| -pub fn solve_p2(_lines: List(String)) -> Result(String, String) { |
50 |
| - Ok("solved by hand in inputs/day24-resolved.txt") |
| 50 | +// I then went looking at results and tried to implement a solution based on |
| 51 | +// this explanation: https://old.reddit.com/r/adventofcode/comments/1hla5ql/2024_day_24_part_2_a_guide_on_the_idea_behind_the/ |
| 52 | +// and from a really great pattern matching example in the Gleam discord by |
| 53 | +// super makioka sisters. This is essentially a copy of their example which |
| 54 | +// I implemented in order to better understand it. |
| 55 | +pub fn solve_p2(lines: List(String)) -> Result(String, String) { |
| 56 | + use #(_, gates) <- result.map(parse(lines)) |
| 57 | + |
| 58 | + // Take the gates and create a dict by output |
| 59 | + gates |
| 60 | + |> list.map(fn(g) { #(g.output, g) }) |
| 61 | + |> dict.from_list |
| 62 | + |> dict.filter(fn(k, v) { |
| 63 | + // filter the dictionary for gates that do not match a half adder followed by |
| 64 | + // a bunch of full adders. |
| 65 | + case k, v { |
| 66 | + // Outputs should be from Xor gates, except the last one |
| 67 | + "z45", Or(_, _, _) -> False |
| 68 | + "z" <> _, Xor(_, _, _) -> False |
| 69 | + "z" <> _, _ -> True |
| 70 | + |
| 71 | + // Xor gates for inputs should connect to AND and XOR gates, but not OR gates |
| 72 | + _, Xor("x" <> _, "y" <> _, _) | _, Xor("y" <> _, "x" <> _, _) -> { |
| 73 | + find_gates_with_input(gates, k) |
| 74 | + |> has_or_gates |
| 75 | + } |
| 76 | + |
| 77 | + // Any other Xor gate is not valid |
| 78 | + _, Xor(_, _, _) -> True |
| 79 | + |
| 80 | + // And gates should have Or gates after them, except for the |
| 81 | + // output of the half-adder. |
| 82 | + // This assumes the output of the And from the first half adder |
| 83 | + // is not swapped. |
| 84 | + _, And("x00", _, _) | _, And(_, "x00", _) -> False |
| 85 | + _, And(_, _, _) -> { |
| 86 | + find_gates_with_input(gates, k) |
| 87 | + |> has_or_gates |
| 88 | + |> bool.negate |
| 89 | + } |
| 90 | + _, _ -> False |
| 91 | + } |
| 92 | + }) |
| 93 | + |> dict.keys |
| 94 | + |> list.sort(string.compare) |
| 95 | + |> string.join(",") |
51 | 96 | }
|
52 | 97 |
|
53 | 98 | type Gate {
|
@@ -151,3 +196,22 @@ fn get_number(wires: Dict(String, Int), prefix: String) -> Int {
|
151 | 196 | |> list.sort(fn(tupa, tupb) { string.compare(tupb.0, tupa.0) })
|
152 | 197 | |> list.fold(0, fn(value, tup) { { value * 2 } + tup.1 })
|
153 | 198 | }
|
| 199 | + |
| 200 | +fn find_gates_with_input(gates: List(Gate), input: String) -> List(Gate) { |
| 201 | + gates |
| 202 | + |> list.filter(fn(g) { |
| 203 | + case g.input1, g.input2 { |
| 204 | + w, _ if w == input -> True |
| 205 | + _, w if w == input -> True |
| 206 | + _, _ -> False |
| 207 | + } |
| 208 | + }) |
| 209 | +} |
| 210 | + |
| 211 | +fn has_or_gates(gates: List(Gate)) -> Bool { |
| 212 | + case gates { |
| 213 | + [Or(_, _, _), ..] -> True |
| 214 | + [_, ..rest] -> has_or_gates(rest) |
| 215 | + [] -> False |
| 216 | + } |
| 217 | +} |
0 commit comments