Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- input = Import[ NotebookDirectory[] <> "input/input_08.txt"] //
- StringSplit[ # , "\n"] &;
- instr = StringCases[
- input , {
- "rect " ~~ a : NumberString ~~ "x" ~~ b : NumberString :> {rect,
- ToExpression /@ {a, b}} ,
- "rotate column x=" ~~ x : NumberString ~~ " by " ~~
- xn : NumberString :> { col, ToExpression /@ {x, xn} + {1, 0}},
- "rotate row y=" ~~ y : NumberString ~~ " by " ~~
- yn : NumberString :> { row,
- ToExpression /@ {y, yn} + { 1, 0}} }] //
- Flatten[ # , { {1, 2} , {3}}] & ;
- step[ array_ , { rect , {a_ , b_ } }] :=
- Module[ {inside} , inside = array ;
- inside [[1 ;; b , 1 ;; a ]] = ConstantArray [1 , { b , a } ];
- inside]
- step[ array_ , { col, { x_ , xn_ } }] :=
- Module[ {inside} , inside = array ;
- inside [[All , x ]] = RotateRight[ array [[All, x ]], xn] ; inside]
- step[ array_ , { row, { y_ , yn_ } }] :=
- Module[ {inside} , inside = array ;
- inside [[y ]] = RotateRight[ array [[y ]], yn] ; inside]
- init = ConstantArray[ 0 , { 6, 50 }];
- (output = Fold[ step , init , instr ] ) // ArrayPlot
- Total@Flatten@output
- animate =
- FoldList[ step , init , instr] //
- Map[ ArrayPlot[ # ,
- ColorRules -> { 0 -> Black , 1 -> Darker@Green }] &] //
- Export["/tmp/animate.gif" , # ,
- "DisplayDurations" ->
- ConstantArray[ .1 , Length@# - 1]~Join~{4}] &
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement