diff options
author | Julien Dessaux | 2024-12-07 14:46:53 +0100 |
---|---|---|
committer | Julien Dessaux | 2024-12-07 14:46:53 +0100 |
commit | 25b4bd0a4846cee7c40b70794f8eb8f5ee03740e (patch) | |
tree | dc5947a1226e19d40d5a6d1cfd60ebaada2c443f | |
parent | 2024-07 in haskell (diff) | |
download | advent-of-code-25b4bd0a4846cee7c40b70794f8eb8f5ee03740e.tar.gz advent-of-code-25b4bd0a4846cee7c40b70794f8eb8f5ee03740e.tar.bz2 advent-of-code-25b4bd0a4846cee7c40b70794f8eb8f5ee03740e.zip |
2024-02 in factor
-rw-r--r-- | 2024/02-Red-Nosed_Reports/02-tests.factor | 7 | ||||
-rw-r--r-- | 2024/02-Red-Nosed_Reports/02.factor | 45 |
2 files changed, 52 insertions, 0 deletions
diff --git a/2024/02-Red-Nosed_Reports/02-tests.factor b/2024/02-Red-Nosed_Reports/02-tests.factor new file mode 100644 index 0000000..64d34bd --- /dev/null +++ b/2024/02-Red-Nosed_Reports/02-tests.factor @@ -0,0 +1,7 @@ +! Copyright (C) 2024 Julien (adyxax) Dessaux. +! See https://factorcode.org/license.txt for BSD license. +USING: tools.test aoc.2024.02.private ; +IN: aoc.2024.02.tests + +{ 2 } [ "example" part1 ] unit-test +{ 4 } [ "example" part2 ] unit-test diff --git a/2024/02-Red-Nosed_Reports/02.factor b/2024/02-Red-Nosed_Reports/02.factor new file mode 100644 index 0000000..455b723 --- /dev/null +++ b/2024/02-Red-Nosed_Reports/02.factor @@ -0,0 +1,45 @@ +! Copyright (C) 2024 Julien (adyxax) Dessaux. +! See https://factorcode.org/license.txt for BSD license. +USING: combinators.short-circuit.smart grouping io io.encodings.utf8 io.files +kernel math math.order math.parser prettyprint sequences splitting ; +IN: aoc.2024.02 + +<PRIVATE + +: load_input ( filename -- reports ) ! reports are a sequence of levels + "~/git/adyxax/aoc/2024/02-Red-Nosed_Reports/" + swap append utf8 file-lines + [ split-words [ string>number ] map ] map ; + +: bounded? ( levels -- ? ) + [ - abs 1 3 between? ] monotonic? ; + +: any_monotonic? ( levels -- ? ) + { [ [ > ] monotonic? ] + [ [ < ] monotonic? ] + } || ; + +: safe? ( levels -- ? ) + { [ any_monotonic? ] [ bounded? ] } && ; + +: part1 ( filename -- n ) + load_input + [ safe? ] count ; + +: candidates ( report -- reports ) + dup length <iota> [ over remove-nth ] map nip ; + +: safish? ( report -- ? ) + candidates [ safe? ] any? ; + +: part2 ( filename -- n ) + load_input + [ safish? ] count ; + +PRIVATE> + +: aoc202402 ( -- ) + "input" part1 . + "input" part2 . ; + +MAIN: aoc202402 |