-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
77 lines (68 loc) · 1.33 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
package main
import (
"fmt"
"io"
"strings"
lib "github.com/teivah/advent-of-code"
)
func fs1(input io.Reader, phases int) string {
s := lib.ReaderToString(input)
pattern := []int{0, 1, 0, -1}
for phase := 0; phase < phases; phase++ {
sb := strings.Builder{}
for i := 0; i < len(s); i++ {
fmt.Println(i, len(s))
r := to(pattern, i, s)
sb.WriteRune(r)
}
s = sb.String()
_ = s
}
return s[:8]
}
func to(pattern []int, i int, s string) rune {
sum := 0
mult := pattern[0]
patternIdx := 0
if i == 0 {
mult = pattern[1]
patternIdx = 1
}
repeat := i
for j := 0; j < len(s); j++ {
v := lib.RuneToInt(rune(s[j])) * mult
sum += v
repeat--
if repeat <= 0 {
patternIdx = (patternIdx + 1) % len(pattern)
mult = pattern[patternIdx]
repeat = i + 1
}
}
return lib.IntToRune(lib.Abs(sum % 10))
}
func fs2(input io.Reader, phases, repeat int) []int {
s := lib.ReaderToString(input)
offset := lib.StringToInt(s[:7])
d := 8
s = strings.Repeat(s, repeat)
v := toInts(s)
for i := 0; i < phases; i++ {
do(v, offset)
}
return v[offset : offset+d]
}
func toInts(s string) []int {
res := make([]int, len(s))
for i := 0; i < len(s); i++ {
res[i] = lib.RuneToInt(rune(s[i]))
}
return res
}
func do(v []int, offset int) {
sum := 0
for i := len(v) - 1; i >= offset; i-- {
sum += v[i]
v[i] = (sum) % 10
}
}