Skip to content

Commit 9853a78

Browse files
2024: Visualize day 24
1 parent 537269d commit 9853a78

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

2024/day24/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
graph_broken.dot
2+
graph_broken.svg
3+
graph_broken_disconnected.dot
4+
graph_broken_disconnected.svg
5+
graph_fixed.dot
6+
graph_fixed.svg
7+
graph_fixed_disconnected.dot
8+
graph_fixed_disconnected.svg

2024/day24/src/main.rs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ fn get_value(wires: &HashMap<&str, bool>, prefix: &str) -> u64 {
4848
/// Convert dot file into svg:
4949
///
5050
/// dot -Tsvg graph.dot -o graph.svg
51-
#[allow(unused)]
52-
fn write_dot_file(gates: &[Gate<'_>]) {
53-
let mut writer = BufWriter::new(File::create("graph.dot").unwrap());
51+
fn write_dot_file(gates: &[Gate<'_>], filename: &str, disconnect_carry_bits: bool) {
52+
let mut writer = BufWriter::new(File::create(filename).unwrap());
5453

5554
let logic_per_wire = gates
5655
.iter()
5756
.map(|g| (g.out, g.logic))
5857
.collect::<HashMap<_, _>>();
5958

60-
writer.write_all(b"digraph {{\n").unwrap();
59+
writer.write_all(b"digraph {\n").unwrap();
6160

6261
// write all nodes and colorize them according to their gate logic
6362
for lpw in &logic_per_wire {
@@ -67,21 +66,23 @@ fn write_dot_file(gates: &[Gate<'_>]) {
6766
Logic::Xor => "red",
6867
};
6968
writer
70-
.write_all(format!("{} [style=filled,fillcolor={}];", lpw.0, color).as_bytes())
69+
.write_all(format!("{} [style=filled,fillcolor={}];\n", lpw.0, color).as_bytes())
7170
.unwrap();
7271
}
7372

7473
// write all edges
7574
for g in gates {
76-
writer
77-
.write_all(format!("{} -> {};", g.a, g.out).as_bytes())
78-
.unwrap();
79-
writer
80-
.write_all(format!("{} -> {};", g.b, g.out).as_bytes())
81-
.unwrap();
75+
if !disconnect_carry_bits || g.logic != Logic::Or || g.out.starts_with("z") {
76+
writer
77+
.write_all(format!("{} -> {};\n", g.a, g.out).as_bytes())
78+
.unwrap();
79+
writer
80+
.write_all(format!("{} -> {};\n", g.b, g.out).as_bytes())
81+
.unwrap();
82+
}
8283
}
8384

84-
writer.write_all(b"}}").unwrap();
85+
writer.write_all(b"}").unwrap();
8586
}
8687

8788
fn run<'a>(
@@ -238,4 +239,22 @@ fn main() {
238239
} else {
239240
panic!("Unsolvable");
240241
}
242+
243+
// visualize broken graph
244+
write_dot_file(&gates, "graph_broken.dot", false);
245+
246+
// visualize broken graph and disconnect carry bits from their inputs (this
247+
// is what I used for debugging during the contest)
248+
write_dot_file(&gates, "graph_broken_disconnected.dot", true);
249+
250+
// visualize fixed graph
251+
let fixed_gates = gates
252+
.iter()
253+
.map(|g| {
254+
let out = renames.get(&g.out).unwrap_or(&g.out);
255+
Gate { out, ..*g }
256+
})
257+
.collect::<Vec<_>>();
258+
write_dot_file(&fixed_gates, "graph_fixed.dot", false);
259+
write_dot_file(&fixed_gates, "graph_fixed_disconnected.dot", true);
241260
}

0 commit comments

Comments
 (0)