Snippets
Created by
Steve Adams
last modified
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | '''
http://adventofcode.com/day/6
--- Day 6: Probably a Fire Hazard ---
Because your neighbors keep defeating you in the holiday house decorating contest
year after year, you've decided to deploy one million lights in a 1000x1000 grid.
Furthermore, because you've been especially nice this year, Santa has mailed you
instructions on how to display the ideal lighting configuration.
Lights in your grid are numbered from 0 to 999 in each direction; the lights at
each corner are at 0,0, 0,999, 999,999, and 999,0. The instructions include whether
to turn on, turn off, or toggle various inclusive ranges given as coordinate pairs.
Each coordinate pair represents opposite corners of a rectangle, inclusive; a
coordinate pair like 0,0 through 2,2 therefore refers to 9 lights in a 3x3 square.
The lights all start turned off.
To defeat your neighbors this year, all you have to do is set up your lights by
doing the instructions Santa sent you in order.
For example:
- turn on 0,0 through 999,999 would turn on (or leave on) every light.
- toggle 0,0 through 999,0 would toggle the first line of 1000 lights, turning off
the ones that were on, and turning on the ones that were off.
- turn off 499,499 through 500,500 would turn off (or leave off) the middle four lights.
- After following the instructions, how many lights are lit?
'''
class LightGrid:
def __init__(self, xSize = 999, ySize = 999, init = True):
self.xSize = xSize
self.ySize = ySize
self.grid = {}
if init:
self.build()
def build(self):
for x in xrange(0, (self.xSize) + 1):
self.grid[x] = {}
for y in xrange(0, (self.ySize) + 1):
self.grid[x][y] = False
def checkCoordinates(self, x, y):
return self.grid[x][y]
def configureCoordinates(self, x, y, setting):
if setting == "on":
self.grid[x][y] = True
elif setting == "off":
self.grid[x][y] = False
elif setting == "toggle":
self.grid[x][y] = not self.grid[x][y]
else:
print("This setting is shit: " + setting)
return
class LightManager:
def __init__(self, grid, instructions):
self.grid = grid
self.instructions = instructions
self.currentStep = None
self.currentSetting = None
self.possibleSettings = ["turn on", "turn off", "toggle"]
self.numberOfLightsOn = None
def extractSetting(self):
for setting in self.possibleSettings:
if setting in self.currentStep:
self.currentStep = self.currentStep.replace(setting + " ", "")
return setting.replace("turn ", "")
return False
def extractCoordinates(self):
coords = {}
rawCoords = self.currentStep.replace(" through ", ",")
cleanCoords = []
for coord in rawCoords.split(','):
cleanCoords.append(int(coord))
[coords['xStart'], coords['yStart'], coords['xEnd'], coords['yEnd']] = cleanCoords
return coords
def stepThroughInstruction(self, step):
self.currentStep = step
self.currentSetting = self.extractSetting()
coords = self.extractCoordinates()
for xCoord in xrange(coords["xStart"], coords["xEnd"] + 1):
for yCoord in xrange(coords["yStart"], coords["yEnd"] + 1):
self.grid.configureCoordinates(xCoord, yCoord, self.currentSetting)
def followInstructions(self):
for step in self.instructions:
self.stepThroughInstruction(step)
def getNumberOfLightsOn(self):
self.numberOfLightsOn = 0
for x in xrange(0, (self.grid.xSize + 1)):
for y in xrange(0, (self.grid.ySize + 1)):
if self.grid.checkCoordinates(x, y):
self.numberOfLightsOn += 1
return self.numberOfLightsOn
grid = LightGrid()
with open("advent_day-6-in.txt", "r") as instructions:
instructions = instructions.readlines()
manager = LightManager(grid, instructions)
manager.followInstructions()
print(manager.getNumberOfLightsOn())
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.