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 {
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