Advertisement
Foxscotch

day 6 part 1 revised

Dec 20th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. import re
  2. from numpy import matrix
  3.  
  4.  
  5. instructions = []
  6.  
  7. with open('input.txt', 'r') as f:
  8.     regex = re.compile(r'([\w ]+) (\d+),(\d+) .+ (\d+),(\d+)')
  9.     for line in f:
  10.         action, x1, y1, x2, y2 = regex.match(line).groups()
  11.         instructions.append((
  12.             action,
  13.             (int(x1), int(y1)),
  14.             (int(x2), int(y2))
  15.         ))
  16.  
  17. lights = matrix([[0 for i in range(1000)] for j in range(1000)])
  18.  
  19. def act(instruction):
  20.     action = instruction[0]
  21.     x1, y1 = instruction[1]
  22.     x2, y2 = instruction[2]
  23.     x2 += 1
  24.     y2 += 1
  25.  
  26.     if action == 'turn on':
  27.         lights[x1:x2, y1:y2] = 1
  28.     elif action == 'turn off':
  29.         lights[x1:x2, y1:y2] = 0
  30.     else:
  31.         lights[x1:x2, y1:y2] ^= 1
  32.  
  33. # The e stands for "easy"
  34. def selection_size_e(instruction):
  35.     x1, y1 = instruction[1]
  36.     x2, y2 = instruction[2]
  37.     x2 += 1
  38.     y2 += 1
  39.  
  40.     return lights[x1:x2, y1:y2].flatten().tolist()[0].count(0)
  41.  
  42. # The m stands for "manual"
  43. def selection_size_m(instruction):
  44.     x1, y1 = instruction[1]
  45.     x2, y2 = instruction[2]
  46.     x2 += 1
  47.     y2 += 1
  48.  
  49.     width = x2 - x1
  50.     height = y2 - y1
  51.     return width * height
  52.  
  53. for step in instructions:
  54.     act(step)
  55.  
  56. print(lights.flatten().tolist()[0].count(1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement