- 145.5k
- 22
- 190
- 479
Optimize: Advent of Code Day 6 w/: toggling lights in a grid with Powershell
I am using the "Advent of Code" series to help with my PowershellPowerShell education. The http://adventofcode.com/day/6Day 6 puzzle has a 1000 ×ばつ 1000 grid of lights. After processing instructions to turn on, turn off, or toggle rectangles of lights in the grid, how many lights are on at the end?
I am confident that this script will produce the right answer. However, it is going way totoo slow. I am interested in optimizing this script for improved performance. I am not against coming up with an alternative way to solve this problem with powershellPowerShell.
There are a million different lights that can be on or off. This massive number is obviously causing the long process time. At the moment, I can not think of any alternatives.
Thank you in advance
Optimize: Advent of Code Day 6 w/ Powershell
I am using the "Advent of Code" series to help with my Powershell education. http://adventofcode.com/day/6
I am confident that this script will produce the right answer. However, it is going way to slow. I am interested in optimizing this script for improved performance. I am not against coming up with an alternative way to solve this problem with powershell.
There are a million different lights that can be on or off. This massive number is obviously causing the long process time. At the moment, I can not think of any alternatives.
Thank you in advance
Advent of Code Day 6: toggling lights in a grid with Powershell
I am using the "Advent of Code" series to help with my PowerShell education. The Day 6 puzzle has a 1000 ×ばつ 1000 grid of lights. After processing instructions to turn on, turn off, or toggle rectangles of lights in the grid, how many lights are on at the end?
I am confident that this script will produce the right answer. However, it is going way too slow. I am interested in optimizing this script for performance. I am not against coming up with an alternative way to solve this problem with PowerShell.
There are a million different lights that can be on or off. This massive number is obviously causing the long process time. At the moment, I can not think of any alternatives.
Optimize: Advent of Code Day 6 w/ Powershell
I am using the "Advent of Code" series to help with my Powershell education. http://adventofcode.com/day/6
I am confident that this script will produce the right answer. However, it is going way to slow. I am interested in optimizing this script for improved performance. I am not against coming up with an alternative way to solve this problem with powershell.
$lightson = New-Object Collections.Generic.HashSet[string]
$strings = Get-Content C:\test\lights.txt
foreach ($string in $strings){
$numbers = [regex]::matches($string,"\d+")
$minX = [convert]::ToInt32($numbers[0],10)
$minY = [convert]::ToInt32($numbers[1],10)
$maxX = [convert]::ToInt32($numbers[2],10)
$maxY = [convert]::ToInt32($numbers[3],10)
$x = $minX
$y = $miny
write-host $string
write-host $x $y
if ($string -like "turn on*"){
write-host "turn on"
while ($x -le $maxX){
while ($y -le $maxY){
$light = "$x,$y"
if ($light -notin $lightson){
[void]$lightson.Add("$x,$y")
#write-host $lightson.Count
}
$y +=1
}
$x += 1
$y = $miny
}
write-host $lightson.Count "`n"
}
if ($string -like "turn off*"){
write-host "turn off"
while ($x -le $maxX){
while ($y -le $maxY){
$light = "$x,$y"
if ($light -in $lightson){
[void]$lightson.Remove("$x,$y")
#write-host $lightson.Count
}
$y +=1
}
$x += 1
$y = $miny
}
write-host $lightson.Count "`n"
}
if ($string -like "toggle*"){
write-host "toggle`n"
while ($x -le $maxX){
while ($y -le $maxY){
$light = "$x,$y"
if ($light -in $lightson){
$lightson.Remove("$x,$y")
#write-host $lightson.Count
}
else{[void]($lightson.Add("$x,$y"))}
$y +=1
}
$x += 1
$y = $miny
}
write-host $lightson.Count "`n"
}
}
There are a million different lights that can be on or off. This massive number is obviously causing the long process time. At the moment, I can not think of any alternatives.
Thank you in advance