AdventOfCode2020/Advent Of Code Day 11-part1.ps1

68 lines
2.1 KiB
PowerShell
Raw Normal View History

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
)
$positionstocheck=@{}
2020-12-11 11:08:30 -06:00
$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)))
2020-12-11 10:45:09 -06:00
$occupiedSeats=0
foreach($position in $positionstocheck.keys){
$row,$num = $positionstocheck.$position
2020-12-11 12:02:06 -06:00
$myrow = $seatmap[$row]
2020-12-11 12:19:10 -06:00
try{
if($myrow[$num] -eq '#'){
$occupiedSeats++
}
} catch{
2020-12-11 10:45:09 -06:00
}
}
2020-12-11 12:02:06 -06:00
return [int]$occupiedSeats
2020-12-11 10:45:09 -06:00
}
2020-12-11 11:08:30 -06:00
function proc-Seats(){
param(
$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 '.'){
$occupiedseats = Get-OpenSeatCount -seatrow $row -seatnum $seatnum -seatmap $seatmap
#write-host "Occupied Around $occupiedseats, SEATNUM $seatnum, SEATROW $row"
$myrow = $seatmap[$row].tochararray()
if( $occupiedseats -ge 4){
$myrow[$seatnum]='L'
$seatmap[$row]=$myrow -join ""
} else {
$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 11:08:30 -06:00
$nochangesin=0
2020-12-11 12:19:10 -06:00
$loopnum=0
2020-12-11 11:08:30 -06:00
do{
2020-12-11 12:19:10 -06:00
$loopnum++
write-host "Loop $loopnum"
$oldseatmap = @($seatmap)
2020-12-11 11:08:30 -06:00
$seatmap = proc-Seats -seatmap $seatmap
2020-12-11 12:19:10 -06:00
if(($oldseatmap -join "") -eq ($seatmap -join "")){
2020-12-11 11:08:30 -06:00
$nochangesin++
}
2020-12-11 12:19:10 -06:00
#$seatmap
2020-12-11 11:08:30 -06:00
} while (
$nochangesin -lt 3
)
2020-12-11 12:19:10 -06:00
(($seatmap -join "").tochararray()|?{$_ -eq '#'}).count