AdventOfCode2020/Advent Of Code Day 11-part1.ps1

59 lines
1.7 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
)
$seatrow.gettype()
$seatnum.gettype()
$seatrow = [int]$seatrow
$seatnum = [int]$seatnum
$seatrow.gettype()
$seatnum.gettype()
write-host "Input: $seatrow , $seatnum"
$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
if($seatmap[$row][$num] -eq '#'){
$occupiedSeats++
}
}
return $occupiedSeats
}
2020-12-11 11:08:30 -06:00
function proc-Seats(){
param(
$seatmap
)
for($row=0;$row -lt $seatmap.count; $row++){
for($seatnum=0, $seatnum -lt $seatmap[$row].ToCharArray().count){
if((Get-OpenSeatCount -seatrow $row -seatnum $seatnum -seatmap $seatmap) -ge 4){
$seatmap[$row][$seatnum]='L'
} else {
$seatmap[$row][$seatnum]='#'
}
}
}
$seatmap
}
2020-12-11 10:45:09 -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
)