Advertisement
Foxscotch

day 6 part 1

Dec 20th, 2015
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 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.         match = regex.match(line)
  11.         instructions.append((
  12.             match.group(1),
  13.             (int(match.group(2)), int(match.group(3))),
  14.             (int(match.group(3)), int(match.group(5)))
  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.  
  24.     if action == 'turn on':
  25.         lights[x1:x2, y1:y2] = 1
  26.     elif action == 'turn off':
  27.         lights[x1:x2, y1:y2] = 0
  28.     else:
  29.         lights[x1:x2, y1:y2] ^= 1
  30.  
  31. for step in instructions:
  32.     act(step)
  33.  
  34. print(lights.flatten().tolist()[0].count(1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement