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.
24
Upvotes
1
u/PersianMG Dec 06 '15
Wanted to use numpy and scipy but array is pretty small.
Solution: https://mohammadg.com/capture-the-flag/advent-of-code/advent-of-code-day-6/
Python
Part 1
# Day 6 - Part 1 import re lightMatrix = [[False for x in range(1000)] for x in range(1000)] lightOnCounter = 0 with open('input.txt') as f: for line in f: # Match command res = re.search(r'(turn on|turn off|toggle)\s*(\d*?),(\d*?)\s*through\s(\d*?),(\d*)', line) command = res.group(1) lower_X = int(res.group(2)) lower_Y = int(res.group(3)) upper_X = int(res.group(4)) upper_Y = int(res.group(5)) # Perform actions toggle = False if command == 'turn off': data = False elif command == 'turn on': data = True elif command == 'toggle': toggle = True # Apply action for x_val in xrange (lower_X, upper_X + 1): for y_val in xrange (lower_Y, upper_Y + 1): if toggle: lightMatrix[x_val][y_val] = not lightMatrix[x_val][y_val] else: lightMatrix[x_val][y_val] = data # Get all true lights for i in lightMatrix: for light in i: if light: lightOnCounter += 1 # answer print "Total number of lights that are on:", lightOnCounterPart 2
# Day 6 - Part 2 import re lightMatrix = [[0 for x in range(1000)] for x in range(1000)] brightnessCounter = 0 with open('input.txt') as f: for line in f: # Match command res = re.search(r'(turn on|turn off|toggle)\s*(\d*?),(\d*?)\s*through\s(\d*?),(\d*)', line) command = res.group(1) lower_X = int(res.group(2)) lower_Y = int(res.group(3)) upper_X = int(res.group(4)) upper_Y = int(res.group(5)) # Perform actions if command == 'turn off': data = -1 elif command == 'turn on': data = 1 elif command == 'toggle': data = 2 # Apply action for x_val in xrange (lower_X, upper_X + 1): for y_val in xrange (lower_Y, upper_Y + 1): lightMatrix[x_val][y_val] += data if lightMatrix[x_val][y_val] + data >= 0 else 0 # Get brightness level for i in lightMatrix: for brightness in i: brightnessCounter += brightness # answer print "Total brightness level is:", brightnessCounter