-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdventOfCode - Day11.2.ps1
139 lines (133 loc) · 3.55 KB
/
AdventOfCode - Day11.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
$inputs = new-object System.Collections.ArrayList
Get-Content -Path '.\ferry seats day 11.txt' | ForEach-OBject {
$inputs.Add($_)
}
function countOccupied($seats, $i, $j) {
$occupiedSeats = 0
$directions = "NW", "N", "NE", "E", "SE", "S", "SW", "W"
foreach ($direction in $directions) {
switch ($direction) {
"NW" {
$x = $i - 1
$y = $j - 1
while ($x -ge 0 -and $y -ge 0 -and '.' -eq $seats[$x][$y]) {
$x -= 1
$y -= 1
}
if ($x -ge 0 -and $y -ge 0 -and '#' -eq $seats[$x][$y]) {
$occupiedSeats++
}
}
"N" {
$x = $i - 1
$y = $j
while ($x -ge 0 -and '.' -eq $seats[$x][$y]) {
$x -= 1
}
if ($x -ge 0 -and '#' -eq $seats[$x][$y]) {
$occupiedSeats++
}
}
"NE" {
$x = $i - 1
$y = $j + 1
while ($x -ge 0 -and $y -lt $seats[$x].Length -and '.' -eq $seats[$x][$y]) {
$x -= 1
$y += 1
}
if ($x -ge 0 -and $y -lt $seats[$x].Length -and '#' -eq $seats[$x][$y]) {
$occupiedSeats++
}
}
"E" {
$x = $i
$y = $j + 1
while ($y -lt $seats[$x].Length -and '.' -eq $seats[$x][$y]) {
$y += 1
}
if ($y -lt $seats[$x].Length -and '#' -eq $seats[$x][$y]) {
$occupiedSeats++
}
}
"SE" {
$x = $i + 1
$y = $j + 1
while ($x -lt $seats.Count -and $y -lt $seats[$x].Length -and '.' -eq $seats[$x][$y]) {
$x += 1
$y += 1
}
if ($x -lt $seats.Count -and $y -lt $seats[$x].Length -and '#' -eq $seats[$x][$y]) {
$occupiedSeats++
}
}
"S" {
$x = $i + 1
$y = $j
while ($x -lt $seats.Count -and '.' -eq $seats[$x][$y]) {
$x += 1
}
if ($x -lt $seats.Count -and '#' -eq $seats[$x][$y]) {
$occupiedSeats++
}
}
"SW" {
$x = $i + 1
$y = $j - 1
while ($x -lt $seats.Count -and $y -ge 0 -and '.' -eq $seats[$x][$y]) {
$x += 1
$y -= 1
}
if ($x -lt $seats.Count -and $y -ge 0 -and '#' -eq $seats[$x][$y]) {
$occupiedSeats++
}
}
"W" {
$x = $i
$y = $j - 1
while ($y -ge 0 -and '.' -eq $seats[$x][$y]) {
$y -= 1
}
if ($y -ge 0 -and '#' -eq $seats[$x][$y]) {
$occupiedSeats++
}
}
}
}
return $occupiedSeats
}
function checkSeat($inputs, $clone, $i, $j) {
$occupiedSeats = countOccupied $clone $i $j
if ('L' -eq $inputs[$i][$j] -and 0 -eq $occupiedSeats) {
$arr = $inputs[$i].ToCharArray()
$arr[$j] = '#'
$inputs[$i] = [System.String]::Join("", $arr)
return $true
} elseif ('#' -eq $inputs[$i][$j] -and 5 -le $occupiedSeats) {
$arr = $inputs[$i].ToCharArray()
$arr[$j] = 'L'
$inputs[$i] = [System.String]::Join("", $arr)
return $true
}
return $false
}
$rounds = 0
$changed = $true
while ($changed) {
$changed = $false
$clone = $inputs.Clone()
for ($i = 0; $i -lt $inputs.Count; $i++) {
for ($j = 0; $j -lt $inputs[$i].Length; $j++) {
if ('.' -ne $inputs[$i][$j]) {
$changed = (checkSeat $inputs $clone $i $j) -or $changed
}
}
}
$rounds++
}
$occupiedSeats = 0
$inputs | % {
if ($_ -match '#') {
$occupiedSeats += [regex]::matches($_,'#').Count
}
}
$occupiedSeats