AOC 2025 - day 06 #149
Replies: 4 comments 1 reply
-
|
Hi... this is how I would solve it: ;; Load data block containing numbers and operators at the end
data: load {123 328 51 64
45 64 387 23
6 98 215 314
* + * + }
;; Split data into numeric part and operator part
parse data [any integer! ops: to end]
ops: take/all ops ;; Extract tail of symbols (* + * +)
;; Determine table dimensions
cols: length? ops ;; Number of columns = number of operators
rows: (length? data) / cols ;; Compute number of rows from total numbers
;; Prepare to store column results
results: copy []
;; Loop through each column index
repeat c cols [
op: ops/:c ;; Operator for this column (* or +)
result: none ;; Result accumulator for current column
i: c ;; Start index of this column
prin ["Column[" c "] "]
;; Traverse down this column by stepping every `cols` elements
while [num: data/:i][
either result [ ;; If result already initialized
prin ["" op num]
result: either op == '+ [ ;; Apply operation
result + num
][ result * num ]
][
prin num
result: num ;; First value initializes result
]
i: i + cols ;; Jump to next row for current column
]
print [" =" result]
append results result ;; Store final column result
]
;; Display sum of all column results
print ["The grand total:" as-green sum results] |
Beta Was this translation helpful? Give feedback.
-
|
To be honest, your solution looks complicated to me. I probably don't see complete task without being in the game. What really beats me in your code is |
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
-
|
Seeing full task, here is my solution: ;; Read input file as lines of text (the number grid)
data: read/lines %input.txt
;; Extract the last line containing operators (* + * +)
ops: take/last data
;; Helper function to compute product of a list of numbers
prod: function[values][
result: 1
foreach value values [result: result * value]
result
]
;; Accumulators for part1 and part2 results
part1: copy []
part2: copy []
;; Parse the operator line to find each operation region
parse ops [
some [
;; Match operator (* or +) followed by any spaces until next operator
s: set op [#"*" | #"+"] any space e: (
;; Calculate start position and width of this operation column
s: index? s
w: (index? e) - s
;; Extract column data for ALL rows at this position/width
nums1: clear []
nums2: clear []
foreach line data [
append nums1 copy/part at line s w
]
;; For part2:
repeat i w [
n: clear ""
forall nums1 [
append n nums1/1/:i
]
;; Only accept if it parses as numbers
unless parse n [some space end][
append nums2 to integer! n
]
]
;; Convert part1 column data to integers
forall nums1 [nums1/1: to integer! nums1/1]
;; Apply operation based on operator type
either op == #"+" [
append part1 sum nums1
append part2 sum nums2
][
append part1 prod nums1
append part2 prod nums2
]
)
]
]
;; Display results
? part1
? part2
print ["The grand total part1:" as-green sum part1]
print ["The grand total part2:" as-green sum part2] |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First attempt at solving the Advent of Code Day 6 puzzle.
If you think there is a more idiomatic way to solve the puzzle or improve performance, please let me know.
I think the times to solve the puzzle are fine.
Test data: 13 ms
Puzzle data: 21 ms
Beta Was this translation helpful? Give feedback.
All reactions