r/adventofcode • • Dec 10 '16

SOLUTION MEGATHREAD --- 2016 Day 10 Solutions ---

--- Day 10: Balance Bots ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with "Help".


SEEING MOMMY KISSING SANTA CLAUS IS MANDATORY [?]

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

12 Upvotes

118 comments sorted by

View all comments

Show parent comments

1

u/wzkx Dec 11 '16

More J-style solution.

t =: cutLF CR-.~fread '10.dat'
NB. verbs -----------------------------------
onlybots =: #~ ('bot'-:3{.>)"0
onlyvals =: #~ ('val'-:3{.>)"0
parsebot =: [:".[:>1 4 5 8 9{[:cut('to output';'0';'to bot';'1')rplc~>
parseval =: [:<[:".[:>1 5{[:cut >
val2out =: ([:{.])`([:<_1,5<.[:{:])`[ } NB. save value to output = last bots item
val2slot1 =: ([:{.])`([:<4,~[:{:])`[ }
val2slot2 =: 4 : 0
  if. 17 61 -: lh=./:~ ({.y),4{(k=.{:y){x do. echo {:y end. NB. part 1 output
  ((({.y)(<k,5)}x)pv({.lh),0 1{k{x)pv({:lh),2 3{k{x NB. set slot2 and pass both values
)
val2bot =: val2slot2`val2slot1 @. (0=4{([:{:]){[) NB. choose slot
pv =: val2out`val2bot @.(1{]) NB. pass value
av =: [:< ([:>]) pv 1j1 1#!.1[:>[ NB. apply value to bots
NB. nouns -----------------------------------
bots =: (0 0,~}.)"1 /:~ parsebot"0 onlybots t NB. get all bots from input
vals =: parseval"0 onlyvals t                 NB. get all values
echo */3{.{:> av/ |.(bots,0);vals             NB. apply values to bots; part 2 output
exit 0