Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. input = Import[ NotebookDirectory[] <> "input/input_08.txt"] //
  2. StringSplit[ # , "\n"] &;
  3.  
  4. instr = StringCases[
  5. input , {
  6. "rect " ~~ a : NumberString ~~ "x" ~~ b : NumberString :> {rect,
  7. ToExpression /@ {a, b}} ,
  8. "rotate column x=" ~~ x : NumberString ~~ " by " ~~
  9. xn : NumberString :> { col, ToExpression /@ {x, xn} + {1, 0}},
  10. "rotate row y=" ~~ y : NumberString ~~ " by " ~~
  11. yn : NumberString :> { row,
  12. ToExpression /@ {y, yn} + { 1, 0}} }] //
  13. Flatten[ # , { {1, 2} , {3}}] & ;
  14.  
  15. step[ array_ , { rect , {a_ , b_ } }] :=
  16. Module[ {inside} , inside = array ;
  17. inside [[1 ;; b , 1 ;; a ]] = ConstantArray [1 , { b , a } ];
  18. inside]
  19. step[ array_ , { col, { x_ , xn_ } }] :=
  20. Module[ {inside} , inside = array ;
  21. inside [[All , x ]] = RotateRight[ array [[All, x ]], xn] ; inside]
  22. step[ array_ , { row, { y_ , yn_ } }] :=
  23. Module[ {inside} , inside = array ;
  24. inside [[y ]] = RotateRight[ array [[y ]], yn] ; inside]
  25.  
  26. init = ConstantArray[ 0 , { 6, 50 }];
  27.  
  28. (output = Fold[ step , init , instr ] ) // ArrayPlot
  29. Total@Flatten@output
  30.  
  31. animate =
  32. FoldList[ step , init , instr] //
  33. Map[ ArrayPlot[ # ,
  34. ColorRules -> { 0 -> Black , 1 -> Darker@Green }] &] //
  35. Export["/tmp/animate.gif" , # ,
  36. "DisplayDurations" ->
  37. ConstantArray[ .1 , Length@# - 1]~Join~{4}] &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement