Skip to content

Commit

Permalink
2024 day 2
Browse files Browse the repository at this point in the history
  • Loading branch information
TanklesXL committed Dec 2, 2024
1 parent 7651d59 commit ccc0a88
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ gleeunit = ">= 1.2.0 and < 2.0.0"

[gladvent.2024]
1 = { pt_1 = 1579939, pt_2 = 20351745 }
2 = { pt_1 = 282, pt_2 = 349 }
42 changes: 42 additions & 0 deletions src/aoc_2024/day_2.gleam
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
}

0 comments on commit ccc0a88

Please sign in to comment.