mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-26 21:30:31 +01:00
Do part 2 using the slice algorithm.
This commit is contained in:
@@ -1,42 +0,0 @@
|
|||||||
from __future__ import print_function
|
|
||||||
import fileinput
|
|
||||||
import re
|
|
||||||
import sys
|
|
||||||
|
|
||||||
commandExpr = re.compile(r"^(toggle|turn (on|off)) (\d+),(\d+) through (\d+),(\d+)$")
|
|
||||||
|
|
||||||
lights = []
|
|
||||||
for x in range(1000):
|
|
||||||
lights.append([])
|
|
||||||
for y in range(1000):
|
|
||||||
lights[x].append(0)
|
|
||||||
|
|
||||||
for line in fileinput.input():
|
|
||||||
match = commandExpr.search(line)
|
|
||||||
|
|
||||||
if not match:
|
|
||||||
print("Invalid string")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
xStart = int(match.group(3))
|
|
||||||
yStart = int(match.group(4))
|
|
||||||
|
|
||||||
xEnd = int(match.group(5))
|
|
||||||
yEnd = int(match.group(6))
|
|
||||||
|
|
||||||
command = match.group(1)
|
|
||||||
|
|
||||||
for x in range(xStart, xEnd + 1):
|
|
||||||
for y in range(yStart, yEnd + 1):
|
|
||||||
if command == "toggle":
|
|
||||||
lights[x][y] += 2
|
|
||||||
elif "on" in command:
|
|
||||||
lights[x][y] += 1
|
|
||||||
else:
|
|
||||||
lights[x][y] = max(0, lights[x][y] - 1)
|
|
||||||
|
|
||||||
total = 0
|
|
||||||
for row in lights:
|
|
||||||
total += sum(row)
|
|
||||||
|
|
||||||
print(total)
|
|
||||||
@@ -23,7 +23,7 @@ def compact(lightList):
|
|||||||
|
|
||||||
def updateState(sliceList, yStart, yEnd, updateFnc):
|
def updateState(sliceList, yStart, yEnd, updateFnc):
|
||||||
newList = []
|
newList = []
|
||||||
for start, end, state in lights[x]:
|
for start, end, state in sliceList:
|
||||||
if not start <= yStart <= end and not yStart <= start <= yEnd:
|
if not start <= yStart <= end and not yStart <= start <= yEnd:
|
||||||
# Block not in range, skip
|
# Block not in range, skip
|
||||||
newList.append((start, end, state))
|
newList.append((start, end, state))
|
||||||
@@ -54,9 +54,19 @@ def getPart1Update(command):
|
|||||||
else:
|
else:
|
||||||
return lambda _: False
|
return lambda _: False
|
||||||
|
|
||||||
lights = []
|
def getPart2Update(command):
|
||||||
|
if "toggle" in command:
|
||||||
|
return lambda x: x + 2
|
||||||
|
elif "on" in command:
|
||||||
|
return lambda x: x + 1
|
||||||
|
else:
|
||||||
|
return lambda x: max(0, x - 1)
|
||||||
|
|
||||||
|
lights1 = []
|
||||||
|
lights2 = []
|
||||||
for x in range(1000):
|
for x in range(1000):
|
||||||
lights.append([(0, 999, False)])
|
lights1.append([(0, 999, False)])
|
||||||
|
lights2.append([(0, 999, 0)])
|
||||||
|
|
||||||
for line in fileinput.input():
|
for line in fileinput.input():
|
||||||
match = re.search(r"^(toggle|turn (on|off)) (\d+),(\d+) through (\d+),(\d+)$", line)
|
match = re.search(r"^(toggle|turn (on|off)) (\d+),(\d+) through (\d+),(\d+)$", line)
|
||||||
@@ -70,8 +80,8 @@ for line in fileinput.input():
|
|||||||
command = match.group(1)
|
command = match.group(1)
|
||||||
|
|
||||||
for x in range(xStart, xEnd + 1):
|
for x in range(xStart, xEnd + 1):
|
||||||
lights[x] = updateState(lights[x], yStart, yEnd, getPart1Update(command))
|
lights1[x] = updateState(lights1[x], yStart, yEnd, getPart1Update(command))
|
||||||
|
lights2[x] = updateState(lights2[x], yStart, yEnd, getPart2Update(command))
|
||||||
|
|
||||||
total = sum(colsum(x) for x in lights)
|
print("Lights on:", sum(colsum(x) for x in lights1))
|
||||||
|
print("Lumen:", sum(colsum(x) for x in lights2))
|
||||||
print(total)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user