-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
44 lines (34 loc) · 765 Bytes
/
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
package day01
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, int) {
frequencies := parse(input)
return solvePart1(frequencies), solvePart2(frequencies)
}
func solvePart1(frequencies []int) (frequency int) {
for _, freq := range frequencies {
frequency += freq
}
return frequency
}
func solvePart2(frequencies []int) (frequency int) {
visited := make(map[int]bool)
for i := 0; ; i = (i + 1) % len(frequencies) {
visited[frequency] = true
frequency += frequencies[i]
if _, ok := visited[frequency]; ok {
return frequency
}
}
}
func parse(input string) (frequencies []int) {
return io.SplitIntLines(input)
}