-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdventOfCode - Day13.2.ps1
53 lines (51 loc) · 1.43 KB
/
AdventOfCode - Day13.2.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
$buses = new-object System.Collections.ArrayList
Get-Content -Path '.\bus lines day 13.txt' | Select-Object -Skip 1 | ForEach-OBject {
$tmp = New-Object System.Collections.ArrayList
$inputs = $_ -split ','
$inputs | ForEach-Object {
if ($_ -eq 'x') {
$tmp.Add($_)
} else {
if ($tmp.Count -gt 0) {
$buses.Add([String]::Join("", $tmp.ToArray()))
$tmp = New-Object System.Collections.ArrayList
}
$buses.Add($_)
}
}
}
[int64]$interval = [int64]::Parse($buses[0])
[int64]$startTime = 0
[int64]$lastStartTime = 0
[int64]$diff = 0
$valid = $false
$round = 1
$currentIx = 0
while (!$valid) {
$valid = $true
$expectedTime = $startTime +1
for ($i = 1; $i -lt $buses.Count; $i++) {
if ('x' -eq $buses[$i][0]) {
$expectedTime += [int64]$buses[$i].Length
} else {
$busId = [int64]::Parse($buses[$i])
if ((($expectedTime - $startTime) + ($startTime % $busId)) % $busId -eq 0) {
if ($currentIx -lt $i) {
if ($startTime - $lastStartTime -eq $diff) {
$currentIx = $i
$interval = $diff
}
$diff = $startTime - $lastStartTime
$lastStartTime = $startTime
}
$expectedTime += [Int64]1
} else {
$valid = $false
$startTime += $interval
$round += 1
break
}
}
}
}
Write-Host $startTime