-
Notifications
You must be signed in to change notification settings - Fork 0
/
day01.go
69 lines (56 loc) · 1.12 KB
/
day01.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 main
import (
"bufio"
"log"
"os"
"strconv"
)
func part1() int64 {
f, err := os.OpenFile("day01.txt", os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatalf("unable to open input: %v", err)
}
defer f.Close()
sc := bufio.NewScanner(f)
freq := int64(0)
for sc.Scan() {
l := sc.Text()
i, err := strconv.ParseInt(l, 10, 32)
if err != nil {
log.Fatalf("Couldn't parse line: %s, %v", l, err)
}
freq += i
}
return freq
}
func part2() int64 {
freq := int64(0)
freqTable := make(map[int64]int)
for {
f, err := os.OpenFile("day01.txt", os.O_RDONLY, os.ModePerm)
if err != nil {
log.Fatalf("unable to open input: %v", err)
}
defer f.Close()
sc := bufio.NewScanner(f)
for sc.Scan() {
l := sc.Text()
i, err := strconv.ParseInt(l, 10, 32)
if err != nil {
log.Fatalf("Couldn't parse line: %s, %v", l, err)
}
freq += i
seen := freqTable[freq]
seen++
freqTable[freq] = seen
if seen > 1 {
return freq
}
}
log.Printf("Looping:%d, %d", freq, len(freqTable))
}
}
func main() {
log.Printf("Part 1 answer:%d", part1())
log.Printf("Part 2 answer:%d", part2())
}