My take on Day 6 was similar to Raul's second part's solution. I did it
tacitly though:
I realised no fish would ever have an age >8, so I kept the number of
fishes for each age in an array, i.e. n{state is the number of fishes at
age n (see enc below).
Then each step rotates the fishes and adds new ones at the right age.

The code:
i06=: ".}: freads'06.txt' NB. }: discards trailing LF
NB. state encoding: count of fishes with age i.9 (since never more than 8).
Appends i.9 to ensure ordering, compensated by <: . x: just to be sure.
enc=: [: x:@<: [: #/.~ (i.9) , ]
NB. rotate by one, plus the new fishes ({.) at the right age (6),
blf=bioluminescent fishes ;).
blf=: [: +/ ((  ((6=i.9)*{.) + }.,{.)@]^:[  enc)
a06=: 80&blf
b06=: 256&blf

(a06;b06)i06


On Mon, Dec 27, 2021, 15:39 Raul Miller <[email protected]> wrote:

> That's a really good insight.
>
> Conceptually, this would be
>    1 (<0 6)}(=/1|.])i.9
> 0 0 0 0 0 0 1 0 1
> 1 0 0 0 0 0 0 0 0
> 0 1 0 0 0 0 0 0 0
> 0 0 1 0 0 0 0 0 0
> 0 0 0 1 0 0 0 0 0
> 0 0 0 0 1 0 0 0 0
> 0 0 0 0 0 1 0 0 0
> 0 0 0 0 0 0 1 0 0
> 0 0 0 0 0 0 0 1 0
>
> But we want to multiply on the right (for ^:) instead of on the left.
> So, instead
>    M=: |:1 (<0 6)}(=/1|.])i.9
> or
>    M=: 1 (<6 0)}(=/~1|.])i.9
>
>    +/M+/ .*^:256 <:#/.~(i.9),3,4,3,1,2
> 26984457539
>
> Or, squaring M, 8 times:
>    +/(+/ .*~^:8 M)+/ .*<:#/.~(i.9),3,4,3,1,2
> 26984457539
>
> Actually, the squaring technique would have allowed us to left
> multiply our representation of the updates:
>
>    +/(<:#/.~(i.9),3,4,3,1,2)+/ .*+/ .*~^:8]1(<0 6)}(=/1|.])i.9
> 26984457539
>
> Pretty concepts.
>
> Thanks,
>
> --
> Raul
>
> On Mon, Dec 27, 2021 at 4:08 AM 'Mike Day' via Programming
> <[email protected]> wrote:
> >
> > Yes,  a 9x9 Boolean matrix with an offset 1s diagonal and an extra 1.
> It might have been worth using the high-power squaring technique,  if the
> power were really large, but for 256 I just took the direct 256th power.
> >
> > Cheers,
> >
> > Mike
> >
> > Sent from my iPad
> >
> > > On 27 Dec 2021, at 06:17, Raul Miller <[email protected]> wrote:
> > >
> > > 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
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to