-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday16b.go
More file actions
92 lines (71 loc) · 2.29 KB
/
day16b.go
File metadata and controls
92 lines (71 loc) · 2.29 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
package main
import (
"fmt"
"strconv"
"strings"
)
func whatRuleSetIsThisValueIn(ruleSet []rule, value int) []int {
var results []int
for rulePos := 0; rulePos < len(ruleSet); rulePos++ {
if (value >= ruleSet[rulePos].lowerLimit1 && value <= ruleSet[rulePos].upperLimit1) ||
(value >= ruleSet[rulePos].lowerLimit2 && value <= ruleSet[rulePos].upperLimit2) {
results = append(results, rulePos)
}
}
return results
}
func isTicketValid(ruleSet []rule, ticket string) bool {
var checkValue int
// Compare all the values in a ticket to the RuleSet
// Each value that fails ALL rules is added to the error rate
ticketValues := strings.Split(ticket, ",")
for _, value := range ticketValues {
checkValue, _ = strconv.Atoi(value)
if !validValueInRuleSet(checkValue, ruleSet) {
return false
}
}
return true
}
func decodeMyTicket(ruleSet []rule, nearbyTickets []string, myTicket string) int {
var checkValue int
var fieldResults map[int]int
// Will hold the count to find the rule for each field
fieldResults = make(map[int]int)
for fieldNumber := 0; fieldNumber < 20; fieldNumber++ {
var rowChoice []int
rowChoice = make([]int, len(ruleSet))
var validTickets = 0
for _, currentTicket := range nearbyTickets {
//fmt.Printf("Checking field %d of %s\n", fieldNumber, currentTicket)
if isTicketValid(ruleSet, currentTicket) {
//fmt.Println("....is valid")
validTickets++
// Check all values in field 1. Then field 2, Then field 3.
ticketValues := strings.Split(currentTicket, ",")
// for _, value := range ticketValues {
// checkValue, _ = strconv.Atoi(value)
checkValue, _ = strconv.Atoi(ticketValues[fieldNumber])
//fmt.Println(whatRuleSetIsThisValueIn(ruleSet, checkValue))
for _, i := range whatRuleSetIsThisValueIn(ruleSet, checkValue) {
rowChoice[i]++
}
} else {
//fmt.Println("....is NOT valid")
}
}
fmt.Println("Results for field:", fieldNumber, rowChoice)
var topValue int = 0
var topPos int = 0
for pos, value := range rowChoice {
// Edge case: some fields will not choose a winner. They will need the others to eliminate what it can be
if value > topValue {
topValue = value
topPos = pos
}
}
fieldResults[fieldNumber] = topPos
//fmt.Println("Results: ", fieldResults)
}
return 0
}