-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday24.go
More file actions
140 lines (116 loc) · 3.03 KB
/
day24.go
File metadata and controls
140 lines (116 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"AdventOfCode-go/advent2024/utils"
"fmt"
"sort"
"strconv"
"strings"
)
var Debug bool
type GateStruct struct {
operation string
inputWire1 string
inputWire2 string
outputWire string
}
func canOperate(gateLine GateStruct, wires map[string]int) bool {
if _, ok1 := wires[gateLine.inputWire1]; ok1 {
if _, ok2 := wires[gateLine.inputWire2]; ok2 {
return true
}
}
return false
}
func combineWires(gateLine GateStruct, wires map[string]int) int {
switch gateLine.operation {
case "AND":
if (wires[gateLine.inputWire1] == 1) && (wires[gateLine.inputWire2] == 1) {
return 1
}
return 0
case "OR":
if (wires[gateLine.inputWire1] == 1) || (wires[gateLine.inputWire2] == 1) {
return 1
}
return 0
case "XOR":
if wires[gateLine.inputWire1] != wires[gateLine.inputWire2] {
return 1
}
return 0
}
return 0
}
func day24(filename string, part byte) int {
var result int
//Puzzle Input:
//- line x: Start value for an x or y wire <x|y><wire-num (2 digits)>: <value (1 digit)>
//- line y: <blank line<
//- line Y+1 onwards: wire combinations and destination
// e.g. tnw OR fst -> frj
// 3 different operators: AND, OR, XOR
wires := make(map[string]int) // Hold the current value of calculated wires
gates := make(map[string]GateStruct) // Hold the instructions for calculating wires
puzzleInput, _ := utils.ReadFile(filename)
// Process the input file
var startValueSection bool = true
for _, i := range puzzleInput {
if len(i) == 0 {
startValueSection = false
continue
}
if startValueSection {
// Start values for wires
tempStrings := strings.Split(i, ": ")
tempWire := tempStrings[0]
tempValue, _ := strconv.Atoi(strings.TrimSpace(tempStrings[1]))
wires[tempWire] = tempValue
} else {
// wire instructions section
tempInstructions := strings.Split(i, " -> ")
tempLeft := strings.Split(tempInstructions[0], " ")
gates[tempInstructions[1]] = GateStruct{operation: tempLeft[1],
inputWire1: tempLeft[0],
inputWire2: tempLeft[2]}
}
}
if Debug {
fmt.Println(wires)
fmt.Println(gates)
}
// How do I decide if a gate instruction has already been used? Bool in the struct?
// Or delete the entry from the map?
for len(gates) > 0 {
for currWire, gateLine := range gates {
if canOperate(gateLine, wires) {
wires[currWire] = combineWires(gateLine, wires)
delete(gates, currWire)
}
}
}
// Now need to collect ONLY the Zxx wires for our result
resultWires := make([]string, 0)
for wire := range wires {
if wire[0] == 'z' {
resultWires = append(resultWires, wire)
}
}
sort.Strings(resultWires)
if Debug {
fmt.Println(resultWires)
}
for i := len(resultWires) - 1; i >= 0; i-- {
result = (result << 1) + wires[resultWires[i]]
}
return result
}
// Main routine
func main() {
filenamePtr, execPart, debug := utils.CatchUserInput()
Debug = debug
if execPart == 'z' {
fmt.Println("Bad part choice. Available choices are 'a' and 'b'")
} else {
fmt.Printf("Result is: %d\n", day24(filenamePtr, execPart))
}
}