I like this approach,
Actually, we could ignore 8 (because that's just "all segments"), but
if we are using intersect ([ -. -.) then 8 is our neutral value.
dcnt=:#@~.@-.&' .'@,S:0
unpack=:{{(b#|:y)<@|:/.~b#+/\-.b=.-.*/y=' '}}
digits=:unpack ];._2 {{)n
aaaa .... aaaa aaaa .... aaaa aaaa aaaa aaaa aaaa
b c . c . c . c b c b . b . . c b c b c
b c . c . c . c b c b . b . . c b c b c
.... .... dddd dddd dddd dddd dddd .... dddd dddd
e f . f e . . f . f . f e f . f e f . f
e f . f e . . f . f . f e f . f e f . f
gggg .... gggg gggg .... gggg gggg .... gggg gggg
}}
Here, we see that the intersection of each digit with digits 1 4 and 8
results in a unique combination of lit segment counts:
~.dcnt"1&.|: digits ([ -. -.)&,each/ 1 4 8{digits
2 3 6
2 2 2
1 2 5
2 3 5
2 4 4
1 3 5
1 3 6
2 2 3
2 4 7
2 4 6
Another alternative would be to use difference instead of intersection
-- here, instead of 8, we would use a: (or we could use counts taken
separately):
dcnt"1&.|: digits -.&,each/ a:,1 4{digits
6 4 3
2 0 0
5 4 3
5 3 2
4 2 0
5 4 2
6 5 3
3 1 1
7 5 3
6 4 2
I think this requires a lookup table, but it allows for
NB. construct seven segment digit lookup table
intuit=:{{
segs=. 10{. ;:^:(0=L.) y
ref=. 10#.#@>segs ([-.-.)each/segs{~2 4 7 i.~#@>segs
segs /: 236 222 125 235 244 135 136 223 247 246 i.ref
}}
And, with this defined, aoc8b becomes:
b8=: {{+/ {{10#.(intuit y) i.&:(/:~each) _4{.;:y}};._2 y}}
or (fully tacit):
b8=: [: +/ (10#.intuit i.&:(/:~each) _4{.;:);._2
Or, I suppose I could have pushed the lookup of output into that first
definition:
decode=:{{
segs=. 10{. all=. ;:^:(0=L.) y-. LF NB. ^: for easy interactive testing
ref=. 10#.#@>segs ([-.-.)each/segs{~2 4 7 i.~#@>segs
tbl=. segs /: 236 222 125 235 244 135 136 223 247 246 i.ref
10#.tbl i.&:(/:~each) _4{.all
}}
Here, instead of producing a lookup table, I produce the numeric value
of the output column. (And, I would probably phrase this slightly
different if I did not have to worry about email line length
restrictions).
Thanks,
--
Raul
On Sat, Jan 1, 2022 at 12:55 PM Stefan Baumann <[email protected]> wrote:
>
> Solved part 1 similarly.
> Not sure if I've got something new for part 2, but my main insight was that
> each number is uniquely defined by it's "segment intersections" with the
> numbers 1, 4 and 8. Calculating those from a normalized input and applying
> them to the left side gives me the numbers represented. Those can finally
> be applied to the right side - code follows:
>
> rd=: ([: /:~&.> ;:);._2 NB. Includes sorting of "words"
> d=: rd 0 : 0
> be cfbegad cbdgef fgaecd cgeb fdcge agebfd fecdb fabcd edb | fdgacbe cefdb
> cefbgd gcbe
> edbfga begcd cbg gc gcadebf fbgde acbgfd abcde gfcbed gfec | fcgedb cgb
> dgebacf gc
> fgaebd cg bdaec gdafb agbcfd gdcbef bgcad gfac gcb cdgabef | cg cg fdcagb
> cbg
> fbegcd cbd adcefb dageb afcb bc aefdc ecdab fgdeca fcdbega | efabcd cedba
> gadfec cb
> aecbfdg fbg gf bafeg dbefa fcge gcbea fcaegb dgceab fcbdga | gecf egdcabf
> bgf bfgea
> fgeab ca afcebg bdacfeg cfaedg gcfdb baec bfadeg bafgc acf | gebdcfa ecba
> ca fadegcb
> dbcfg fgd bdegcaf fgec aegbdf ecdfab fbedc dacgb gdcebf gf | cefg dcbef
> fcge gbcadfe
> bdfegc cbegaf gecbf dfcage bdacg ed bedf ced adcbefg gebcd | ed bcgafe
> cdgba cbgef
> egadfb cdbfeg cegd fecab cgb gbdefca cg fgcdab egfdb bfceg | gbdfcae bgc cg
> cgb
> gcafb gcf dcaebfg ecagb gf abcdeg gaef cafbge fdbac fegbdc | fgae cfgab fg
> bagce
> )
> +/, 2 4 3 7 =/"1 #&> _4&{."1 d NB. (*) 1 4 7 8 have 2 4 3 7 segments
> resp.
> 26
> n=: ;: 'abcefg cf acdeg acdfg bcdf abdfg abdefg acf abcdefg abcdfg'
> NB. i ~ Matrix of number of "segment intersections" with 1 4 8
> i=: (+/@e.~&>/ 1 4 8&{) n
> NB. r ~ Matrix of numbers represented by the "words" on the left side
> r=: i i."2 (+/@e.~&>/"1 ({"1~ 2 4 7 i."1~ #&>)) 10 {."1 d
> +/ 10&#. r {"1~ (10&{. i."(1) _4&{.)"1 d NB. (**)
> 61229
>
> Thanks. Stefan.
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm