2020-12-11 16:12:50 -06:00
|
|
|
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()
|
|
|
|
}
|
2020-12-11 10:45:09 -06:00
|
|
|
function Get-OpenSeatCount {
|
|
|
|
param (
|
|
|
|
[int]$seatrow,
|
2020-12-11 11:08:30 -06:00
|
|
|
[int]$seatnum,
|
|
|
|
$seatmap
|
2020-12-11 10:45:09 -06:00
|
|
|
)
|
2020-12-11 16:12:50 -06:00
|
|
|
# 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))
|
|
|
|
}
|
|
|
|
#write-host $positionstocheck
|
|
|
|
#write-host "SEATROW: $seatrow SEATNUM: $seatnum"
|
|
|
|
$adjacentseats=""
|
2020-12-11 10:45:09 -06:00
|
|
|
foreach($position in $positionstocheck.keys){
|
2020-12-11 16:12:50 -06:00
|
|
|
$thisrow, $thisseat = $positionstocheck[$position]
|
|
|
|
if($thisrow -eq -1){
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if($thisrow -gt $seatmap.count -1){
|
|
|
|
continue
|
2020-12-11 10:45:09 -06:00
|
|
|
}
|
2020-12-11 16:12:50 -06:00
|
|
|
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]
|
2020-12-11 10:45:09 -06:00
|
|
|
}
|
2020-12-11 16:12:50 -06:00
|
|
|
#write-host $adjacentseats
|
|
|
|
return ($adjacentseats.tochararray()|?{$_ -eq '#'}).count
|
2020-12-11 10:45:09 -06:00
|
|
|
}
|
2020-12-11 11:08:30 -06:00
|
|
|
function proc-Seats(){
|
|
|
|
param(
|
|
|
|
$seatmap
|
|
|
|
)
|
2020-12-11 16:12:50 -06:00
|
|
|
$origmap = $seatmap.clone()
|
|
|
|
|
|
|
|
#$myseatmap= $seatmap.clone()
|
|
|
|
#$myseatmap=$seatmap
|
2020-12-11 12:19:10 -06:00
|
|
|
for($row=0;$row -lt $seatmap.count; $row++){
|
|
|
|
for($seatnum=0; $seatnum -lt ($seatmap[$row].ToCharArray().count); $seatnum++){
|
2020-12-11 12:02:06 -06:00
|
|
|
if($seatmap[$row][$seatnum] -ne '.'){
|
2020-12-11 16:12:50 -06:00
|
|
|
$occupiedseats = Get-OpenSeatCount -seatrow $row -seatnum $seatnum -seatmap $origmap
|
2020-12-11 12:02:06 -06:00
|
|
|
#write-host "Occupied Around $occupiedseats, SEATNUM $seatnum, SEATROW $row"
|
|
|
|
if( $occupiedseats -ge 4){
|
2020-12-11 16:12:50 -06:00
|
|
|
#write-host $occupiedseats
|
|
|
|
$myrow = $seatmap[$row].tochararray()
|
2020-12-11 12:02:06 -06:00
|
|
|
$myrow[$seatnum]='L'
|
|
|
|
$seatmap[$row]=$myrow -join ""
|
2020-12-11 16:12:50 -06:00
|
|
|
} elseif ($occupiedseats -eq 0) {
|
|
|
|
#write-host $occupiedseats
|
|
|
|
$myrow = $seatmap[$row].tochararray()
|
2020-12-11 12:02:06 -06:00
|
|
|
$myrow[$seatnum]='#'
|
|
|
|
$seatmap[$row]=$myrow -join ""
|
|
|
|
}
|
2020-12-11 11:08:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$seatmap
|
|
|
|
}
|
2020-12-11 12:02:06 -06:00
|
|
|
$seatmap = @(get-content .\aocd11input.txt)
|
2020-12-11 16:12:50 -06:00
|
|
|
#$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
|
2020-12-11 12:19:10 -06:00
|
|
|
$loopnum=0
|
2020-12-11 16:12:50 -06:00
|
|
|
$optionhashes = @()
|
2020-12-11 11:08:30 -06:00
|
|
|
do{
|
2020-12-11 16:12:50 -06:00
|
|
|
$maphash =Get-StringHash -String ($seatmap -join '')
|
2020-12-11 12:19:10 -06:00
|
|
|
$loopnum++
|
|
|
|
write-host "Loop $loopnum"
|
2020-12-11 16:12:50 -06:00
|
|
|
write-host $maphash
|
|
|
|
$seatmap = @(proc-Seats -seatmap $seatmap)
|
|
|
|
$seatmap
|
|
|
|
if($optionhashes -contains $maphash ){
|
|
|
|
write-host "Repeat detected. Stopping"
|
|
|
|
$repeat=$true
|
|
|
|
} else {
|
|
|
|
$optionhashes += $maphash
|
2020-12-11 11:08:30 -06:00
|
|
|
}
|
2020-12-11 16:12:50 -06:00
|
|
|
#read-host
|
2020-12-11 11:08:30 -06:00
|
|
|
} while (
|
2020-12-11 16:12:50 -06:00
|
|
|
$repeat -eq $false
|
2020-12-11 11:08:30 -06:00
|
|
|
)
|
2020-12-11 12:19:10 -06:00
|
|
|
(($seatmap -join "").tochararray()|?{$_ -eq '#'}).count
|