From 3ca5f9b6a3e0021318b0334d6dc8025a2e9c58c4 Mon Sep 17 00:00:00 2001 From: refaktor Date: Tue, 3 Dec 2024 00:17:59 +0100 Subject: [PATCH] aoc 2024 :: 2 --- examples/adventofcode/2024/2/main.rye | 53 ++++++++++++--------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/examples/adventofcode/2024/2/main.rye b/examples/adventofcode/2024/2/main.rye index 0953a7a8..1a29607d 100644 --- a/examples/adventofcode/2024/2/main.rye +++ b/examples/adventofcode/2024/2/main.rye @@ -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 -} \ No newline at end of file + \ No newline at end of file