Clever use of the state machine. I enjoy seeing those... Here's mine:
NB. part 1 # (,@((#,{.);.1~ _1&(|.!._) ~: ]))^:40 input=:(1 1 1 3 2 2 2 1 1 3) 252594 NB. part 2 # (,@((#,{.);.1~ _1&(|.!._) ~: ]))^:50 input=:(1 1 1 3 2 2 2 1 1 3) 3579328 To explain, we are going to use cut to split the array when the number is different than the previous number Return 1 if the number is different than the previous number, otherwise 0 (_1&(|.!._) ~: ]) (1 1 1 3 2 2 2 1 1 3) 1 0 0 1 1 0 0 1 0 1 Apply cut to the sequence to cut into intervals (<;.1~ _1&(|.!._) ~: ]) (1 1 1 3 2 2 2 1 1 3) ┌─────┬─┬─────┬───┬─┐ │1 1 1│3│2 2 2│1 1│3│ └─────┴─┴─────┴───┴─┘ Instead of boxing, return count of the first item in each interval ((#,{.);.1~ _1&(|.!._) ~: ]) input=:(1 1 1 3 2 2 2 1 1 3) 3 1 1 3 3 2 2 1 1 3 from here, just ravel and count the numbers On Thu, Dec 10, 2015 at 3:55 AM, Ryan Eckbo <ec...@cim.mcgill.ca> wrote: > Oops, typo in the title - this is for day 10, not 8. > > On 10 Dec 2015, at 19:52, Ryan Eckbo wrote: > >> A previous advent answer got me thinking about state machines, so I wrote >> one for this problem. The extra initial state ruins the natural mapping >> between states and numbers, but I think it's unavoidable? Also someone >> could probably write a clever verb to generate the table. >> >> >> NB. state machine -- can be confusing because rows/states 1,2,3,.. >> represent numbers 0,1,2,.. >> S=: 0 10#: 10* ". }. [;._2 noun define >> 0 1 2 3 4 5 6 7 8 9 >> 1.1 2.1 3.1 4.1 5.1 6.1 7.1 8.1 9.1 10.1 NB. initial >> 1.0 2.2 3.2 4.2 5.2 6.2 7.2 8.2 9.2 10.2 NB. 0 >> 1.2 2.0 3.2 4.2 5.2 6.2 7.2 8.2 9.2 10.2 NB. 1 >> 1.2 2.2 3.0 4.2 5.2 6.2 7.2 8.2 9.2 10.2 NB. 2 >> 1.2 2.2 3.2 4.0 5.2 6.2 7.2 8.2 9.2 10.2 NB. 3 >> 1.2 2.2 3.2 4.2 5.0 6.2 7.2 8.2 9.2 10.2 NB. 4 >> 1.2 2.2 3.2 4.2 5.2 6.0 7.2 8.2 9.2 10.2 NB. 5 >> 1.2 2.2 3.2 4.2 5.2 6.2 7.0 8.2 9.2 10.2 NB. 6 >> 1.2 2.2 3.2 4.2 5.2 6.2 7.2 8.0 9.2 10.2 NB. 7 >> 1.2 2.2 3.2 4.2 5.2 6.2 7.2 8.2 9.0 10.2 NB. 8 >> 1.2 2.2 3.2 4.2 5.2 6.2 7.2 8.2 9.2 10.0 NB. 9 >> ) >> v=:[: ,@:((#,{.)every) (0;S)&;: >> smoutput $ v^:40 Input=: 1 1 1 3 2 2 2 1 1 3 >> smoutput $ v^:50 Input >> ---------------------------------------------------------------------- >> 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