Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [CmdletBinding()]
- Param(
- [string[]]
- $rules,
- [int]$loops=18
- )
- if (-not $rules) {
- $rules = gc .\day21.txt
- #$rules = "../.# => ##./#../...%.#./..#/### => #..#/..../..../#..#" -split "%"
- }
- class grid {
- [char[, ]]$items
- [int]$size
- grid ([int] $size) {
- $this.size = $size
- $this.items = New-Object "char[,]" $size, $size
- }
- grid ([int]$size, [string]$data) {
- $this.size = $size
- $this.items = New-Object "char[,]" $size, $size
- $chars = $data.ToCharArray()
- for ( $i = 0; $i -lt $chars.Count; $i++ ) {
- $x = 0
- $y= [system.math]::DivRem($i,$size,[ref]$x)
- $this.items[$x, $y] = $chars[$i]
- }
- }
- [grid]flipx(){
- $cs = for ( $y = 0; $y -lt $this.size; $y++ ) {
- for ( $x = ($this.size-1); $x -ge 0; $x-- ){
- $this.items[$x, $y]
- }
- }
- return [grid]::new($this.size,($cs-join''))
- }
- [grid]flipy(){
- $cs = for ( $y = ($this.size-1); $y -ge 0; $y-- ) {
- for ( $x = 0; $x -lt $this.size; $x++ ){
- $this.items[$x, $y]
- }
- }
- return [grid]::new($this.size,($cs-join''))
- }
- [grid]flipxy(){
- $cs = for ( $y = ($this.size-1); $y -ge 0; $y-- ) {
- for ( $x = ($this.size-1); $x -ge 0; $x-- ){
- $this.items[$x, $y]
- }
- }
- return [grid]::new($this.size,($cs-join''))
- }
- [grid]rotateCW(){
- $cs = for ( $y = ($this.size-1); $y -ge 0; $y-- ) {
- for ( $x = ($this.size-1); $x -ge 0; $x-- ){
- $this.items[(($this.size-1)-$y), $x]
- }
- }
- return [grid]::new($this.size,($cs-join''))
- }
- [string]toStringGrid(){
- $cs = for ( $y = 0; $y -lt $this.size; $y++ ) {
- for ( $x = 0; $x -lt $this.size; $x++ ){
- $this.items[$y, $x]
- }
- "`n"
- }
- return ($cs -join '')
- }
- [string]toString(){
- $cs = for ( $y = 0; $y -lt $this.size; $y++ ) {
- for ( $x = 0; $x -lt $this.size; $x++ ){
- $this.items[$y, $x]
- }
- #"`n"
- }
- return ($cs -join '')
- }
- static[grid]join([grid[]] $grids){
- $gs = $grids[0].size
- $gridss = [system.math]::Sqrt( $grids.Count )
- $s = $gs * $gridss
- $newgrid = [grid]::new($s)
- for ( $i = 0; $i -lt $grids.Count; $i++ ) {
- $gridx = 0
- $gridy= [system.math]::DivRem($i,$gridss,[ref]$gridx)
- for ( $j = 0; $j -lt $gs*$gs; $j++ ){
- $x = 0
- $y= [system.math]::DivRem($j,$grids[$i].size,[ref]$x)
- $newgrid.items[
- ( ($gridx * $gs ) + $x ),
- ( ($gridy * $gs ) + $y )
- ] = $grids[$i].items[$x,$y]
- }
- }
- return $newgrid
- }
- [grid[]]split([int]$size){
- $gs = $size
- $gridss = $this.size / $size
- $newgrids = for ( $i = 0; $i -lt $gridss*$gridss; $i++ ) {
- $gridx = 0
- $gridy= [system.math]::DivRem($i,$gridss,[ref]$gridx)
- $childgrid = [grid]::new($gs)
- for ( $j = 0; $j -lt $gs*$gs; $j++ ){
- $x = 0
- $y= [system.math]::DivRem($j,$gs,[ref]$x)
- $childgrid.items[$x,$y] = $this.items[
- ( ($gridx * $gs ) + $x ),
- ( ($gridy * $gs ) + $y )
- ]
- }
- $childgrid
- }
- return $newgrids
- }
- }
- $subs = @{}
- $rules | % {
- $a=$_ -replace '/','' -split ' => '
- $subs[$a[0]] = $a[1]
- }
- #start
- $newgrid = [grid]::new(3,'.#...####')
- $script:cache = @{}
- function calc1 {
- Param(
- $subs,
- [grid]$grid
- )
- $split = $false
- $gs=$null
- if ($grid.size % 2 -eq 0){
- $gs = $grid.split(2)
- $split = $true
- } elseif ($grid.size % 3 -eq 0) {
- $gs = $grid.split(3)
- $split = $true
- } else {
- Write-Error "Can't split grid anymore."
- }
- if ($split){
- $newgrids = $gs | %{
- $str = $_.toString()
- if ( $script:cache[ $str ] ){
- $script:cache[ $str ]
- } else {
- $patterns = @(
- $str
- $_.flipx().ToString() #flipx
- $_.flipy().ToString() #flipy
- $_.flipxy().ToString() #rot180
- $_.rotateCW().ToString() # rot90
- $_.rotateCW().flipx().ToString() #flipxrot90
- $_.rotateCW().flipy().ToString() #flipyrot90
- $_.rotateCW().flipxy().ToString() # rot270
- )
- $reps = $patterns | %{
- if ($subs[$_.ToString()]){
- $size = [system.math]::Sqrt( $subs[$_.ToString()].length )
- [grid]::new($size,$subs[$_.ToString()])
- }
- }
- $return = if ($reps.Count -gt 1){
- $reps[0]
- } else {
- $reps
- }
- $patterns | %{
- if(-not ($script:cache[$_])){
- $script:cache[$_]=$return
- }
- }
- $return
- }
- }
- return [grid]::join($newgrids)
- }
- }
- 1..$loops| %{
- $time = Measure-Command {
- $newgrid = calc1 -subs $subs -grid $newgrid
- }
- "$_ : $($time.tostring())"
- }
- #>
- ($newgrid.toString() -replace "[.]",'').length
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement