r/adventofcode • • Dec 06 '15

SOLUTION MEGATHREAD --- Day 6 Solutions ---

--- Day 6: Probably a Fire Hazard ---

Post your solution as a comment. Structure your post like the Day Five thread.

22 Upvotes

171 comments sorted by

View all comments

Show parent comment

1

u/Godspiral Dec 07 '15

a much cooler J solution (not my own),

i =. <@('y =. y ' , ;@(<@('(' , ,&')');._2)@:(,&' '));._2 wd 'clippaste'
through =: (1000 1000 {. ({. ($&1)))/@(-~/\)@:-@(,:~ >:)                
turn =: 1 : 'u'                                                         
'`off on toggle' =: >`+.`~:                                             
+/@, 3 : i 1000 1000 $ 0                                                
For part 2, use                                                         
'`off on toggle' =: (0 >. -)`+`(+ +:)                                   

i reads input to create 300 lines like:

+---------------------------------------------+
|y =. y (turn)(on)(887,9)(through)(959,629)   |
+---------------------------------------------+
|y =. y (turn)(on)(454,398)(through)(844,448) |
+---------------------------------------------+
|y =. y (turn)(off)(539,243)(through)(559,965)|
+---------------------------------------------+

where all of those words are defined functions, and , is a builtin. y =. y... reassigns in place the result from the line. (y is the right parameter name to all explicit functions)

3 : i creates a 300 line function from the data.

the through function creates a 1000 by 1000 matrix where the rectangle is set to 1, and off on toggle are all boolean functions.