Not having looked at the problem very closely, I notice the adverb is only
ever passed <: and >. So, I think a potential simplification could come
by instead conditionally negating the result of the comparison. That
would let you replace (= -:@# u +/) with (m = ] = -:@# > +/) (where m is 0
or 1), which is easier to express tacitly and could even be rewritten as a
verb.
(Or you could say u@:(] = ...), where u is either -. or ]. That would
also be easy to make tacit, as it is just ]: @: (] = ...), but is
obviously harder to make a verb out of.)
-E
On Fri, 24 Dec 2021, Stefan Baumann 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