-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (76 loc) · 1.92 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
// https://adventofcode.com/2024/day/2
// Day 2: Red-Nosed Reports
package main
import (
"aoc/libaoc"
"fmt"
"strings"
)
type Report struct {
crease string // In- or De- ?
values []int // Report data
unsafe bool // Is it gonna blow?
}
func main() {
// input, err := libaoc.ReadLines("example.txt")
input, err := libaoc.ReadLines("input.txt")
if err != nil {
panic("No input!")
}
reports := getReports(input)
fmt.Printf("%d safe reports\n", countSafeReports(reports))
}
// getReports reads the input and makes Reports out of them.
func getReports(input []string) (reports []Report) {
for _, line := range input {
exploded := strings.Fields(line)
var report Report
for _, val := range exploded {
report.values = append(report.values, libaoc.SilentAtoi(val))
}
switch { // Get the in- or decrease
case report.values[0] > report.values[1]:
report.crease = "de"
case report.values[0] < report.values[1]:
report.crease = "in"
default: // Only reached if the same numbers
report.unsafe = true
reports = append(reports, report)
continue
}
report.unsafe = checkReportvaluesUnsafe(report)
reports = append(reports, report)
}
return
}
// checkReportvaluesUnsafe checks if a report is unsafe
func checkReportvaluesUnsafe(report Report) bool {
for i := 0; i < len(report.values)-1; i++ {
var high, low int
switch report.crease { // Which is high, which is low?
case "de":
high = report.values[i]
low = report.values[i+1]
case "in":
low = report.values[i]
high = report.values[i+1]
}
switch { // Let's do the safety-maths!
case high-low > 3: // To big a -crease
return true
case high-low <= 0: // Wrong way!
return true
default: // Nothing wrong here
}
}
return false
}
// countSafeReports returns the number of safe reports
func countSafeReports(reports []Report) (num int) {
for _, report := range reports {
if report.unsafe == false {
num++
}
}
return num
}