Using sorting is a great trick here, and using /. is just superb! Thank you for this great example!
On Mon, Dec 14, 2015 at 12:46 AM, Ryan Eckbo <[email protected]> wrote: > Here's my solution -- like Joe I also just modified earlier input to get > part 2's answer. > > NB. part 1 > Edges=: (0 9 2 { ;:@:}:);._2 'gain' delstring ('lose ';'_') rplc~ Input > ppl=: ~. {."1 Edges > EdgesD=: /:~ Edges, |: ppl,ppl,:<'0' NB. add diagonal elements > Tbl=: (".@>)/./ 0 2 { |: EdgesD NB. happiness relationaship table > happPair=: +&({&Tbl)&< |. NB. happPair 2 3 > happ=: +/@:(2 happPair\ (, {.)) NB. happ i.8 > smoutput happMax=: >./ happ"1 (i.@! A. i.) # Tbl > > NB. part 2 > Tbl=: Tbl {.~ >: 2##Tbl > happPair=: +&({&Tbl)&< |. > happ=: +/@:(2 happPair\ (, {.)) > smoutput happMax=: >./ happ"1 (i.@! A. i.) # Tbl > > > On 14 Dec 2015, at 4:41, Moon S wrote: > > So far these were simple tasks with clear algorithms, so it's interesting >> to play with the programs -- keep everything in verbs and be as lazy as >> possible (i.e. build one final function, that just gives the solution of >> the task being given the data; especially funny if the functions are >> tacit), or calculate whatever is possible as soon as possible. >> >> NB. happy dinner http://adventofcode.com/day/13 >> >> m=: ' '&cut & > cutLF ('.',CR) -.~ fread {:ARGV NB. matrix of input >> u=: ~. {."1 m NB. noun: unique names = all the ppl >> s=: _1 1{~'gain'-: >@:(2&{) NB. _1 for 'lose', 1 for 'gain' >> n=: ". @ > @ (3&{) NB. numeric value >> g=: 4 : 'v=.0$~2##x for_e. y do. v=. ((s*n)e) (< x i. 0 10{e) }v end. v' >> NB. calc matrix >> v=: u g m NB. noun: 'happiness' matrix; all data are >> parsed now >> >> h=: {&v @ < @ , NB. get 'happiness' for x -> y >> r=: 2 h/\ ] NB. one way relations betrween ppl in one >> combination >> w=: +/@r + +/@r@|. NB. total happiness of one combination >> p=: ([ , {.)"1@(i.@! A. i.) NB. all permutation with first column >> appended >> on the right >> echo >./ w"1 p #u >> >> h=: ({&v @ < @ ,) :: 0: NB. all missing 'happiness' values will be 0 >> echo >./ w"1 p >:#u NB. repeat calculation for one more (virtual) >> person >> >> exit 0 >> >> On Sun, Dec 13, 2015 at 5:07 PM, 'Pascal Jasmin' via Programming < >> [email protected]> wrote: >> >> with input on clipboard, >>> >>> version that works with both parts by returning 0 for index errors. t is >>> the lookup table that is maybe a tad sloppy. >>> >>> >>> in =. '.' -.~leaf(0 2 3 _1 { ])@;:@}:"1 a =. > cutLF wdclippaste '' >>> uni =. (~. {."1 in) , <'Me' >>> t =. (( (,~ -)@".@(2&{::) ({~ >@('gain' -: leaf ])) 1 { ]) ; 0 3&{) "1 >>> in >>> calc =. (({."1 t) {~ (}."1 t) I.@:(-:"1) ]) :: 0: >>> calcr =. (({."1 t) {~ (}."1 t) I.@:(-:"1) |.) :: 0: >>> perm =: i.@! A. i. >>> >>> >>> parts 1 and 2, >>> >>> ./ +/@:;@(,/)@((2 (calc , calcr)\ ]))@( uni {~ ] , {.)"1 ] perm 8 >>>> ./ +/@:;@(,/)@((2 (calc , calcr)\ ]))@( uni {~ ] , {.)"1 ] perm 9 >>>> >>> >>> >>> >>> ----- Original Message ----- >>> From: Joe Bogner <[email protected]> >>> To: [email protected] >>> Sent: Sunday, December 13, 2015 9:47 AM >>> Subject: [Jprogramming] advent 13 >>> >>> not pretty, but here is my solution >>> >>> pt2 was kind of hacky instead of rewriting the train to calculate, I >>> just reworked the input to add myself to it >>> >>> input =: fread 'c:/joe/lang-lab/j/advent2015/day13.txt' >>> parsed=: ' ' cut each cutLF input >>> >>> who =: {. every >>> sitBy =: ([: }: each {: every) >>> getScore=: [: (] -@]^:((<'lose') e. 0&{@[) ".@>@(1&{))"1 @: ((2,3)&{) >>> each ] >>> getChoices=: ([: (] A.~ i.@!@#) [: ~. who) >>> >>> getMax=: [: (>./)&:(+/"1) (>@getScore@] {~ (who,.sitBy) i. >>> (],|."1@])@((2]\ ])"1)@([: ({:,])"1 getChoices)) >>> >>> smoutput pt1=:getMax parsed >>> >>> addMe=: ((((;: 'would gain 0 happiness units sitting next to Me.') >>> (];[) ]) each) @ (~.&:who)) >>> addThem=: ((((;: 'Me would gain 0 happiness units sitting next to') >>> ([,<@]) ('.',~])) each) @ (~.&:who)) >>> >>> smoutput pt2=: getMax (] , (addMe,addThem)) parsed >>> >>> sample=: 0 : 0 >>> Alice would gain 54 happiness units by sitting next to Bob. >>> Alice would lose 79 happiness units by sitting next to Carol. >>> Alice would lose 2 happiness units by sitting next to David. >>> Bob would gain 83 happiness units by sitting next to Alice. >>> Bob would lose 7 happiness units by sitting next to Carol. >>> Bob would lose 63 happiness units by sitting next to David. >>> Carol would lose 62 happiness units by sitting next to Alice. >>> Carol would gain 60 happiness units by sitting next to Bob. >>> Carol would gain 55 happiness units by sitting next to David. >>> David would gain 46 happiness units by sitting next to Alice. >>> David would lose 7 happiness units by sitting next to Bob. >>> David would gain 41 happiness units by sitting next to Carol. >>> ) >>> ---------------------------------------------------------------------- >>> 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 >> > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
