-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday02.odin
76 lines (63 loc) · 1.29 KB
/
day02.odin
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
package day02
import "core:fmt"
import "core:os"
import "core:slice"
import "core:strconv"
import "core:strings"
main :: proc() {
content, ok := os.read_entire_file("input/day02.txt")
if !ok {
// could not read file
return
}
defer delete(content)
lines := strings.split_lines(string(content))
reports := make([][]int, len(lines))
for line, i in lines {
reports[i] = slice.mapper(
strings.split(line, " "),
proc(x: string) -> int {return strconv.atoi(x)},
)
}
safe_reports_count_1 := 0
for report in reports {
if is_safe(report) {
safe_reports_count_1 += 1
}
}
// Part 1
fmt.println(safe_reports_count_1)
safe_reports_count_2 := 0
for report in reports {
for i in 0 ..< len(report) {
// remove element at i
x: [dynamic]int
append(&x, ..report[0:i])
append(&x, ..report[i + 1:len(report)])
if is_safe(x[:]) {
safe_reports_count_2 += 1
break
}
}
}
// Part 2
fmt.println(safe_reports_count_2)
}
is_safe :: proc(report: []int) -> bool {
increasing := report[0] < report[1]
for i in 1 ..< len(report) {
prev := report[i - 1]
curr := report[i]
diff := curr - prev
if increasing {
if !(diff > 0 && diff < 4) {
return false
}
} else { // decreasing
if !(diff < 0 && diff > -4) {
return false
}
}
}
return true
}