-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
86 lines (73 loc) · 1.89 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
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"io/ioutil"
"log"
"strconv"
"strings"
)
func main() {
input, err := ioutil.ReadFile("input.txt")
if err != nil {
log.Fatalf("unable to read file: %v", err)
}
changes, err := parseInput(input)
if err != nil {
log.Fatalf("unable to parse input: %v", err)
}
log.Printf("final frequency: %d", frequency(changes))
log.Printf("first frequency reached twice: %d", firstFrequencyReachedTwice(changes))
}
// parseInput tries to parse an input of the following form into a list of changes:
//
// +1
// -2
// +3
//
// The resulting list contains the changes as integers, with the sign denoting either an addition or subtraction.
func parseInput(input []byte) ([]int, error) {
lines := strings.Split(string(input), "\n")
result := make([]int, len(lines))
for i, line := range lines {
change, err := strconv.Atoi(line[1:])
if err != nil {
return nil, fmt.Errorf("failed to parse line %d: %v", i, err)
}
if strings.HasPrefix(line, "+") {
result[i] = change
} else {
result[i] = change * -1
}
}
return result, nil
}
// frequency calculates the resulting frequency after applying the given list of changes to an initial frequency of 0.
func frequency(changes []int) int {
var frequency = 0
for _, c := range changes {
frequency += c
}
return frequency
}
// firstFrequencyReachedTwice applies the given list of changes until a frequency is reached twice.
func firstFrequencyReachedTwice(changes []int) int {
var frequencies = make(map[int]bool)
var frequency = 0
var i = 0
for {
// mark the current frequency as reached
frequencies[frequency] = true
// calculate the next frequency
frequency += changes[i]
// if the frequency was previously reached, return it
if frequencies[frequency] {
return frequency
}
// Advance i to the next position of the list of changes, looping around
if i+1 < len(changes) {
i++
} else {
i = 0
}
}
}