|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "os" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +func main() { |
| 12 | + facts, rules := parseInput() |
| 13 | + |
| 14 | + // GenerateDot(rules) |
| 15 | + // return |
| 16 | + |
| 17 | + // After generating the dot file, I could generate an image with |
| 18 | + // |
| 19 | + // dot -Tpng -O graph.dot |
| 20 | + // |
| 21 | + // and thanks to the colors of the arrows, it was easy to spot inconsistencies. |
| 22 | + // E.g. |
| 23 | + // All zXX were reached by two red arrows (a XOR operation). |
| 24 | + // `z13` was reached by two green, meaning it needed to be swapped with a node |
| 25 | + // that is reached by two red arrows instead of green. |
| 26 | + // `z19` and `z33` were incorrectly blue. |
| 27 | + // |
| 28 | + // The full list was: |
| 29 | + // - z13 green (supposed red) |
| 30 | + // - z19 blue (supposed red) |
| 31 | + // - z33 blue (supposed red) |
| 32 | + // - gws blue (supposed red) |
| 33 | + // - hgj red (supposed blue) |
| 34 | + // - npf red (supposed green) |
| 35 | + // - nnt red (supposed blue) |
| 36 | + // - cph red (supposed blue) |
| 37 | + |
| 38 | + list := []string{"z13", "z19", "z33", "npf", "hgj", "gws", "nnt", "cph"} |
| 39 | + sort.Strings(list) |
| 40 | + fmt.Println(strings.Join(list, ",")) |
| 41 | + |
| 42 | + return |
| 43 | + |
| 44 | + // The assignment didn't ask to fix the gates (so the list above is enough |
| 45 | + // to generate the solution: |
| 46 | + // |
| 47 | + // Anyway it's pretty easy to spot the pairs: |
| 48 | + // - z13 <> npf (the only green/red pair) |
| 49 | + // - gws <> nnt (they are both in the z09 area: a different swap would change the graph massively) |
| 50 | + // - z19 <> cph (they are both in the z19 area) |
| 51 | + // - z33 <> hgj (they are both in the z33 area) |
| 52 | + |
| 53 | + swap("z13", "npf", rules) |
| 54 | + swap("z33", "hgj", rules) |
| 55 | + swap("gws", "nnt", rules) |
| 56 | + swap("z19", "cph", rules) |
| 57 | + x := GetNumber("x", facts, rules) |
| 58 | + y := GetNumber("y", facts, rules) |
| 59 | + z := GetNumber("z", facts, rules) |
| 60 | + fmt.Printf(" x: %d +\n y: %d =\nx+y: %d\n z: %d\n", x, y, x+y, z) |
| 61 | + fmt.Printf("Sum is working = %t\n", x+y == z) |
| 62 | + |
| 63 | + // x: 22423510084409 + |
| 64 | + // y: 19634559019535 = |
| 65 | + // x+y: 42058069103944 |
| 66 | + // z: 42058069103944 |
| 67 | + // Sum is working = true |
| 68 | +} |
| 69 | + |
| 70 | +func swap(r1, r2 string, rules map[string]Rule) { |
| 71 | + rules[r1], rules[r2] = rules[r2], rules[r1] |
| 72 | +} |
| 73 | + |
| 74 | +func GenerateDot(rules map[string]Rule) { |
| 75 | + f, err := os.Create("graph.dot") |
| 76 | + if err != nil { |
| 77 | + log.Fatal(err) |
| 78 | + } |
| 79 | + defer f.Close() |
| 80 | + |
| 81 | + const Size = 45 |
| 82 | + fmt.Fprint(f, "digraph {\n") |
| 83 | + for rName, r := range rules { |
| 84 | + var color string |
| 85 | + switch r.Op { |
| 86 | + case "XOR": |
| 87 | + color = "red" |
| 88 | + case "AND": |
| 89 | + color = "blue" |
| 90 | + case "OR": |
| 91 | + color = "green" |
| 92 | + } |
| 93 | + |
| 94 | + fmt.Fprintf(f, "%s -> %s [color=\"%s\"];\n", r.Left, rName, color) |
| 95 | + fmt.Fprintf(f, "%s -> %s [color=\"%s\"];\n", r.Right, rName, color) |
| 96 | + } |
| 97 | + |
| 98 | + var xs, ys, zs []string |
| 99 | + for i := 0; i < Size; i++ { |
| 100 | + xs = append(xs, fmt.Sprintf("x%02d", i)) |
| 101 | + ys = append(ys, fmt.Sprintf("y%02d", i)) |
| 102 | + zs = append(zs, fmt.Sprintf("z%02d", i)) |
| 103 | + } |
| 104 | + fmt.Fprintf(f, "{rank = min;\n %s ; \n};\n", strings.Join(xs, " -> ")) |
| 105 | + fmt.Fprintf(f, "{rank = min;\n %s ; \n};\n", strings.Join(ys, " -> ")) |
| 106 | + fmt.Fprintf(f, "{rank = max;\n %s ; \n};\n", strings.Join(zs, " -> ")) |
| 107 | + fmt.Fprint(f, "}\n") |
| 108 | +} |
0 commit comments