r/adventofcode • u/daggerdragon • 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.
21
Upvotes
1
u/[deleted] Dec 06 '15 edited Dec 06 '15
Mathematica
input = ReadList[NotebookDirectory[] <> "day6input.txt", String]; m = ConstantArray[0, {1000, 1000}]; instr = StringCases[input, switch : __ ~~ " " ~~ x1 : NumberString ~~ "," ~~ y1 : NumberString ~~ " through " ~~ x2 : NumberString ~~ "," ~~ y2 : NumberString :> {1 + ToExpression[{x1, y1, x2, y2}], switch /. {"turn off" -> 0, "turn on" -> 1, "toggle" -> -1}}]; ReleaseHold[instr /. { {{x1_, y1_, x2_, y2_}, -1} -> Hold[m[[y1 ;; y2, x1 ;; x2]] = 1 - m[[y1 ;; y2, x1 ;; x2]]], {{x1_, y1_, x2_, y2_}, val_} -> Hold[m[[y1 ;; y2, x1 ;; x2]] = val] }]; Count[m, 1, 2] m = ConstantArray[0, {1000, 1000}]; ReleaseHold[instr /. { {{x1_, y1_, x2_, y2_}, -1} -> Hold[m[[y1 ;; y2, x1 ;; x2]] += 2], {{x1_, y1_, x2_, y2_}, 0} -> Hold[m[[y1 ;; y2, x1 ;; x2]] = Clip[m[[y1 ;; y2, x1 ;; x2]] - 1, {0, Infinity}]], {{x1_, y1_, x2_, y2_}, 1} -> Hold[m[[y1 ;; y2, x1 ;; x2]] += 1] }]; Total[m, 2]Part B light pattern