part 1 complete!

This commit is contained in:
dan 2020-12-11 16:12:50 -06:00
parent b82ab61b2d
commit 8681963898

View File

@ -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 { function Get-OpenSeatCount {
param ( param (
[int]$seatrow, [int]$seatrow,
[int]$seatnum, [int]$seatnum,
$seatmap $seatmap
) )
$positionstocheck=@{} # write-host "$(Get-StringHash -String ($seatmap -join ''))"
$positionstocheck.add('UpperLeft',@(($seatrow -1),($seatnum -1))) $positionstocheck=@{
$positionstocheck.add('UpperMiddle',@(($seatrow -1),($seatnum))) 'UpperLeft'=@(($seatrow -1),($seatnum -1))
$positionstocheck.add('UpperRight',@(($seatrow -1),($seatnum +1))) 'UpperMiddle'=@(($seatrow -1),($seatnum))
$positionstocheck.add('Left',@(($seatrow),($seatnum -1))) 'UpperRight'=@(($seatrow -1),($seatnum +1))
$positionstocheck.add('Right',@(($seatrow),($seatnum +1))) 'Left'=@(($seatrow),($seatnum -1))
$positionstocheck.add('LowerLeft',@(($seatrow +1),($seatnum -1))) 'Right'=@(($seatrow),($seatnum +1))
$positionstocheck.add('LowerMiddle',@(($seatrow +1),($seatnum))) 'LowerLeft'=@(($seatrow +1),($seatnum -1))
$positionstocheck.add('LowerRight',@(($seatrow +1),($seatnum +1))) 'LowerMiddle'=@(($seatrow +1),($seatnum))
$occupiedSeats=0 'LowerRight'=@(($seatrow +1),($seatnum +1))
foreach($position in $positionstocheck.keys){
$row,$num = $positionstocheck.$position
$myrow = $seatmap[$row]
try{
if($myrow[$num] -eq '#'){
$occupiedSeats++
}
} catch{
}
} }
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(){ function proc-Seats(){
param( param(
$seatmap $seatmap
) )
$origmap = $seatmap.clone()
#$myseatmap= $seatmap.clone()
#$myseatmap=$seatmap
for($row=0;$row -lt $seatmap.count; $row++){ for($row=0;$row -lt $seatmap.count; $row++){
for($seatnum=0; $seatnum -lt ($seatmap[$row].ToCharArray().count); $seatnum++){ for($seatnum=0; $seatnum -lt ($seatmap[$row].ToCharArray().count); $seatnum++){
if($seatmap[$row][$seatnum] -ne '.'){ 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" #write-host "Occupied Around $occupiedseats, SEATNUM $seatnum, SEATROW $row"
$myrow = $seatmap[$row].tochararray()
if( $occupiedseats -ge 4){ if( $occupiedseats -ge 4){
#write-host $occupiedseats
$myrow = $seatmap[$row].tochararray()
$myrow[$seatnum]='L' $myrow[$seatnum]='L'
$seatmap[$row]=$myrow -join "" $seatmap[$row]=$myrow -join ""
} else { } elseif ($occupiedseats -eq 0) {
#write-host $occupiedseats
$myrow = $seatmap[$row].tochararray()
$myrow[$seatnum]='#' $myrow[$seatnum]='#'
$seatmap[$row]=$myrow -join "" $seatmap[$row]=$myrow -join ""
} }
@ -50,18 +83,25 @@ function proc-Seats(){
$seatmap $seatmap
} }
$seatmap = @(get-content .\aocd11input.txt) $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 $loopnum=0
$optionhashes = @()
do{ do{
$maphash =Get-StringHash -String ($seatmap -join '')
$loopnum++ $loopnum++
write-host "Loop $loopnum" write-host "Loop $loopnum"
$oldseatmap = @($seatmap) write-host $maphash
$seatmap = proc-Seats -seatmap $seatmap $seatmap = @(proc-Seats -seatmap $seatmap)
if(($oldseatmap -join "") -eq ($seatmap -join "")){ $seatmap
$nochangesin++ if($optionhashes -contains $maphash ){
write-host "Repeat detected. Stopping"
$repeat=$true
} else {
$optionhashes += $maphash
} }
#$seatmap #read-host
} while ( } while (
$nochangesin -lt 3 $repeat -eq $false
) )
(($seatmap -join "").tochararray()|?{$_ -eq '#'}).count (($seatmap -join "").tochararray()|?{$_ -eq '#'}).count