diff --git a/day2/part2.lua b/day2/part2.lua index 290d7eb..2bc1f36 100644 --- a/day2/part2.lua +++ b/day2/part2.lua @@ -98,6 +98,7 @@ local function is_safe(report) return unsafe, unsafe_idx_1, unsafe_idx_2 end +-- Copy the report, remove the specified index, and test if it is safe local function test_altered_report(report, idx) local test_report = table.shallow_copy(report) table.remove(test_report, idx) @@ -107,9 +108,11 @@ end local safe_count = 0 for _, report in ipairs(reports) do + -- if unsafe == true, idx1 and idx2 will be null local unsafe, idx1, idx2 = is_safe(report) if unsafe then + -- if the report is unsafe, generate 2 reports with the potentially bad values removed, and test them again if not test_altered_report(report, idx1) or not test_altered_report(report, idx2) then unsafe = false end