mirror of
https://github.com/Xevion/advent-of-code.git
synced 2025-12-05 23:14:16 -06:00
cleanup to merge part 1 & 2 day 6 2015
This commit is contained in:
@@ -9,40 +9,59 @@ PATTERN = r'(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)'
|
||||
class Grid(object):
|
||||
def __init__(self, x, y):
|
||||
self.x, self.y = 1000, 1000
|
||||
self.boolgrid = [[False for y in range(self.y)] for x in range(self.x)]
|
||||
self.grid = [[0 for y in range(self.y)] for x in range(self.x)]
|
||||
|
||||
def read(self, string):
|
||||
def read(self, string, type):
|
||||
match = re.match(PATTERN, string)
|
||||
command = match.group(1)
|
||||
x1, y1 = int(match.group(2)), int(match.group(3))
|
||||
x2, y2 = int(match.group(4)), int(match.group(5))
|
||||
self.command(command, x1, y1, x2, y2)
|
||||
|
||||
def command(self, command, x1, y1, x2, y2):
|
||||
if type == 1:
|
||||
self.command1(command, x1, y1, x2, y2)
|
||||
elif type == 2:
|
||||
self.command2(command, x1, y1, x2, y2)
|
||||
|
||||
def command1(self, command, x1, y1, x2, y2):
|
||||
for x in range(x1, x2 + 1):
|
||||
for y in range(y1, y2 + 1):
|
||||
if command == 'turn on':
|
||||
self.boolgrid[x][y] = True
|
||||
self.grid[x][y] = 1
|
||||
elif command == 'turn off':
|
||||
self.boolgrid[x][y] = False
|
||||
self.grid[x][y] = 0
|
||||
elif command == 'toggle':
|
||||
self.boolgrid[x][y] = not self.boolgrid[x][y]
|
||||
self.grid[x][y] = 0 if self.grid[x][y] else 1
|
||||
|
||||
|
||||
def command2(self, command, x1, y1, x2, y2):
|
||||
for x in range(x1, x2 + 1):
|
||||
for y in range(y1, y2 + 1):
|
||||
if command == 'turn on':
|
||||
self.grid[x][y] += 1
|
||||
elif command == 'turn off':
|
||||
self.grid[x][y] = max(0, self.grid[x][y] - 1)
|
||||
elif command == 'toggle':
|
||||
self.grid[x][y] += 2
|
||||
|
||||
def count(self):
|
||||
count = 0
|
||||
for x in range(len(self.boolgrid)):
|
||||
for y in range(len(self.boolgrid[x])):
|
||||
count += 1 if self.boolgrid[x][y] else 0
|
||||
for x in range(len(self.grid)):
|
||||
for y in range(len(self.grid[x])):
|
||||
count += self.grid[x][y]
|
||||
return count
|
||||
|
||||
def main():
|
||||
grid = Grid(1000, 1000)
|
||||
# Part 1 solution
|
||||
p1 = Grid(1000, 1000)
|
||||
for line in DATA:
|
||||
grid.read(line)
|
||||
print(grid.count())
|
||||
p1.read(line, 1)
|
||||
print(p1.count())
|
||||
|
||||
# Part 2 solution
|
||||
p2 = Grid(1000, 1000)
|
||||
for line in DATA:
|
||||
p2.read(line, 2)
|
||||
print(p2.count())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
# grid = Grid(1000, 1000)
|
||||
# grid.command('turn on', 0, 0, 2, 2)
|
||||
# print(grid.count())
|
||||
main()
|
||||
@@ -1,48 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import re
|
||||
|
||||
PATH = os.path.join(sys.path[0], '..', 'input')
|
||||
DATA = open(PATH, 'r').read().split('\n')
|
||||
PATTERN = r'(turn on|turn off|toggle) (\d+),(\d+) through (\d+),(\d+)'
|
||||
|
||||
class Grid(object):
|
||||
def __init__(self, x, y):
|
||||
self.x, self.y = 1000, 1000
|
||||
self.boolgrid = [[0 for y in range(self.y)] for x in range(self.x)]
|
||||
|
||||
def read(self, string):
|
||||
match = re.match(PATTERN, string)
|
||||
command = match.group(1)
|
||||
x1, y1 = int(match.group(2)), int(match.group(3))
|
||||
x2, y2 = int(match.group(4)), int(match.group(5))
|
||||
self.command(command, x1, y1, x2, y2)
|
||||
|
||||
def command(self, command, x1, y1, x2, y2):
|
||||
for x in range(x1, x2 + 1):
|
||||
for y in range(y1, y2 + 1):
|
||||
if command == 'turn on':
|
||||
self.boolgrid[x][y] += 1
|
||||
elif command == 'turn off':
|
||||
self.boolgrid[x][y] = max(0, self.boolgrid[x][y] - 1)
|
||||
elif command == 'toggle':
|
||||
self.boolgrid[x][y] += 2
|
||||
|
||||
def count(self):
|
||||
count = 0
|
||||
for x in range(len(self.boolgrid)):
|
||||
for y in range(len(self.boolgrid[x])):
|
||||
count += self.boolgrid[x][y]
|
||||
return count
|
||||
|
||||
def main():
|
||||
grid = Grid(1000, 1000)
|
||||
for line in DATA:
|
||||
grid.read(line)
|
||||
print(grid.count())
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
# grid = Grid(1000, 1000)
|
||||
# grid.command('turn on', 0, 0, 2, 2)
|
||||
# print(grid.count())
|
||||
Reference in New Issue
Block a user