-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02p1.odin
90 lines (71 loc) · 1.55 KB
/
02p1.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import "core:fmt"
import "core:strconv"
import "core:strings"
D02P1 :: proc() {
input_string := #load("inputs/02.txt", string)
lines := strings.split(input_string, "\n", context.temp_allocator)
safe_reports := get_safe_reports(lines, false)
fmt.printf("Number of safe reports: %d\n", safe_reports)
}
get_safe_reports :: proc(lines: []string, dampener: bool) -> int {
result := 0
for line in lines {
if line == "" {
continue
}
str_numbers := strings.split(line, " ", context.temp_allocator)
numbers := make([dynamic]int)
defer delete(numbers)
for str in str_numbers {
append(&numbers, strconv.atoi(str))
}
if is_safe_report(numbers, dampener) {
result += 1
}
}
return result
}
is_safe_report :: proc(numbers: [dynamic]int, dampener: bool) -> bool {
if check_sequence(numbers) {
return true
}
if !dampener {
return false
}
for i in 0 ..< len(numbers) {
temp := make([dynamic]int)
defer delete(temp)
for j in 0 ..< len(numbers) {
if j != i {
append(&temp, numbers[j])
}
}
if check_sequence(temp) {
return true
}
}
return false
}
check_sequence :: proc(numbers: [dynamic]int) -> bool {
prev := numbers[0]
curr := numbers[1]
diff := abs(curr - prev)
if diff > 3 || diff == 0 {
return false
}
increasing := curr > prev
prev = curr
for i in 2 ..< len(numbers) {
curr = numbers[i]
if (increasing && curr <= prev) || (!increasing && curr >= prev) {
return false
}
diff = abs(curr - prev)
if diff > 3 || diff == 0 {
return false
}
prev = curr
}
return true
}