mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Update for a more general solution.
This commit is contained in:
@@ -21,25 +21,7 @@ def compact(lightList):
|
|||||||
|
|
||||||
return newList
|
return newList
|
||||||
|
|
||||||
def colsum(lightList):
|
def updateState(sliceList, yStart, yEnd, updateFnc):
|
||||||
return sum(int(state) * (stop - start + 1) for start, stop, state in lightList)
|
|
||||||
|
|
||||||
lights = []
|
|
||||||
for x in range(1000):
|
|
||||||
lights.append([(0, 999, False)])
|
|
||||||
|
|
||||||
for line in fileinput.input():
|
|
||||||
match = re.search(r"^(toggle|turn (on|off)) (\d+),(\d+) through (\d+),(\d+)$", line)
|
|
||||||
|
|
||||||
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):
|
|
||||||
newList = []
|
newList = []
|
||||||
for start, end, state in lights[x]:
|
for start, end, state in lights[x]:
|
||||||
if not start <= yStart <= end and not yStart <= start <= yEnd:
|
if not start <= yStart <= end and not yStart <= start <= yEnd:
|
||||||
@@ -57,14 +39,38 @@ for line in fileinput.input():
|
|||||||
newList.append((yEnd + 1, end, state))
|
newList.append((yEnd + 1, end, state))
|
||||||
end = yEnd
|
end = yEnd
|
||||||
|
|
||||||
if "toggle" in command:
|
newList.append((start, end, updateFnc(state)))
|
||||||
newList.append((start, end, not state))
|
|
||||||
elif "on" in command:
|
|
||||||
newList.append((start, end, True))
|
|
||||||
else:
|
|
||||||
newList.append((start, end, False))
|
|
||||||
|
|
||||||
lights[x] = compact(newList)
|
return compact(newList)
|
||||||
|
|
||||||
|
def colsum(lightList):
|
||||||
|
return sum(int(state) * (stop - start + 1) for start, stop, state in lightList)
|
||||||
|
|
||||||
|
def getPart1Update(command):
|
||||||
|
if "toggle" in command:
|
||||||
|
return lambda x: not x
|
||||||
|
elif "on" in command:
|
||||||
|
return lambda _: True
|
||||||
|
else:
|
||||||
|
return lambda _: False
|
||||||
|
|
||||||
|
lights = []
|
||||||
|
for x in range(1000):
|
||||||
|
lights.append([(0, 999, False)])
|
||||||
|
|
||||||
|
for line in fileinput.input():
|
||||||
|
match = re.search(r"^(toggle|turn (on|off)) (\d+),(\d+) through (\d+),(\d+)$", line)
|
||||||
|
|
||||||
|
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):
|
||||||
|
lights[x] = updateState(lights[x], yStart, yEnd, getPart1Update(command))
|
||||||
|
|
||||||
total = sum(colsum(x) for x in lights)
|
total = sum(colsum(x) for x in lights)
|
||||||
|
|
||||||
Reference in New Issue
Block a user