-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day5.ps1
65 lines (56 loc) · 1.51 KB
/
Day5.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#
# https://adventofcode.com/2020
#
#
Start-Transcript .\Day5.log
$InputData = Get-Content .\Day5.data
Write-Host "$($InputData.Count) values to check !"
function GetSeat {
param (
[string]$SeatCode
)
$SeatCodeArray = $SeatCode.ToCharArray()
$MinRow = 0
$MaxRow = 127
for ($i = 0; $i -lt 7; $i++) {
if ($SeatCodeArray[$i] -eq "F") {
$MaxRow = $MaxRow - (($MaxRow - $MinRow + 1) / 2)
} elseif ($SeatCodeArray[$i] -eq "B") {
$MinRow = $MinRow + (($MaxRow - $MinRow + 1) / 2)
}
#Write-Host $i $MinRow $MaxRow
}
$MinCol = 0
$MaxCol = 7
for ($i = 7; $i -lt 10; $i++) {
if ($SeatCodeArray[$i] -eq "L") {
$MaxCol = $MaxCol - (($MaxCol - $MinCol + 1) / 2)
}
elseif ($SeatCodeArray[$i] -eq "R") {
$MinCol = $MinCol + (($MaxCol - $MinCol + 1) / 2)
}
#Write-Host $i $MinCol $MaxCol
}
Return $MaxRow * 8 + $MaxCol
}
$MaxSeatID = 0
foreach ($InputDataLine in $InputData) {
if ($MaxSeatID -lt $(GetSeat($InputDataLine))) {
$MaxSeatID = $(GetSeat($InputDataLine))
}
}
Write-Host "Max Seat ID = $MaxSeatID"
# Part 2
$Seats = New-Object int[] $MaxSeatID
foreach ($InputDataLine in $InputData) {
$Seats[$(GetSeat($InputDataLine))-1] = 1
}
$Start = $False
for ($i = 0; $i -lt $Seats.Count; $i++) {
if ($Seats[$i] -eq 0 -and $Start) {
Write-Host "My Seat $i"
} elseif ($Seats[$i] -ne 0) {
$Start = $true
}
}
Stop-Transcript