mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-27 05:40:32 +01:00
Solutions for day 6.
This commit is contained in:
41
day6-1.py
Normal file
41
day6-1.py
Normal file
@@ -0,0 +1,41 @@
|
||||
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(False)
|
||||
|
||||
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] = not lights[x][y]
|
||||
elif "on" in command:
|
||||
lights[x][y] = True
|
||||
else:
|
||||
lights[x][y] = False
|
||||
|
||||
total = 0
|
||||
for row in lights:
|
||||
total += sum([int(i) for i in row])
|
||||
|
||||
print total
|
||||
Reference in New Issue
Block a user