aboutsummaryrefslogtreecommitdiff
path: root/2024
diff options
context:
space:
mode:
authorJulien Dessaux2024-12-11 21:31:30 +0100
committerJulien Dessaux2024-12-11 21:41:51 +0100
commite8941a3e0ede21527d95c0e10e218005fb6b633d (patch)
treee37d612f510bb29c915bac725e6ffc8c70baabfc /2024
parent2024-11 in haskell (diff)
downloadadvent-of-code-e8941a3e0ede21527d95c0e10e218005fb6b633d.tar.gz
advent-of-code-e8941a3e0ede21527d95c0e10e218005fb6b633d.tar.bz2
advent-of-code-e8941a3e0ede21527d95c0e10e218005fb6b633d.zip
2024-03 in factor
Diffstat (limited to '2024')
-rw-r--r--2024/02-Red-Nosed_Reports/02-tests.factor2
-rw-r--r--2024/02-Red-Nosed_Reports/02.factor2
-rw-r--r--2024/03-Mull_It_Over/03-tests.factor7
-rw-r--r--2024/03-Mull_It_Over/03.factor94
4 files changed, 103 insertions, 2 deletions
diff --git a/2024/02-Red-Nosed_Reports/02-tests.factor b/2024/02-Red-Nosed_Reports/02-tests.factor
index 64d34bd..3e0ae24 100644
--- a/2024/02-Red-Nosed_Reports/02-tests.factor
+++ b/2024/02-Red-Nosed_Reports/02-tests.factor
@@ -1,5 +1,5 @@
! Copyright (C) 2024 Julien (adyxax) Dessaux.
-! See https://factorcode.org/license.txt for BSD license.
+! See https://git.adyxax.org/adyxax/advent-of-code/tree/LICENSE for EUPL license.
USING: tools.test aoc.2024.02.private ;
IN: aoc.2024.02.tests
diff --git a/2024/02-Red-Nosed_Reports/02.factor b/2024/02-Red-Nosed_Reports/02.factor
index 455b723..a758715 100644
--- a/2024/02-Red-Nosed_Reports/02.factor
+++ b/2024/02-Red-Nosed_Reports/02.factor
@@ -1,5 +1,5 @@
! Copyright (C) 2024 Julien (adyxax) Dessaux.
-! See https://factorcode.org/license.txt for BSD license.
+! See https://git.adyxax.org/adyxax/advent-of-code/tree/LICENSE for EUPL 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
diff --git a/2024/03-Mull_It_Over/03-tests.factor b/2024/03-Mull_It_Over/03-tests.factor
new file mode 100644
index 0000000..e2a9d39
--- /dev/null
+++ b/2024/03-Mull_It_Over/03-tests.factor
@@ -0,0 +1,7 @@
+! Copyright (C) 2024 Julien (adyxax) Dessaux.
+! See https://git.adyxax.org/adyxax/advent-of-code/tree/LICENSE for EUPL license.
+USING: tools.test aoc.2024.03.private ;
+IN: aoc.2024.03.tests
+
+{ 161 } [ "example" part1 ] unit-test
+{ 48 } [ "example2" part2 ] unit-test
diff --git a/2024/03-Mull_It_Over/03.factor b/2024/03-Mull_It_Over/03.factor
new file mode 100644
index 0000000..c585923
--- /dev/null
+++ b/2024/03-Mull_It_Over/03.factor
@@ -0,0 +1,94 @@
+! Copyright (C) 2024 Julien (adyxax) Dessaux.
+! See https://git.adyxax.org/adyxax/advent-of-code/tree/LICENSE for EUPL license.
+USING: accessors io.encodings.utf8 io.files kernel make math math.parser peg
+peg.parsers prettyprint regexp sequences ;
+IN: aoc.2024.03
+
+<PRIVATE
+
+: load_input ( filename -- string )
+ "~/git/adyxax/aoc/2024/03-Mull_It_Over/"
+ swap append utf8 file-contents ;
+
+! ----- Let's do part1 with regexes --------------------------------------------
+: get_muls ( string -- instructions )
+ R/ mul\(\d+,\d+\)/ all-matching-subseqs ;
+
+: mul>result ( string -- n )
+ R/ \d+/ all-matching-subseqs
+ [ string>number ] map
+ product ;
+
+: part1 ( filename -- n )
+ load_input
+ get_muls
+ [ mul>result ] map
+ sum ;
+
+! ----- And part2 with a real parser -------------------------------------------
+TUPLE: computer total multiplying? ;
+
+GENERIC: compute ( computer item -- computer )
+TUPLE: do ;
+M: do compute
+ drop
+ t >>multiplying? ;
+
+TUPLE: dont ;
+M: dont compute
+ drop
+ f >>multiplying? ;
+
+TUPLE: mul opX opY ;
+M: mul compute
+ over multiplying?>>
+ [ [ opX>> ] [ opY>> ] bi *
+ over total>> + >>total ]
+ [ drop ]
+ if ;
+
+TUPLE: nop ;
+M: nop compute
+ drop ;
+
+: parse_do ( -- parser )
+ "do()" token hide [ drop do boa ] action ;
+
+: parse_dont ( -- parser )
+ "don't()" token hide [ drop dont boa ] action ;
+
+: parse_mul ( -- parser )
+ [ "mul(" token hide ,
+ integer-parser ,
+ "," token hide ,
+ integer-parser ,
+ ")" token hide ,
+ ] seq*
+ [ first2 mul boa ] action ;
+
+: parse_nop ( -- parser )
+ any-char hide [ drop nop boa ] action ;
+
+PEG: parse_input ( string -- ast )
+ parse_do
+ parse_dont
+ parse_mul
+ parse_nop
+ 4choice
+ repeat1 ;
+
+: part2 ( filename -- n )
+ load_input
+ parse_input
+ 0 t computer boa
+ swap
+ [ compute ] each
+ total>> ;
+
+PRIVATE>
+
+: aoc202403 ( -- )
+ "input" part1 .
+ "input" part2 . ;
+
+MAIN: aoc202403