From 48cf7ade00fb60990b901dd4c56189b7901a75d1 Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Sat, 9 Dec 2023 21:37:55 +0100 Subject: [PATCH] Finished on 2015-12-07 p2 --- 2015/day6/part1.py | 49 ++++++++++++++++++++++++++++++++++++++++++++++ 2015/day6/part2.py | 35 +++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 2015/day6/part1.py create mode 100644 2015/day6/part2.py diff --git a/2015/day6/part1.py b/2015/day6/part1.py new file mode 100644 index 0000000..6426a4b --- /dev/null +++ b/2015/day6/part1.py @@ -0,0 +1,49 @@ +import sys +from pprint import pprint +import re +from itertools import product +from collections import defaultdict + +instructions = [] + +with open(sys.argv[1]) 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) +#maps = [ [False]*size for i in range(size)] + + +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 + + #for y in range(i[1],i[3]+1): + # for x in range(i[2],i[4]+1): + # if i[0] == 'turn off': + # maps[x][y] = False + # elif i[0] == 'turn on': + # maps[x][y] = False + # else: + # maps[x][y] = True if maps[x][y] == False else maps[x][y] == True + +count = 0 + +for i in product(range(0,size),range(0,size)): + if maps[i] == True: + count += 1 + +print(count) diff --git a/2015/day6/part2.py b/2015/day6/part2.py new file mode 100644 index 0000000..0a46a9b --- /dev/null +++ b/2015/day6/part2.py @@ -0,0 +1,35 @@ +import sys +from pprint import pprint +import re +from itertools import product +from collections import defaultdict + +instructions = [] + +with open(sys.argv[1]) 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] + +print(count)