-
Notifications
You must be signed in to change notification settings - Fork 0
/
day2.go
101 lines (84 loc) · 1.73 KB
/
day2.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
package main
import (
"log"
)
// BoxLetterCount checksum helper
func BoxLetterCount(boxID string) (int, int) {
seen := make(map[rune]int)
for _, char := range boxID {
seen[char] = seen[char] + 1
}
var twice int
var thrice int
for _, cnt := range seen {
if cnt == 2 {
twice = 1
}
if cnt == 3 {
thrice = 1
}
}
return twice, thrice
}
// BoxSetCount checksum all the boxIDs
func BoxSetCount(boxIDs ...string) int {
var twice int
var thrice int
for _, boxID := range boxIDs {
a, b := BoxLetterCount(boxID)
twice = twice + a
thrice = thrice + b
}
return twice * thrice
}
func scanDay2File() []string {
lines, err := FileInput(2)
if err != nil {
log.Fatalf("failed to get data, %s", err)
}
return lines
}
func day2() int {
lines := scanDay2File()
checksum := BoxSetCount(lines...)
return checksum
}
func day2pt2() string {
lines := scanDay2File()
answer := BoxSetProximity(lines...)
return answer
}
// BoxProximity calculate how close two boxes are
func BoxProximity(boxA string, boxB string) (int, string) {
aRunes := []rune(boxA)
bRunes := []rune(boxB)
same := make([]rune, 0)
var diff int
for i := 0; i < len(aRunes); i++ {
if aRunes[i] == bRunes[i] {
same = append(same, aRunes[i])
} else {
diff++
}
}
return diff, string(same)
}
// BoxSetProximity proximity of all the boxIDs
func BoxSetProximity(boxIDs ...string) string {
var answer string
length := len(boxIDs)
var cycles int
log.Printf("length: %d\n", length)
for i := 0; i < length; i++ {
for j := i + 1; j < length; j++ {
cycles++
diff, same := BoxProximity(boxIDs[i], boxIDs[j])
if diff == 1 {
log.Printf("found after %d comparisons\n", cycles)
answer = same
return answer
}
}
}
return answer
}