-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (116 loc) · 3.31 KB
/
main.go
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
package main
import (
"cristianrb2015/io"
"sort"
"strconv"
"strings"
)
func main() {
lines, _ := io.ReadLines("Day-07/inputs/input_01")
wires := runCircuit(lines)
println(wires["a"])
}
// Could be improved a lot.
func runCircuit(input []string) map[string]uint16 {
wires := map[string]uint16{}
for len(input) > 0 {
idxToDel := []int{}
for i, line := range input {
if strings.Contains(line, "AND") {
op1, op2, out, valid := andArguments(line, wires)
if valid {
wires[out] = op1 & op2
idxToDel = append(idxToDel, i)
}
} else if strings.Contains(line, "OR") {
arguments := strings.Split(line, " OR ")
first := arguments[0]
second := strings.Split(arguments[1], " -> ")[0]
result := strings.Split(arguments[1], " -> ")[1]
_, fExist := wires[first]
_, sExist := wires[second]
if fExist && sExist {
wires[result] = wires[first] | wires[second]
idxToDel = append(idxToDel, i)
}
} else if strings.Contains(line, "LSHIFT") {
arguments := strings.Split(line, " LSHIFT ")
first := arguments[0]
_, fExist := wires[first]
if fExist {
second, _ := strconv.Atoi(strings.Split(arguments[1], " -> ")[0])
result := strings.Split(arguments[1], " -> ")[1]
wires[result] = wires[first] << second
idxToDel = append(idxToDel, i)
}
} else if strings.Contains(line, "RSHIFT") {
arguments := strings.Split(line, " RSHIFT ")
first := arguments[0]
_, fExist := wires[first]
if fExist {
second, _ := strconv.Atoi(strings.Split(arguments[1], " -> ")[0])
result := strings.Split(arguments[1], " -> ")[1]
wires[result] = wires[first] >> second
idxToDel = append(idxToDel, i)
}
} else if strings.Contains(line, "NOT") {
arguments := strings.Split(line, "NOT ")
first := strings.Split(arguments[1], " -> ")[0]
_, fExist := wires[first]
if fExist {
result := strings.Split(arguments[1], " -> ")[1]
wires[result] = ^wires[first]
idxToDel = append(idxToDel, i)
}
} else {
arguments := strings.Split(line, " -> ")
number, err := strconv.Atoi(arguments[0])
if err != nil {
_, fExist := wires[arguments[0]]
if fExist {
wires[arguments[1]] = wires[arguments[0]]
idxToDel = append(idxToDel, i)
}
} else {
wires[arguments[1]] = uint16(number)
idxToDel = append(idxToDel, i)
}
}
}
sort.Slice(idxToDel, func(i, j int) bool {
return idxToDel[i] > idxToDel[j]
})
for _, idx := range idxToDel {
input = remove(input, idx)
}
}
return wires
}
func andArguments(line string, wires map[string]uint16) (uint16, uint16, string, bool) {
arguments := strings.Split(line, " AND ")
first := arguments[0]
second := strings.Split(arguments[1], " -> ")[0]
result := strings.Split(arguments[1], " -> ")[1]
_, fExist := wires[first]
_, sExist := wires[second]
if fExist && sExist {
return wires[first], wires[second], result, true
} else {
if fExist {
secondNum, err := strconv.Atoi(second)
if err == nil {
return wires[first], uint16(secondNum), result, true
}
} else if sExist {
firstNum, err := strconv.Atoi(first)
if err == nil {
return uint16(firstNum), wires[second], result, true
}
}
}
return 0, 0, "0", false
}
func remove(s []string, i int) []string {
s[i] = s[len(s)-1]
return s[:len(s)-1]
}