-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a1d0c7
commit a63f329
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import math | ||
import os | ||
|
||
lines := os.read_lines('reports.input')! | ||
|
||
mut safe_reports := 0 | ||
mut dampened_safe_reports := 0 | ||
mut val := 0 | ||
mut abs_val := 0 | ||
|
||
for line in lines { | ||
mut all_safe := true | ||
mut bad_levels := 0 | ||
reps := line.split(' ').map(it.int()) | ||
|
||
mut desc := (reps[0] - reps[1]) > 0 | ||
|
||
for level_idx in 0 .. reps.len - 1 { | ||
val = reps[level_idx] - reps[level_idx + 1] | ||
abs_val = math.abs(val) | ||
if (val > 0) != desc || abs_val < 1 || abs_val > 3 { | ||
all_safe = false | ||
bad_levels++ | ||
match level_idx { | ||
// if the first problem was at the start of the line, | ||
// re-do the `desc` var in case the direction changes | ||
0 { | ||
desc = (reps[1] - reps[2]) > 0 | ||
continue | ||
} | ||
// if the first problem is the 2nd level, recalculate | ||
// desc starting from here, just in case | ||
1 { | ||
desc = (reps[1] - reps[2]) > 0 | ||
} | ||
// if the problem was on the last 2 levels, no need | ||
// to worry about anything else - if that was the only | ||
// problem, we still have a dampened report | ||
reps.len - 2 { | ||
break | ||
} | ||
// otherwise, try checking around the error point | ||
else { | ||
desc = (reps[level_idx - 1] - reps[level_idx + 1]) > 0 | ||
val = reps[level_idx - 1] - reps[level_idx + 1] | ||
abs_val = math.abs(val) | ||
if (val > 0) != desc || abs_val < 1 || abs_val > 3 { | ||
bad_levels++ | ||
break | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if all_safe == true { | ||
safe_reports++ | ||
} | ||
|
||
if bad_levels < 2 { | ||
dampened_safe_reports++ | ||
} | ||
} | ||
|
||
println('Part 1: ${safe_reports}') | ||
println('Part 2: ${dampened_safe_reports}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Part 1: 2 | ||
Part 2: 4 |