90 lines
2.4 KiB
Python
90 lines
2.4 KiB
Python
#!/bin/python3
|
|
import sys,time,re
|
|
from pprint import pprint
|
|
from collections import defaultdict
|
|
from itertools import product
|
|
|
|
sys.path.insert(0, '../../')
|
|
from fred import list2int,get_re,nprint,lprint,loadFile
|
|
start_time = time.time()
|
|
|
|
input_f = 'input'
|
|
|
|
#########################################
|
|
# #
|
|
# Part 1 #
|
|
# #
|
|
#########################################
|
|
def part1():
|
|
instructions = []
|
|
|
|
with open(input_f) as file:
|
|
tmp = []
|
|
for line in file:
|
|
tmp = [re.match(r'.+?(?=\d)',line).group().strip()]
|
|
tmp += [int(i) for i in re.findall(r'\d+',line)]
|
|
instructions.append(tmp)
|
|
|
|
size = 1000
|
|
maps = defaultdict(bool)
|
|
|
|
for i in instructions:
|
|
for x in product(range(i[1],i[3]+1),range(i[2],i[4]+1)):
|
|
if i[0] == 'turn off':
|
|
maps[x] = False
|
|
elif i[0] == 'turn on':
|
|
maps[x] = True
|
|
else:
|
|
if maps[x] == True:
|
|
maps[x] = False
|
|
else:
|
|
maps[x] = True
|
|
count = 0
|
|
|
|
for i in product(range(0,size),range(0,size)):
|
|
if maps[i] == True:
|
|
count += 1
|
|
return count
|
|
|
|
start_time = time.time()
|
|
print('Part 1:',part1(), '\t\t', round((time.time() - start_time)*1000), 'ms')
|
|
|
|
|
|
#########################################
|
|
# #
|
|
# Part 2 #
|
|
# #
|
|
#########################################
|
|
def part2():
|
|
instructions = []
|
|
|
|
with open(input_f) as file:
|
|
tmp = []
|
|
for line in file:
|
|
tmp = [re.match(r'.+?(?=\d)',line).group().strip()]
|
|
tmp += [int(i) for i in re.findall(r'\d+',line)]
|
|
instructions.append(tmp)
|
|
|
|
size = 1000
|
|
|
|
maps = defaultdict(int)
|
|
|
|
for i in instructions:
|
|
for x in product(range(i[1],i[3]+1),range(i[2],i[4]+1)):
|
|
if i[0] == 'turn off':
|
|
if maps[x] != 0:
|
|
maps[x] -= 1
|
|
elif i[0] == 'turn on':
|
|
maps[x] += 1
|
|
else:
|
|
maps[x] += 2
|
|
|
|
count = 0
|
|
|
|
for i in product(range(0,size),range(0,size)):
|
|
count += maps[i]
|
|
|
|
return count
|
|
|
|
start_time = time.time()
|
|
print('Part 2:',part2(), '\t', round((time.time() - start_time)*1000), 'ms') |