|
|
|
@ -16,12 +16,12 @@ main =
|
|
|
|
|
part2 = List.keepIf levels \l -> (isSafe l Bool.true) || (isSafe (List.dropAt l 0) Bool.false) || (isSafe (List.dropAt l 1) Bool.false)
|
|
|
|
|
Stdout.line! "Safe Reports, Part 2: $(Num.toStr (List.len part2))"
|
|
|
|
|
|
|
|
|
|
isSafe = \level, isDampening ->
|
|
|
|
|
List.walkWithIndexUntil level { previous: 0, direction: Unset, valid: Bool.true, skipped: 0 } \state, elem, idx ->
|
|
|
|
|
isSafe = \line, isDampening ->
|
|
|
|
|
List.walkWithIndexUntil line { value: 0, direction: Unset, valid: Bool.true, skipped: 0 } \state, value, idx ->
|
|
|
|
|
if idx == 0 then # on first iteration, can't fail, just return
|
|
|
|
|
Continue { state & previous: elem, direction: Unset }
|
|
|
|
|
Continue { state & value, direction: Unset }
|
|
|
|
|
else
|
|
|
|
|
diff = Num.absDiff elem state.previous
|
|
|
|
|
diff = Num.absDiff value state.value
|
|
|
|
|
|
|
|
|
|
if diff < 1 || diff > 3 then # always check diff
|
|
|
|
|
if isDampening && state.skipped == 0 then
|
|
|
|
@ -29,16 +29,16 @@ isSafe = \level, isDampening ->
|
|
|
|
|
else
|
|
|
|
|
Break { state & valid: Bool.false }
|
|
|
|
|
else
|
|
|
|
|
direction = if elem > state.previous then Ascending else Descending
|
|
|
|
|
direction = if value > state.value then Ascending else Descending
|
|
|
|
|
|
|
|
|
|
if idx == 1 then # on 2nd iteration, we don't check for direction change
|
|
|
|
|
Continue { state & previous: elem, direction: direction }
|
|
|
|
|
Continue { state & value, direction }
|
|
|
|
|
else if state.direction != direction then
|
|
|
|
|
if isDampening && state.skipped == 0 then
|
|
|
|
|
Continue { state & skipped: 1 }
|
|
|
|
|
else
|
|
|
|
|
Break { state & valid: Bool.false }
|
|
|
|
|
else
|
|
|
|
|
Continue { state & previous: elem, direction: direction }
|
|
|
|
|
Continue { state & value, direction }
|
|
|
|
|
|
|
|
|
|
|> .valid
|
|
|
|
|