Skip to content

Commit a63f329

Browse files
authored
2024 Day 2 (vlang#69)
1 parent 8a1d0c7 commit a63f329

2 files changed

Lines changed: 68 additions & 0 deletions

File tree

2024/02/JalonSolov.v

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import math
2+
import os
3+
4+
lines := os.read_lines('reports.input')!
5+
6+
mut safe_reports := 0
7+
mut dampened_safe_reports := 0
8+
mut val := 0
9+
mut abs_val := 0
10+
11+
for line in lines {
12+
mut all_safe := true
13+
mut bad_levels := 0
14+
reps := line.split(' ').map(it.int())
15+
16+
mut desc := (reps[0] - reps[1]) > 0
17+
18+
for level_idx in 0 .. reps.len - 1 {
19+
val = reps[level_idx] - reps[level_idx + 1]
20+
abs_val = math.abs(val)
21+
if (val > 0) != desc || abs_val < 1 || abs_val > 3 {
22+
all_safe = false
23+
bad_levels++
24+
match level_idx {
25+
// if the first problem was at the start of the line,
26+
// re-do the `desc` var in case the direction changes
27+
0 {
28+
desc = (reps[1] - reps[2]) > 0
29+
continue
30+
}
31+
// if the first problem is the 2nd level, recalculate
32+
// desc starting from here, just in case
33+
1 {
34+
desc = (reps[1] - reps[2]) > 0
35+
}
36+
// if the problem was on the last 2 levels, no need
37+
// to worry about anything else - if that was the only
38+
// problem, we still have a dampened report
39+
reps.len - 2 {
40+
break
41+
}
42+
// otherwise, try checking around the error point
43+
else {
44+
desc = (reps[level_idx - 1] - reps[level_idx + 1]) > 0
45+
val = reps[level_idx - 1] - reps[level_idx + 1]
46+
abs_val = math.abs(val)
47+
if (val > 0) != desc || abs_val < 1 || abs_val > 3 {
48+
bad_levels++
49+
break
50+
}
51+
}
52+
}
53+
}
54+
}
55+
56+
if all_safe == true {
57+
safe_reports++
58+
}
59+
60+
if bad_levels < 2 {
61+
dampened_safe_reports++
62+
}
63+
}
64+
65+
println('Part 1: ${safe_reports}')
66+
println('Part 2: ${dampened_safe_reports}')

known/2024/02/JalonSolov.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Part 1: 2
2+
Part 2: 4

0 commit comments

Comments
 (0)