Advertisement
Guest User

Day21

a guest
Dec 21st, 2017
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. Param(
  3.     [string[]]
  4.     $rules,
  5.     [int]$loops=18
  6. )
  7.  
  8. if (-not $rules) {
  9.     $rules = gc .\day21.txt
  10.     #$rules = "../.# => ##./#../...%.#./..#/### => #..#/..../..../#..#" -split "%"
  11. }
  12.  
  13. class grid {
  14.     [char[, ]]$items
  15.     [int]$size
  16.     grid ([int] $size) {
  17.         $this.size = $size
  18.         $this.items = New-Object "char[,]" $size, $size
  19.     }
  20.     grid ([int]$size, [string]$data) {
  21.         $this.size = $size
  22.         $this.items = New-Object "char[,]" $size, $size
  23.         $chars = $data.ToCharArray()
  24.         for ( $i = 0; $i -lt $chars.Count; $i++ ) {
  25.             $x = 0
  26.             $y= [system.math]::DivRem($i,$size,[ref]$x)
  27.             $this.items[$x, $y] = $chars[$i]
  28.         }
  29.     }
  30.     [grid]flipx(){
  31.         $cs = for ( $y = 0; $y -lt $this.size; $y++ ) {
  32.             for ( $x = ($this.size-1); $x -ge 0; $x-- ){
  33.                 $this.items[$x, $y]
  34.             }
  35.         }
  36.         return [grid]::new($this.size,($cs-join''))
  37.     }
  38.     [grid]flipy(){
  39.         $cs = for ( $y = ($this.size-1); $y -ge 0; $y-- ) {
  40.             for ( $x = 0; $x -lt $this.size; $x++ ){
  41.                 $this.items[$x, $y]
  42.             }
  43.         }
  44.         return [grid]::new($this.size,($cs-join''))
  45.     }
  46.     [grid]flipxy(){
  47.         $cs = for ( $y = ($this.size-1); $y -ge 0; $y-- ) {
  48.             for ( $x = ($this.size-1); $x -ge 0; $x-- ){
  49.                 $this.items[$x, $y]
  50.             }
  51.         }
  52.         return [grid]::new($this.size,($cs-join''))
  53.     }
  54.     [grid]rotateCW(){
  55.         $cs = for ( $y = ($this.size-1); $y -ge 0; $y-- ) {
  56.             for ( $x = ($this.size-1); $x -ge 0; $x-- ){
  57.                 $this.items[(($this.size-1)-$y), $x]
  58.             }
  59.         }
  60.         return [grid]::new($this.size,($cs-join''))
  61.     }
  62.     [string]toStringGrid(){
  63.         $cs = for ( $y = 0; $y -lt $this.size; $y++ ) {
  64.             for ( $x = 0; $x -lt $this.size; $x++ ){
  65.                 $this.items[$y, $x]
  66.             }
  67.             "`n"
  68.         }
  69.         return ($cs -join '')
  70.     }
  71.     [string]toString(){
  72.         $cs = for ( $y = 0; $y -lt $this.size; $y++ ) {
  73.             for ( $x = 0; $x -lt $this.size; $x++ ){
  74.                 $this.items[$y, $x]
  75.             }
  76.             #"`n"
  77.         }
  78.         return ($cs -join '')
  79.     }
  80.     static[grid]join([grid[]] $grids){
  81.         $gs = $grids[0].size
  82.         $gridss = [system.math]::Sqrt( $grids.Count )
  83.         $s = $gs * $gridss
  84.         $newgrid = [grid]::new($s)
  85.         for ( $i = 0; $i -lt $grids.Count; $i++ ) {
  86.             $gridx = 0
  87.             $gridy= [system.math]::DivRem($i,$gridss,[ref]$gridx)
  88.             for ( $j = 0; $j -lt $gs*$gs; $j++ ){
  89.                 $x = 0
  90.                 $y= [system.math]::DivRem($j,$grids[$i].size,[ref]$x)
  91.                 $newgrid.items[
  92.                     ( ($gridx * $gs ) + $x ),
  93.                     ( ($gridy * $gs ) + $y )
  94.                 ] = $grids[$i].items[$x,$y]
  95.             }
  96.         }
  97.         return $newgrid
  98.     }
  99.     [grid[]]split([int]$size){
  100.         $gs = $size
  101.         $gridss = $this.size / $size
  102.         $newgrids = for ( $i = 0; $i -lt $gridss*$gridss; $i++ ) {
  103.             $gridx = 0
  104.             $gridy= [system.math]::DivRem($i,$gridss,[ref]$gridx)
  105.             $childgrid = [grid]::new($gs)
  106.             for ( $j = 0; $j -lt $gs*$gs; $j++ ){
  107.                 $x = 0
  108.                 $y= [system.math]::DivRem($j,$gs,[ref]$x)
  109.                 $childgrid.items[$x,$y] = $this.items[
  110.                     ( ($gridx * $gs ) + $x ),
  111.                     ( ($gridy * $gs ) + $y )
  112.                 ]
  113.             }
  114.             $childgrid
  115.         }
  116.         return $newgrids
  117.     }
  118. }
  119.  
  120. $subs = @{}
  121.  
  122. $rules | % {  
  123.     $a=$_ -replace '/','' -split ' => '
  124.     $subs[$a[0]] = $a[1]
  125. }
  126.  
  127. #start
  128. $newgrid = [grid]::new(3,'.#...####')
  129.  
  130. $script:cache = @{}
  131.  
  132. function calc1 {
  133.     Param(
  134.         $subs,
  135.         [grid]$grid
  136.     )
  137.     $split = $false
  138.     $gs=$null
  139.     if ($grid.size % 2 -eq 0){
  140.         $gs = $grid.split(2)
  141.         $split = $true
  142.     } elseif ($grid.size % 3 -eq 0) {
  143.         $gs = $grid.split(3)
  144.         $split = $true
  145.     } else {
  146.         Write-Error "Can't split grid anymore."
  147.     }
  148.     if ($split){
  149.         $newgrids = $gs | %{
  150.             $str = $_.toString()
  151.             if ( $script:cache[ $str ] ){
  152.                 $script:cache[ $str ]
  153.             } else {
  154.                 $patterns = @(
  155.                     $str
  156.                     $_.flipx().ToString() #flipx
  157.                     $_.flipy().ToString() #flipy
  158.                     $_.flipxy().ToString() #rot180
  159.                     $_.rotateCW().ToString() # rot90
  160.                     $_.rotateCW().flipx().ToString() #flipxrot90
  161.                     $_.rotateCW().flipy().ToString() #flipyrot90
  162.                     $_.rotateCW().flipxy().ToString() # rot270
  163.                 )
  164.                 $reps = $patterns | %{
  165.                     if ($subs[$_.ToString()]){
  166.                         $size = [system.math]::Sqrt( $subs[$_.ToString()].length )
  167.                         [grid]::new($size,$subs[$_.ToString()])
  168.                     }
  169.                 }
  170.                 $return = if ($reps.Count -gt 1){
  171.                     $reps[0]
  172.                 } else {
  173.                     $reps
  174.                 }
  175.                 $patterns | %{
  176.                     if(-not ($script:cache[$_])){
  177.                         $script:cache[$_]=$return
  178.                     }
  179.                 }
  180.                 $return
  181.             }
  182.         }
  183.         return [grid]::join($newgrids)
  184.     }
  185. }
  186.  
  187. 1..$loops| %{
  188.     $time = Measure-Command {
  189.         $newgrid = calc1 -subs $subs -grid $newgrid
  190.     }
  191.     "$_ : $($time.tostring())"
  192. }
  193. #>
  194. ($newgrid.toString() -replace "[.]",'').length
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement