-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2206.go
82 lines (73 loc) · 1.17 KB
/
aoc2206.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
package main
import "fmt"
import "io/ioutil"
import "sort"
func solve(path string) (int, int) {
data, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
s := string(data)
n := len(s)
var i, j int
var ok bool
// part 1
i = 0
res := -1
for i < n - 4 {
c4 := s[i : i + 4]
a := []rune (c4)
sort.Slice(a, func(l int, r int) bool {
return a[l] < a[r]
})
j = 0
ok = true // check if a contains no repeated runes
for j < 3 {
if a[j] == a[j + 1] {
ok = false
}
j++
}
if ok {
res = i + 4
break
}
i++
}
// part 2
res2 := -1
p2 := 14
i = 0
for i < n - p2 {
c4 := s[i : i + p2]
a := []rune (c4)
sort.Slice(a, func(l int, r int) bool {
return a[r] < a[l]
})
j = 0
ok = true // check if a contains no repeated runes
for j < p2 - 1 {
if a[j] == a[j + 1] {
ok = false
}
j++
}
if ok {
res2 = i + p2
break
}
i++
}
return res, res2
}
func main() {
var r1, r2 int
r1, r2 = solve("_inputs/2206.0")
fmt.Println("data")
fmt.Println("Star 1:", r1)
fmt.Println("Star 2:", r2, "\n")
r1, r2 = solve("_inputs/2206.1")
fmt.Println("test")
fmt.Println("Star 1:", r1)
fmt.Println("Star 2:", r2)
}