I think this one may have been so easy nobody had anything to say about it. But 
for completeness, here are my remarks.

The problem in part one is to find lines whose sequence of words contain no 
duplicates. This is pretty straightforward, I just nub the split array and see 
if the lengths match. I made this overcomplicated by using 13 : to condense it:

   input =. cutLF fread '~/Desktop/input'
   valid =. [: (# = [: # ~.) [: cut ' ' , ]
   +/ >valid&.> input

Part two modifies the requirements by saying anagrams count as the same word. 
For part 2, I do almost the same thing, but to find the anagrams I just sort 
the characters of each word and then nub. If the words are anagrams, they’ll 
have the same sorted representation.

I have a mixture of pride and horror here. I felt like I should have been able 
to use =&# above, but now that I have, it looks a bit long:

   +/ ([ =&# ([: ~. /:~&.>))@:([: cut ' ', ])&> input

The cut works the same way here. @: to signify a separate step. The left side 
train has two parts: I’m saying the input equals-under-length the input sorted 
(unbox the boxes), nubbed. Cap is necessary here because I only have two verbs. 
This still looks like more work than is necessary to my beginner eyes.

Overall I like it. I’m happy it was quick to slap together!

A neat thing I’ve noticed about J: while I struggle sometimes to come up with 
the solution, when I do, it seems to be right on the first try. I haven’t had a 
single one of my advent-of-code solutions kicked back. This is a nice property!

-- 
Daniel Lyons




----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to