https://adventofcode.com/2021/day/6
For day 6, we had a list of numbers which represented a school of fish.
sample=: 3,4,3,1,2
Because this was just a comma separated list of digits, no special
parsing routines were necessary.
The task was conceptually to generate a step function which would
decrease each non-zero fish's number by 1. For fish represented by the
value 0, we replace them with a 6 and also append an 8 to the end of
the list. (And then we would run that step function the specified
number of times and find the length of the result.)
So, the complication here is that we could not use modulo arithmetic,
because that would interfere with fish represented by the number 7.
(Though if we limited modulo to a prefix which did not include any
values greater than 6...).
For this, I did not bother to create a step function. If I had, it
might have looked like this:
#((8#~0=]),<: |~ 7+]*7<])^:18] 3,4,3,1,2
26
#((8#~0=]),<: |~ 7+]*7<])^:80] 3,4,3,1,2
5934
But I did not feel like implementing that at the time, so instead I wrote this:
aoc6a=: {{
l=. y
for.i.x do.
l=. ((7>.l)|<:l),(0=l)#8
end.
#l
}}
What can I say? ... It worked.
For the second part, instead of 80 days, we needed to run for 256
days. So, simulating individual fish, we simulate the count of each
fish with each given value. And, I wanted to be able to quickly spot
any problems, so I threw an echo statement into my code, for
intermediate results:
aoc6=: {{
echo l=. _1 +#/.~(i.9),y
echo b=.6=i.9
for.i.x do.
echo l=.(1|.l)+b*{.l
end.
+/l
}}
Clearly, I could have instead used a step function here.
(Did I mention that AoC was sort of a bad habit for me?)
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm