From 86819638986ee38d0019cd73355548dfa6327be8 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 11 Dec 2020 16:12:50 -0600 Subject: [PATCH] part 1 complete! --- Advent Of Code Day 11-part1.ps1 | 102 ++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 31 deletions(-) diff --git a/Advent Of Code Day 11-part1.ps1 b/Advent Of Code Day 11-part1.ps1 index 4ea15bc..a31ba69 100644 --- a/Advent Of Code Day 11-part1.ps1 +++ b/Advent Of Code Day 11-part1.ps1 @@ -1,46 +1,79 @@ +Function Get-StringHash([String] $String,$HashName = "MD5") +{ + $StringBuilder = New-Object System.Text.StringBuilder + [System.Security.Cryptography.HashAlgorithm]::Create($HashName).ComputeHash([System.Text.Encoding]::UTF8.GetBytes($String))|%{ + [Void]$StringBuilder.Append($_.ToString("x2")) +} +$StringBuilder.ToString() +} function Get-OpenSeatCount { param ( [int]$seatrow, [int]$seatnum, $seatmap ) - $positionstocheck=@{} - $positionstocheck.add('UpperLeft',@(($seatrow -1),($seatnum -1))) - $positionstocheck.add('UpperMiddle',@(($seatrow -1),($seatnum))) - $positionstocheck.add('UpperRight',@(($seatrow -1),($seatnum +1))) - $positionstocheck.add('Left',@(($seatrow),($seatnum -1))) - $positionstocheck.add('Right',@(($seatrow),($seatnum +1))) - $positionstocheck.add('LowerLeft',@(($seatrow +1),($seatnum -1))) - $positionstocheck.add('LowerMiddle',@(($seatrow +1),($seatnum))) - $positionstocheck.add('LowerRight',@(($seatrow +1),($seatnum +1))) - $occupiedSeats=0 - foreach($position in $positionstocheck.keys){ - $row,$num = $positionstocheck.$position - $myrow = $seatmap[$row] - try{ - if($myrow[$num] -eq '#'){ - $occupiedSeats++ - } - } catch{ - - } + # write-host "$(Get-StringHash -String ($seatmap -join ''))" + $positionstocheck=@{ + 'UpperLeft'=@(($seatrow -1),($seatnum -1)) + 'UpperMiddle'=@(($seatrow -1),($seatnum)) + 'UpperRight'=@(($seatrow -1),($seatnum +1)) + 'Left'=@(($seatrow),($seatnum -1)) + 'Right'=@(($seatrow),($seatnum +1)) + 'LowerLeft'=@(($seatrow +1),($seatnum -1)) + 'LowerMiddle'=@(($seatrow +1),($seatnum)) + 'LowerRight'=@(($seatrow +1),($seatnum +1)) } - return [int]$occupiedSeats + #write-host $positionstocheck + #write-host "SEATROW: $seatrow SEATNUM: $seatnum" + $adjacentseats="" + foreach($position in $positionstocheck.keys){ + $thisrow, $thisseat = $positionstocheck[$position] + if($thisrow -eq -1){ + continue + } + if($thisrow -gt $seatmap.count -1){ + continue + } + if($thisseat -eq -1){ + continue + } + if($thisseat -gt $seatmap[$seatrow].length -1){ + continue + } + + $myposition = $positionstocheck[$position] + $row = $myposition[0] + #write-host "$row" + $seat = $myposition[1] + #write-host "$seat" + #write-host $seatmap[$row] + $myrow = $seatmap[$row].ToCharArray() + $adjacentseats += $myrow[$seat] + } + #write-host $adjacentseats + return ($adjacentseats.tochararray()|?{$_ -eq '#'}).count } function proc-Seats(){ param( $seatmap ) + $origmap = $seatmap.clone() + + #$myseatmap= $seatmap.clone() + #$myseatmap=$seatmap for($row=0;$row -lt $seatmap.count; $row++){ for($seatnum=0; $seatnum -lt ($seatmap[$row].ToCharArray().count); $seatnum++){ if($seatmap[$row][$seatnum] -ne '.'){ - $occupiedseats = Get-OpenSeatCount -seatrow $row -seatnum $seatnum -seatmap $seatmap + $occupiedseats = Get-OpenSeatCount -seatrow $row -seatnum $seatnum -seatmap $origmap #write-host "Occupied Around $occupiedseats, SEATNUM $seatnum, SEATROW $row" - $myrow = $seatmap[$row].tochararray() if( $occupiedseats -ge 4){ + #write-host $occupiedseats + $myrow = $seatmap[$row].tochararray() $myrow[$seatnum]='L' $seatmap[$row]=$myrow -join "" - } else { + } elseif ($occupiedseats -eq 0) { + #write-host $occupiedseats + $myrow = $seatmap[$row].tochararray() $myrow[$seatnum]='#' $seatmap[$row]=$myrow -join "" } @@ -50,18 +83,25 @@ function proc-Seats(){ $seatmap } $seatmap = @(get-content .\aocd11input.txt) -$nochangesin=0 +#$seatmap=@('L.LL.LL.LL','LLLLLLL.LL','L.L.L..L..','LLLL.LL.LL','L.LL.LL.LL','L.LLLLL.LL','..L.L.....','LLLLLLLLLL','L.LLLLLL.L','L.LLLLL.LL') +$repeat=$false $loopnum=0 +$optionhashes = @() do{ + $maphash =Get-StringHash -String ($seatmap -join '') $loopnum++ write-host "Loop $loopnum" - $oldseatmap = @($seatmap) - $seatmap = proc-Seats -seatmap $seatmap - if(($oldseatmap -join "") -eq ($seatmap -join "")){ - $nochangesin++ + write-host $maphash + $seatmap = @(proc-Seats -seatmap $seatmap) + $seatmap + if($optionhashes -contains $maphash ){ + write-host "Repeat detected. Stopping" + $repeat=$true + } else { + $optionhashes += $maphash } - #$seatmap + #read-host } while ( - $nochangesin -lt 3 + $repeat -eq $false ) (($seatmap -join "").tochararray()|?{$_ -eq '#'}).count