Skip to content

Commit

Permalink
aoc 2024 :: 2
Browse files Browse the repository at this point in the history
  • Loading branch information
refaktor committed Dec 2, 2024
1 parent ddcbbf6 commit 3ca5f9b
Showing 1 changed file with 23 additions and 30 deletions.
53 changes: 23 additions & 30 deletions examples/adventofcode/2024/2/main.rye
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
{

; Advent of Code 2024 :: Day 2 - hard but *no brute-force* for p2! :)

is-ok: fn { dif sign } {
any { all { sign > 0 dif > 0 dif < 4 }
all { sign < 0 dif < 0 dif > -4 } }
}

; Advent of Code 2024 :: Day 2 - hard but *no brute-force* for p2! :)

is-ok: fn { dif sign } {
any { all { sign > 0 dif > 0 dif < 4 }
all { sign < 0 dif < 0 dif > -4 } }
}

calc-sign: fn { r } {
.split\every ( r .length? / 2 |to-integer )
|map ?avg |set! { a b } , b - a
}

is-safe: fn { row tolerance } {
bad: 0 sign: calc-sign row ,
.reduce 'prev {
::cur - prev ::x ,
either all { is-ok x sign } { cur } { inc! 'bad , prev }
}
bad <= tolerance
calc-sign: fn { r } {
.split\every ( r .length? / 2 |to-integer )
|map ?avg |set! { a b } , b - a
}

is-safe: fn { row tolerance } {
bad: 0 sign: calc-sign row ,
.reduce 'prev {
::cur - prev ::x ,
either is-ok x sign { cur } { inc! 'bad , prev }
}

read\lines %input.txt |map ?load :data

data |map { .is-safe 0 } |sum |print ; part 1

data |map { .is-safe 1 ::x , .reverse! .is-safe 1 ::z , any { x z } }
|sum |print ; part 2
bad <= tolerance
}

read\lines %input.txt |map ?load :data

data |map { .is-safe 0 } |sum |print ; part 1

data |map { .is-safe 1 ::x , .reverse! .is-safe 1 ::z , any { x z } }
|sum |print ; part 2

}

0 comments on commit 3ca5f9b

Please sign in to comment.