-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
2 changed files
with
43 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
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,42 @@ | ||
import gleam/int | ||
import gleam/list | ||
import gleam/result | ||
import gleam/string | ||
|
||
pub fn parse(input: String) -> List(List(Int)) { | ||
use line <- list.map(string.split(input, "\n")) | ||
use item <- list.map(string.split(line, " ")) | ||
let assert Ok(i) = int.parse(item) | ||
i | ||
} | ||
|
||
type Direction { | ||
Increasing | ||
Decreasing | ||
Start | ||
} | ||
|
||
fn is_safe(line: List(Int)) -> Bool { | ||
{ | ||
use safety, #(left, right) <- list.try_fold(list.window_by_2(line), Start) | ||
let diff = right - left | ||
case safety { | ||
Start | Increasing if diff >= 1 && diff <= 3 -> Ok(Increasing) | ||
Start | Decreasing if diff <= -1 && diff >= -3 -> Ok(Decreasing) | ||
_ -> Error(Nil) | ||
} | ||
} | ||
|> result.is_ok | ||
} | ||
|
||
pub fn pt_1(input: List(List(Int))) { | ||
input |> list.filter(is_safe) |> list.length | ||
} | ||
|
||
pub fn pt_2(input: List(List(Int))) { | ||
input | ||
|> list.filter(fn(line) { | ||
list.any(list.combinations(line, list.length(line) - 1), is_safe) | ||
}) | ||
|> list.length | ||
} |