I need to learn how to recurse and fold in J. If a problem strikes my brain as 
requiring iteration to solve, and a solution with the power conjunction (^:) 
isn't obvious, I quickly resort to the explicit control structures as in my 
solution below.

Mike 

parse =: "."0 ;._2 

freqs =: # (- ,. ]) +/

power =: {{)m
  yfreqs  =. freqs y
  gamma   =. #. </"1 yfreqs
  epsilon =. #. >/"1 yfreqs
  gamma ; epsilon ; gamma * epsilon
}}

search =: {{)a
  mask   =. = u/@:freqs
  filter =. mask@:({~"1) # [

  report   =. y
  position =. 0
  while. (# report) > 1 do.
    report   =. report filter position
    position =. >: position
  end.
  #. report
}}

o2   =: <: search
co2  =: > search
life =: o2 * co2


--
Michael P. Manti
[email protected]

> On Dec 24, 2021, at 05:31, Stefan Baumann <[email protected]> wrote:
> 
> Hello Raul,
> Not day 2 but the day 3 puzzle lead me to practicing the fold conjunction:
> 
> rd=: "."0;._2
> ]d=: rd 0 : 0
> 00100
> 11110
> 10110
> 10111
> 10101
> 01111
> 00111
> 11100
> 10000
> 11001
> 00010
> 01010
> )
> (*&#. -.) (-:@# < +/) d NB. (*) power consumption
> NB. Rating adverb r: u=.<: oxygen generator, u=.> CO2 scrubber
> r=: {{ x #.F..((] #~ [: (= -:@# u +/) {"1)^:(1<#@])) y }}
> ((<:r * >r) i.@{:@$) d  NB. (**) life support rating
> 
> Was only wondering if the r adverb could be written tacitly - currently
> have a hard time understanding
> these modifier trains...
> 
> Thanks. Stefan.
> 
> 
> 
> 
>> On Thu, Dec 23, 2021 at 5:35 PM Raul Miller <[email protected]> wrote:
>> 
>> previous message:
>> http://jsoftware.com/pipermail/programming/2021-December/059435.html
>> AoC day 3 details: https://adventofcode.com/2021/day/3
>> 
>> This is a somewhat cleaned up version of my AoC day 3 implementation.
>> (This was my first "midnight attempt" which lends itself to illegible
>> code.)
>> 
>> parse=: {{_".1j1#y}};._2
>> sample=: parse {{)n
>> 00100
>> 11110
>> 10110
>> 10111
>> 10101
>> 01111
>> 00111
>> 11100
>> 10000
>> 11001
>> 00010
>> 01010
>> }}
>> 
>> NB. "part a"
>> a3=: powerConsumption=:{{
>>  gamma=. (0.5 < +/%#) y
>>  (*&#. -.) gamma
>> }}
>> 
>> NB. "part b"
>> b3=: lifeSupportRating=:{{
>>  (<: filter y) *&#. > filter y
>> }}
>> 
>> NB. recursive
>> filter=:{{
>>  if. 1=#y do. ,y return.end.
>>  bits=. {."1 y
>>  bit=. (0.5 u +/%#) bits
>>  if. 1 < {:$y do.
>>    bit,u filter (bit=bits)#}."1 y
>>  else.
>>    bit
>>  end.
>> }}
>> 
>> The part b puzzle smells an awful lot like a median or mode
>> calculation, and there might be a simpler approach.
>> 
>> Nothing fancy here, but it works...
>> 
>> --
>> 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

Reply via email to