Cleaning, renaming

master
Dustin Swan 1 month ago
parent b421513019
commit e28c531be2
Signed by: dustinswan
GPG Key ID: AB49BD6B2B3A6377

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

Loading…
Cancel
Save