-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (124 loc) · 3.15 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"github.com/fwielstra/AoC2022/day1"
"github.com/fwielstra/AoC2022/day2"
"github.com/fwielstra/AoC2022/day3"
"github.com/fwielstra/AoC2022/day4"
"github.com/fwielstra/AoC2022/day5"
"github.com/fwielstra/AoC2022/day6"
"io"
"os"
"strconv"
"time"
)
func main() {
day := os.Args[1]
filename := os.Args[2]
args := os.Args[3:]
file, err := os.Open(filename)
if err != nil {
fmt.Printf("error opening file %s: %s\n", filename, err)
os.Exit(1)
}
switch day {
case "day1":
runDay1(filename, args)
case "day2":
runDay2(filename)
case "day3":
runDay3(filename)
case "day4":
runDay4(filename)
case "day5":
runDay5(filename)
case "day6":
runDay6(filename)
default:
fmt.Printf("unrecognized day %s\n", day)
}
file.Close()
}
func withFile(filename string, callback func(r io.Reader)) {
file, err := os.Open(filename)
if err != nil {
fmt.Printf("error opening file %s: %s\n", filename, err)
os.Exit(1)
}
start := time.Now()
callback(file)
elapsed := time.Since(start)
fmt.Printf("processing file %s took %s\n", filename, elapsed)
err = file.Close()
if err != nil {
fmt.Printf("error closing file %s: %s\n", filename, err)
os.Exit(1)
}
}
func runDay1(filename string, args []string) {
withFile(filename, func(r io.Reader) {
top, _ := strconv.Atoi(args[0])
result := day1.FindHighestCalories(r, top)
fmt.Printf("Day 1 result top %d for file %s: %d\n", top, filename, result)
})
}
func runDay2(filename string) {
withFile(filename, func(r io.Reader) {
scores := day2.GetScores(r)
sum := day2.Sum(scores)
fmt.Printf("Result for day 2: %d\n", sum)
})
withFile(filename, func(r io.Reader) {
// simple approach
dumb := day2.Dumb(r)
fmt.Printf("Result for day 2 dumb: %d\n", dumb)
})
withFile(filename, func(r io.Reader) {
// part 2
scores := day2.GetScoresPartTwo(r)
sum := day2.Sum(scores)
fmt.Printf("Result for day 2 part 2: %d\n", sum)
})
}
func runDay3(filename string) {
withFile(filename, func(r io.Reader) {
sum := day3.SumCompartiments(r)
fmt.Printf("Result for day 3 part 1: %d\n", sum)
})
withFile(filename, func(r io.Reader) {
sum := day3.SumBadges(r)
fmt.Printf("Result for day 3 part 2: %d\n", sum)
})
}
func runDay4(filename string) {
withFile(filename, func(r io.Reader) {
pairs := day4.CountFullContains(r)
fmt.Printf("Resullt for day 4 part 1: %d\n", pairs)
})
withFile(filename, func(r io.Reader) {
overlaps := day4.CountOverlaps(r)
fmt.Printf("Resullt for day 4 part 2: %d\n", overlaps)
})
}
func runDay5(filename string) {
withFile(filename, func(r io.Reader) {
result := day5.Process(r)
fmt.Printf("Result for day 5 part 1: %s\n", result)
})
withFile(filename, func(r io.Reader) {
result := day5.Process9001(r)
fmt.Printf("Result for day 5 part 2: %s\n", result)
})
}
func runDay6(filename string) {
withFile(filename, func(r io.Reader) {
str, _ := io.ReadAll(r)
result := day6.FindStartMarker(string(str))
fmt.Printf("Result for day 6 part 1: %d\n", result)
})
withFile(filename, func(r io.Reader) {
str, _ := io.ReadAll(r)
result := day6.FindStartMessage(string(str))
fmt.Printf("Result for day 6 part 2: %d\n", result)
})
}