|
| 1 | +import gleam/dict.{type Dict} |
| 2 | +import gleam/int |
| 3 | +import gleam/io |
| 4 | +import gleam/list |
| 5 | +import gleam/result |
| 6 | +import gleam/string |
| 7 | +import internal/aoc_utils |
| 8 | + |
| 9 | +pub fn main() { |
| 10 | + let filename = "inputs/day24.txt" |
| 11 | + |
| 12 | + let lines_result = aoc_utils.read_lines(from: filename) |
| 13 | + case lines_result { |
| 14 | + Ok(lines) -> { |
| 15 | + // If the file was converting into a list of lines |
| 16 | + // successfully then run each part of the problem |
| 17 | + aoc_utils.run_part_and_print("Part 1", fn() { solve_p1(lines) }) |
| 18 | + aoc_utils.run_part_and_print("Part 2", fn() { solve_p2(lines) }) |
| 19 | + } |
| 20 | + Error(_) -> io.println("Error reading file") |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +// Part 1 |
| 25 | +pub fn solve_p1(lines: List(String)) -> Result(String, String) { |
| 26 | + use #(wires, gates) <- result.map(parse(lines)) |
| 27 | + |
| 28 | + find_wire_values(wires, gates) |
| 29 | + |> get_number("z") |
| 30 | + |> int.to_string |
| 31 | +} |
| 32 | + |
| 33 | +// Part 2 |
| 34 | +pub fn solve_p2(lines: List(String)) -> Result(String, String) { |
| 35 | + use #(wires, gates) <- result.map(parse(lines)) |
| 36 | + |
| 37 | + get_number(wires, "x") |
| 38 | + |> int.to_base2 |
| 39 | + |> io.debug |
| 40 | + |
| 41 | + get_number(wires, "y") |
| 42 | + |> int.to_base2 |
| 43 | + |> io.debug |
| 44 | + |
| 45 | + find_wire_values(wires, gates) |
| 46 | + |> get_number("z") |
| 47 | + |> int.to_base2 |
| 48 | + |> io.debug |
| 49 | + todo |
| 50 | +} |
| 51 | + |
| 52 | +type Gate { |
| 53 | + And(input1: String, input2: String, output: String) |
| 54 | + Or(input1: String, input2: String, output: String) |
| 55 | + Xor(input1: String, input2: String, output: String) |
| 56 | +} |
| 57 | + |
| 58 | +fn parse( |
| 59 | + lines: List(String), |
| 60 | +) -> Result(#(Dict(String, Int), List(Gate)), String) { |
| 61 | + let assert [values, gates] = aoc_utils.chunk_around_empty_strings(lines) |
| 62 | + |
| 63 | + use wire_dict <- result.try(parse_values(values)) |
| 64 | + |
| 65 | + use gate_list <- result.map(parse_gates(gates)) |
| 66 | + |
| 67 | + #(wire_dict, gate_list) |
| 68 | +} |
| 69 | + |
| 70 | +fn parse_values(values: List(String)) -> Result(Dict(String, Int), String) { |
| 71 | + use value_list <- result.map({ |
| 72 | + values |
| 73 | + |> list.map(fn(line) { |
| 74 | + case string.split(line, ": ") { |
| 75 | + [wire, boolstring] -> |
| 76 | + case int.parse(boolstring) { |
| 77 | + Ok(n) -> Ok(#(wire, n)) |
| 78 | + Error(Nil) -> Error("Unable to parse wire value") |
| 79 | + } |
| 80 | + _ -> Error("Unexpected wire value line") |
| 81 | + } |
| 82 | + }) |
| 83 | + |> result.all |
| 84 | + }) |
| 85 | + |
| 86 | + dict.from_list(value_list) |
| 87 | +} |
| 88 | + |
| 89 | +fn parse_gates(gates: List(String)) -> Result(List(Gate), String) { |
| 90 | + gates |
| 91 | + |> list.map(fn(line) { |
| 92 | + case string.split(line, " ") { |
| 93 | + [i1, name, i2, _, o] -> { |
| 94 | + case name { |
| 95 | + "AND" -> Ok(And(i1, i2, o)) |
| 96 | + "OR" -> Ok(Or(i1, i2, o)) |
| 97 | + "XOR" -> Ok(Xor(i1, i2, o)) |
| 98 | + _ -> Error("Gate type not recognized") |
| 99 | + } |
| 100 | + } |
| 101 | + _ -> Error("Unexpected gate format") |
| 102 | + } |
| 103 | + }) |
| 104 | + |> result.all |
| 105 | +} |
| 106 | + |
| 107 | +fn find_wire_values( |
| 108 | + wires: Dict(String, Int), |
| 109 | + gates: List(Gate), |
| 110 | +) -> Dict(String, Int) { |
| 111 | + case gates { |
| 112 | + [] -> wires |
| 113 | + _ -> { |
| 114 | + let newdict = |
| 115 | + gates |
| 116 | + |> list.fold(wires, fn(d, gate) { |
| 117 | + case resolve_gate(gate, wires) { |
| 118 | + Ok(#(output, value)) -> { |
| 119 | + dict.insert(d, output, value) |
| 120 | + } |
| 121 | + _ -> d |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + find_wire_values( |
| 126 | + newdict, |
| 127 | + gates |> list.filter(fn(g) { !dict.has_key(newdict, g.output) }), |
| 128 | + ) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +fn resolve_gate( |
| 134 | + gate: Gate, |
| 135 | + wires: Dict(String, Int), |
| 136 | +) -> Result(#(String, Int), Nil) { |
| 137 | + use v1 <- result.try(dict.get(wires, gate.input1)) |
| 138 | + use v2 <- result.map(dict.get(wires, gate.input2)) |
| 139 | + |
| 140 | + case gate { |
| 141 | + And(_, _, o) -> #(o, int.bitwise_and(v1, v2)) |
| 142 | + Or(_, _, o) -> #(o, int.bitwise_or(v1, v2)) |
| 143 | + Xor(_, _, o) -> #(o, int.bitwise_exclusive_or(v1, v2)) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +fn get_number(wires: Dict(String, Int), prefix: String) -> Int { |
| 148 | + dict.to_list(wires) |
| 149 | + |> list.filter(fn(tup) { string.starts_with(tup.0, prefix) }) |
| 150 | + |> list.sort(fn(tupa, tupb) { string.compare(tupb.0, tupa.0) }) |
| 151 | + |> list.fold(0, fn(value, tup) { { value * 2 } + tup.1 }) |
| 152 | +} |
0 commit comments