AdventOfCode2020/Advent Of Code Day 11-part1.ps1

60 lines
1.9 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]
if($myrow[$num] -eq '#'){
2020-12-11 10:45:09 -06:00
$occupiedSeats++
}
}
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:02:06 -06:00
for($row=0;$row -lt $seatmap.count - 1; $row++){
for($seatnum=0; $seatnum -lt ($seatmap[$row].ToCharArray().count -1); $seatnum++){
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
do{
$oldseatmap = $seatmap
$seatmap = proc-Seats -seatmap $seatmap
if($oldseatmap -eq $seatmap){
$nochangesin++
}
} while (
$nochangesin -lt 3
)