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

Reply via email to