-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
202 lines (187 loc) · 5.01 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"fmt"
"io/ioutil"
"os"
"strconv"
"strings"
)
func main() {
fmt.Println("step1 --> ", step1(load("sample_input.txt")))
fmt.Println("step2-sample2 --> ", step2(load("sample2_input.txt")))
fmt.Println("step1 --> ", step1(load("puzzle_input.txt")))
fmt.Println("step2 --> ", step2(load("puzzle_input.txt")))
}
type bound struct {
lower int
upper int
}
type constraint struct {
name string
bounds [2]bound
}
func (c *constraint) check(value int) bool {
return (value >= c.bounds[0].lower && value <= c.bounds[0].upper) || (value >= c.bounds[1].lower && value <= c.bounds[1].upper)
}
func step2(dataAsString string) int {
constraints, ownTickets, allNearbyTicket := readInput(dataAsString)
nearbyTicket := [][]int{}
// remove fully invalid ticket (not really needed I would think...)
for _, ticket := range allNearbyTicket {
valid := true
for _, ticketValue := range ticket {
invalidCount := 0
for _, contraint := range constraints {
if !contraint.check(ticketValue) {
invalidCount++
}
}
if invalidCount == len(constraints) {
valid = false
break
}
}
if valid {
nearbyTicket = append(nearbyTicket, ticket)
}
}
// count all column which are invalid for each ticket for each constraint
invalidList := map[string]map[int]bool{} //<-- what :D
for _, constraint := range constraints {
invalidList[constraint.name] = map[int]bool{}
for _, ticket := range nearbyTicket {
for columnIndex, val := range ticket {
_, exist := invalidList[constraint.name][columnIndex]
if exist {
continue
}
if !constraint.check(val) {
invalidList[constraint.name][columnIndex] = false
}
}
}
}
// find every ticket that has but one invalid constraint
// keep the record of that column and that constraint name
// flag all ither ticket as invalid for that column
// continue unil all tickets are found
association := map[string]int{}
for len(association) != len(constraints) {
for _, constraint := range constraints {
invalidCount := 0
validColumn := -1
for column := 0; column < len(ownTickets); column++ {
_, exist := invalidList[constraint.name][column]
if exist {
invalidCount++
} else {
validColumn = column
}
}
if invalidCount == len(ownTickets)-1 {
association[constraint.name] = validColumn
fmt.Println("Column", validColumn, "is associated with ", constraint.name)
for _, constraint := range constraints {
invalidList[constraint.name][validColumn] = false
}
}
}
}
// compute result looping on the association map
result := 1
for _, constraint := range constraints {
if strings.HasPrefix(constraint.name, "departure") {
val := ownTickets[association[constraint.name]]
fmt.Println(constraint.name, "col(", association[constraint.name], ") is", val)
result *= val
}
}
return result
}
func step1(dataAsString string) int {
constraints, _, nearbyTicket := readInput(dataAsString)
sum := 0
for _, ticket := range nearbyTicket {
for _, ticketValue := range ticket {
invalidCount := 0
for _, contraint := range constraints {
if !contraint.check(ticketValue) {
invalidCount++
}
}
if invalidCount == len(constraints) {
sum += ticketValue
}
}
}
return sum
}
func readInput(dataAsString string) ([]constraint, []int, [][]int) {
readOwnTicket := false
readNearbyTicket := false
constraints := []constraint{}
ownTicket := []int{}
nearbyTicket := [][]int{}
for _, line := range strings.Split(dataAsString, "\n") {
if len(line) == 0 {
continue
}
if line == "your ticket:" {
readOwnTicket = true
continue
}
if line == "nearby tickets:" {
readOwnTicket = false
readNearbyTicket = true
continue
}
if readOwnTicket == false && readNearbyTicket == false {
// read constraints
parts := strings.Split(line, ":")
constraint := constraint{name: parts[0], bounds: [2]bound{}}
boundParts := strings.Split(parts[1], "or")
for i := 0; i < 2; i++ {
lowMinPart := strings.Split(boundParts[i], "-")
lowerPart := atoi(strings.TrimSpace(lowMinPart[0]))
upperPart := atoi(strings.TrimSpace(lowMinPart[1]))
constraint.bounds[i] = bound{lower: lowerPart, upper: upperPart}
}
constraints = append(constraints, constraint)
} else if readOwnTicket {
for _, val := range strings.Split(line, ",") {
ownTicket = append(ownTicket, atoi(val))
}
} else if readNearbyTicket {
ticket := []int{}
for _, val := range strings.Split(line, ",") {
ticket = append(ticket, atoi(val))
}
nearbyTicket = append(nearbyTicket, ticket)
}
}
return constraints, ownTicket, nearbyTicket
}
// ----
func exist(array []int, item int) bool {
for _, element := range array {
if element == item {
return true
}
}
return false
}
func load(filename string) string {
text, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Print(err)
}
return string(text)
}
func atoi(line string) int {
number, err := strconv.Atoi(line)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
return number
}