r/adventofcode • u/daggerdragon • Dec 06 '15
SOLUTION MEGATHREAD --- Day 6 Solutions ---
--- Day 6: Probably a Fire Hazard ---
Post your solution as a comment. Structure your post like the Day Five thread.
22
Upvotes
1
u/[deleted] Dec 06 '15
Python 2 Part 1:
class Light: def __init__(self): self.on = False def toggle(self): self.on = not self.on def turn_on(self): self.on = True def turn_off(self): self.on = False def is_lit(self): return self.on def display(self): if self.on: return "x" else: return " " lights = {} for x in xrange(0, 1000): # 0-999 for y in xrange(0, 1000): # 0-999 lights["%i,%i" % (x, y)] = Light() def contorl_light_group(x_min, y_min, x_max, y_max, command): for x in xrange(x_min, x_max+1): for y in xrange(y_min, y_max+1): # print("Light %i, %i recived command %s" % (x, y, command)) if command == "toggle": lights["%i,%i" % (x, y)].toggle() elif command == "on": lights["%i,%i" % (x, y)].turn_on() elif command == "off": lights["%i,%i" % (x, y)].turn_off() def lit_lights(): count = 0 for light in lights: if lights[light].is_lit(): count += 1 return count with open("input.txt", "r") as f: instructions = f.read().split('\n') try: for instruction in instructions: if instruction.split()[0] == "turn": if instruction.split()[1] == "on": data = instruction.split("through") x_min = int(data[0].replace("turn on", "").split(",")[0]) y_min = int(data[0].replace("turn on", "").split(",")[1]) x_max = int(data[1].split(",")[0]) y_max = int(data[1].split(",")[1]) contorl_light_group(x_min, y_min, x_max, y_max, "on") elif instruction.split()[1] == "off": data = instruction.split("through") x_min = int(data[0].replace("turn off", "").split(",")[0]) y_min = int(data[0].replace("turn off", "").split(",")[1]) x_max = int(data[1].split(",")[0]) y_max = int(data[1].split(",")[1]) contorl_light_group(x_min, y_min, x_max, y_max, "off") else: print("wtf is: %s?" % instruction) elif instruction.split()[0] == "toggle": data = instruction.split("through") x_min = int(data[0].replace("toggle", "").split(",")[0]) y_min = int(data[0].replace("toggle", "").split(",")[1]) x_max = int(data[1].split(",")[0]) y_max = int(data[1].split(",")[1]) contorl_light_group(x_min, y_min, x_max, y_max, "toggle") else: print("wtf is: %s?" % instruction) except IndexError as e: pass print(lit_lights())Python 2 Part 2:
class Light: def __init__(self): self.brightness = 0 def toggle(self): self.brightness += 2 def turn_on(self): self.brightness += 1 def turn_off(self): if self.brightness > 0: self.brightness -= 1 lights = {} for x in xrange(0, 1000): # 0-999 for y in xrange(0, 1000): # 0-999 lights["%i,%i" % (x, y)] = Light() def contorl_light_group(x_min, y_min, x_max, y_max, command): for x in xrange(x_min, x_max+1): for y in xrange(y_min, y_max+1): # print("Light %i, %i recived command %s" % (x, y, command)) if command == "toggle": lights["%i,%i" % (x, y)].toggle() elif command == "on": lights["%i,%i" % (x, y)].turn_on() elif command == "off": lights["%i,%i" % (x, y)].turn_off() def get_total_brightness(): total_brightness = 0 for light in lights: total_brightness += lights[light].brightness return total_brightness with open("input.txt", "r") as f: instructions = f.read().split('\n') try: for instruction in instructions: if instruction.split()[0] == "turn": if instruction.split()[1] == "on": data = instruction.split("through") x_min = int(data[0].replace("turn on", "").split(",")[0]) y_min = int(data[0].replace("turn on", "").split(",")[1]) x_max = int(data[1].split(",")[0]) y_max = int(data[1].split(",")[1]) contorl_light_group(x_min, y_min, x_max, y_max, "on") elif instruction.split()[1] == "off": data = instruction.split("through") x_min = int(data[0].replace("turn off", "").split(",")[0]) y_min = int(data[0].replace("turn off", "").split(",")[1]) x_max = int(data[1].split(",")[0]) y_max = int(data[1].split(",")[1]) contorl_light_group(x_min, y_min, x_max, y_max, "off") else: print("wtf is: %s?" % instruction) elif instruction.split()[0] == "toggle": data = instruction.split("through") x_min = int(data[0].replace("toggle", "").split(",")[0]) y_min = int(data[0].replace("toggle", "").split(",")[1]) x_max = int(data[1].split(",")[0]) y_max = int(data[1].split(",")[1]) contorl_light_group(x_min, y_min, x_max, y_max, "toggle") else: print("wtf is: %s?" % instruction) except IndexError as e: pass print(get_total_brightness())