-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (56 loc) · 1.1 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
package day02
import (
_ "embed"
"fmt"
"github.com/code-shoily/aocgo/io"
)
//go:embed input.txt
var input string
func Run() {
fmt.Println(solve(input))
}
func solve(input string) (int, string) {
ids := io.SplitLines(input)
return solvePart1(ids), solvePart2(ids)
}
func solvePart1(ids []string) int {
var twice, thrice int
for _, id := range ids {
hasTwice, hasThrice := twiceOrThrice(id)
if hasTwice {
twice++
}
if hasThrice {
thrice++
}
}
return twice * thrice
}
func solvePart2(ids []string) string {
visitedPartialIds := make(map[string]int)
for _, id := range ids {
for i := 0; i < len(id); i++ {
subId := id[:i] + id[i+1:]
if idx, ok := visitedPartialIds[subId]; ok && idx == i {
return subId
}
visitedPartialIds[subId] = i
}
}
panic("ERROR: No matching ID found")
}
func twiceOrThrice(id string) (twice bool, thrice bool) {
frequency := make(map[string]int)
for _, ch := range io.SplitBy(id, "") {
frequency[ch]++
}
for _, freq := range frequency {
switch freq {
case 2:
twice = true
case 3:
thrice = true
}
}
return twice, thrice
}