add remaining code and input files from 2020

This commit is contained in:
Cookie committed 2021-11-30 00:36:00 +01:00
1 parent c10382b0c0
commit 23b2e01280
41 files changed
+11320 -37

No files matched your search

+20
View File
@@ -0,0 +1,20 @@
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input01") as f:
lines = f.readlines()
# make ints out of strings
numbers = ([0] * (len(lines) + 1))
for n in range(0, len(lines)):
numbers[n] = int(lines[n])
# challenge 1
for i in range(0, len(numbers)):
for j in range(i + 1, len(numbers)):
if numbers[i] + numbers[j] == 2020:
print(numbers[i] * numbers[j])
# challenge 2
for i in range(0, len(numbers)):
for j in range(i+1, len(numbers)):
for k in range(j+1, len(numbers)):
if numbers[i] + numbers[j] + numbers[k] == 2020:
print(numbers[i] * numbers[j] * numbers[k])
+49
View File
@@ -0,0 +1,49 @@
import re
# decode line into bounds and letter and password
def decode_line(l):
groups = re.compile(r"(?P<lower>\d+)-(?P<higher>\d+) (?P<letter>\w): (?P<pwd>\w+)").search(l)
return groups
# check if valid with certain number of chars
def check_if_valid(lower, higher, letter, pwd):
if int(lower) <= pwd.count(letter) <= int(higher):
return True
else:
return False
# check if valid with chars on places
def check_if_valid2(lower, higher, letter, pwd):
# both = line.split(": ")
# cond = both[0].split(" ")
# times = cond[0].split("-")
# lower = int(times[0])
# higher = int(times[1])
# letter = cond[1]
# pwd = (both[1])[:-1]
if (pwd[int(lower) - 1] == letter) != (pwd[int(higher) - 1] == letter):
return True
else:
return False
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input02") as f:
lines = f.readlines()
# count for challenge 1
i = 0
for line in lines:
if check_if_valid(*decode_line(line).groups()):
i += 1
print(i)
# count for challenge 2
i = 0
for line in lines:
if check_if_valid2(*decode_line(line).groups()):
i += 1
print(i)
+41
View File
@@ -0,0 +1,41 @@
# count trees when going 1 down and 2 right
def tree_count(ls):
x = 0
trees = 0
for y, line in enumerate(ls):
if line[x] == '#':
trees += 1
x = (x + 3) % (len(line) - 1)
return trees
# count trees when going dx right and dy down
def tree_count2(ls, dx, dy):
x = 0
y = 0
trees = 0
while True:
try:
if ls[y][x] == '#':
trees += 1
x = (x + dx) % (len(ls[y]) - 1)
y = y + dy
except IndexError:
break
return trees
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input03") as f:
lines = f.readlines()
# Count trees on way down
print(tree_count(lines))
# Multiply trees on different slopes
print(tree_count2(lines, 1, 1) *
tree_count2(lines, 3, 1) *
tree_count2(lines, 5, 1) *
tree_count2(lines, 7, 1) *
tree_count2(lines, 1, 2))
+78
View File
@@ -0,0 +1,78 @@
import re
# Valid Passport if all fields on it
def valid_passport(line):
# Must contain "byr:", "pid:", "iyr:". "eyr:", "hgt:" "hcl:" und "ecl:"
contains = ["byr:", "pid:", "iyr:", "eyr:", "hgt:", "hcl:", "ecl:"]
return all(i in line for i in contains)
# Bene's function for height
def valid_height(height):
match = re.compile(r"(\d+)(cm|in)").search(height)
if match is None: return False
num, unit = match.groups()
if unit == 'cm': return 150 <= int(num) <= 193
if unit == 'in': return 59 <= int(num) <= 76
return False
# Valid passport if fields also contain fitting values
def valid_passport2(line):
# Must contain "byr:", "pid:", "iyr:". "eyr:", "hgt:" "hcl:" und "ecl:"
contains = ["byr:", "pid:", "iyr:", "eyr:", "hgt:", "hcl:", "ecl:"]
if all(i in line for i in contains):
groups = re.compile(r"(?P<key>\w+):(?P<value>\S+)")
for (key, value) in groups.findall(line):
if key == "byr":
if not (len(value) == 4 and (1920 <= int(value) <= 2002)):
return False
elif key == "iyr":
if not (len(value) == 4 and (2010 <= int(value) <= 2020)):
return False
elif key == "eyr":
if not (len(value) == 4 and (2020 <= int(value) <= 2030)):
return False
elif key == "hgt":
pattern1 = re.compile(r"(1[5-8][0-9]|19[0-3])cm")
pattern2 = re.compile(r"(59|6[0-9]|7[0-6])in")
if not valid_height(value):
return False
elif key == "hcl":
pattern = re.compile(r"#([0-9]|[a-f]){6}")
if not pattern.match(value):
return False
elif key == "ecl":
if value not in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
return False
elif key == "pid":
pattern = re.compile(r"\d{9}")
if not (pattern.match(value) and len(value) == 9):
return False
else:
if not key == "cid":
return False
return True
return False
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input04") as f:
lines = f.read()
lines = lines.replace("\n\n", '\t').replace("\n", " ").split("\t")
sum(valid_passport(p) for p in lines)
# count for challenge 1
count = 0
for l in lines:
if valid_passport(l):
count += 1
print(count)
# count for challenge 2
count2 = 0
for l in lines:
if valid_passport2(l):
count2 += 1
print(count2)
+23
View File
@@ -0,0 +1,23 @@
# Get ID
def to_binary(line):
line = line.replace('B', '1').replace('F', '0').replace('R', '1').replace('L', '0')
return int(line, 2)
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input05") as f:
lines = f.readlines()
nums = [to_binary(line) for line in lines]
# Find biggest seat-ID
print(max(nums))
# Find missing seat-ID
for num in nums:
if (num in nums) and (num+2 in nums) and (num+1 not in nums):
print(num+1)
for i in range(min(nums), max(nums)+1):
if i not in nums:
print(i)
+32
View File
@@ -0,0 +1,32 @@
# count answers in that group
def count_answers(line):
my_list = list(line)
my_set = set(my_list)
length = len(my_set)
return length
# count which answers were answered by all
def count_answers_all(group):
answers = group.split('\n')
z = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z'}
intersect = z.intersection(* [set(answer) for answer in answers if answer != ''])
return len(intersect)
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input06") as f:
lines = f.read()
# count answered letters
lines1 = lines.replace("\n\n", '\t').replace("\n", "").split("\t")
count = sum(count_answers(l) for l in lines1)
print(count)
# count answered by all
groups = lines.split("\n\n")
count2 = sum(count_answers_all(g) for g in groups)
print(count2)
+61
View File
@@ -0,0 +1,61 @@
import re
# find colours and the colours it can contain
def parse(l):
group = re.compile(r"(?P<colour>.*?) bags contain").search(l)
group2 = re.compile(r"(?P<number>\d+) (?P<colour>.*?) bag").findall(l)
return group.group(1), group2
# count what a bag of given colour can contain
def containments(colour):
if colour not in dic:
memo[colour] = 0
if colour in memo:
return memo[colour]
total = 0
for (x, y) in dic[colour]:
total += int(x) * containments(y)
memo[colour] = total
return total
# count number of bags, that can be contained
def number_bags(colour):
if colour not in dic:
memo2[colour] = 0
if colour in memo2:
return memo2[colour]
total = 0
for (x,y) in dic[colour]:
total += int(x) + int(x) * number_bags(y)
memo2[colour] = total
return total
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input07") as f:
lines = f.readlines()
# make dictionary with the values
dic = {}
for line in lines:
key, value = parse(line)
dic[key] = value
# for challenge 1
memo = {"shiny gold": 1}
count = 0
for key in dic.keys():
if containments(key) > 0:
count += 1
print(count-1)
# print for challenge 2
memo2 = {}
print(number_bags("shiny gold"))
+55
View File
@@ -0,0 +1,55 @@
import re
def decode(line):
groups = re.compile(r"(?P<op>\w\w\w) (?P<value>[+|-]\d+)").search(line)
return groups
def run(dict):
line = 0
acc = 0
while True:
(x, y) = dict[line]
if x == 'stop':
return acc
if x == 'nop':
dict[line] = ('stop', y)
line += 1
if x == 'acc':
dict[line] = ('stop', y)
acc += int(y)
line += 1
if x == 'jmp':
dict[line] = ('stop', y)
line = line + int(y)
if line > 632:
return acc
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input08") as f:
lines = f.readlines()
memo = {}
for n, line in enumerate(lines):
memo[n] = decode(line).groups()
(x, y) = memo[405]
if memo[405] == ('nop', y):
memo[405] = ('jmp', y)
elif memo[405] == ('jmp', y):
memo[405] = ('nop', y)
print(run(memo))
for line in memo:
for n, l in enumerate(lines):
memo[n] = decode(l).groups()
(a, b) = memo[line]
if memo[line] == ('nop', b):
memo[line] = ('jmp', b)
elif memo[line] == ('jmp', b):
memo[line] = ('nop', b)
if run(memo) == 'here':
print(line)
+39
View File
@@ -0,0 +1,39 @@
# check if a number is valid (2 in the previous 25 numbers add up to it)
def check_if_valid(prev, num):
for i in prev:
for j in prev:
if i + j == num:
return True
return False
# find a continuous list of numbers that add up to the weakness found earlier
def find_sum(num):
for d in range(0, len(lines)):
for e in range(d, len(lines)):
if sum(lines[d:e]) == num and len(lines[d:e]) > 1:
return lines[d:e]
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input09") as f:
lines = f.readlines()
# convert ti integers
for (a, l) in enumerate(lines):
lines[a] = int(l)
# find weakness
weakness = 0
for k in range(25, len(lines)-1):
if not check_if_valid(lines[(k-25):k], lines[k]):
weakness = lines[k]
break
# challenge 1 print
print(weakness)
# challenge 2 print
print(min(find_sum(weakness)) + max(find_sum(weakness)))
+58
View File
@@ -0,0 +1,58 @@
def adapter_chain(adapts):
jolt1 = 0
jolt3 = 1
for i in range(1, len(adapts)):
if adapts[i-1] - adapts[i] == 1:
jolt1 += 1
elif adapts[i-1] - adapts[i] == 3:
jolt3 += 1
else:
print(adapts[i] - adapts[i-1])
return jolt1 * jolt3
def memo(f):
memo = {}
def f2(x):
if x not in memo:
memo[x] = f(x)
return memo[x]
return f2
@memo
def combinations(i):
j = 0
if i == max(nums):
return 1
if i+1 in nums:
j += combinations(i+1)
if i+2 in nums:
j += combinations(i+2)
if i+3 in nums:
j += combinations(i+3)
return j
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input10") as f:
lines = f.readlines()
# convert to integers
nums = [int(line) for line in lines]
nums.append(0)
nums.sort()
nums.reverse()
print(adapter_chain(nums))
nums.append(nums[-1] + 3)
nums.reverse()
print(combinations(0))
+141
View File
@@ -0,0 +1,141 @@
from copy import deepcopy
def test(array, x, y):
test = []
if not x-1 < 0:
try:
test.append(array[x-1][y])
except IndexError:
pass
try:
test.append(array[x - 1][y + 1])
except IndexError:
pass
try:
test.append(array[x - 1][y - 1])
except IndexError:
pass
try:
test.append(array[x][y-1])
except IndexError:
pass
try:
test.append(array[x + 1][y - 1])
except IndexError:
pass
try:
test.append(array[x + 1][y])
except IndexError:
pass
try:
test.append(array[x][y + 1])
except IndexError:
pass
try:
test.append(array[x + 1][y + 1])
except IndexError:
pass
return test
def occupy_seats(array):
second = deepcopy(array)
changes = 0
for x in range(0, len(array)):
for y in range(0, len(array[x])):
tests = test(array, x, y)
d = tests.count('#')
if array[x][y] == 'L' and d == 0:
second[x][y] = '#'
changes += 1
if array[x][y] == "#" and d > 3:
second[x][y] = 'L'
changes += 1
return changes, second
def oob(space, y, x):
if y < 0: return True
if y >= len(space): return True
if x < 0: return True
if x >= len(space[y]): return True
return False
def visible_seats(space, y, x):
dirs = [(y, x) for x in [0, 1, -1] for y in [0, 1, -1]]
neighbors = []
for a, b in dirs:
if a == 0 and b == 0: continue
step = 1
def pos():
return (step * a + y, step * b + x)
while (not oob(space, *pos())):
loc = space[pos()[0]][pos()[1]]
if loc == "L" or loc == "#":
neighbors.append(loc)
break
step += 1
return neighbors
def occupy_seats2(array):
second = deepcopy(array)
changes = 0
for x in range(0, len(array)):
for y in range(0, len(array[x])):
d = visible_seats(array, x, y).count('#')
if array[x][y] == 'L' and d == 0:
second[x][y] = '#'
changes += 1
if array[x][y] == "#" and d > 4:
second[x][y] = 'L'
changes += 1
return changes, second
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input11") as f:
lines = f.readlines()
for i in range(0, len(lines)):
lines[i] = list(lines[i])
lines2 = deepcopy(lines)
while True:
c, lines = occupy_seats(lines)
if c == 0:
break
count = 0
for line in lines:
count += line.count('#')
print(count)
while True:
c, lines2 = occupy_seats2(lines2)
print(c)
if c == 0:
break
count2 = 0
for line in lines2:
count2 += line.count('#')
print(count2)
+76
View File
@@ -0,0 +1,76 @@
import re
def parse(l):
group = re.compile(r"(?P<dir>\w)(?P<int>\d+)").findall(l)
return group[0]
def move_ship(direction, amount, east, north, facing):
array = ['N', 'E', 'S', 'W']
amount = int(amount)
if direction == 'F':
direction = facing
if direction == 'N':
north += amount
if direction == 'S':
north -= amount
if direction == 'E':
east += amount
if direction == 'W':
east -= amount
if direction == 'L':
pos = array.index(facing)
pos2 = amount // 90
facing = array[pos - pos2]
if direction == 'R':
pos = array.index(facing)
pos2 = amount // 90
facing = array[(pos + pos2) % 4]
return east, north, facing
def move_ship_waypoint(direction, amount, ship, waypoint):
amount = int(amount)
if direction == 'F':
ship += waypoint*amount
if direction == 'N':
waypoint += amount * (0+1j)
if direction == 'S':
waypoint += amount * (0-1j)
if direction == 'E':
waypoint += amount * (1+0j)
if direction == 'W':
waypoint += amount * (-1+0j)
if direction == 'L':
waypoint = waypoint * 1j**(amount // 90)
if direction == 'R':
waypoint = waypoint * (-1j)**(amount // 90)
return ship, waypoint
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input12") as f:
lines = f.readlines()
# make dictionary with the values
directions = []
for line in lines:
directions.append(parse(line))
# current position [east / west, north / south, facing]
current = [0, 0, 'E']
for dire in directions:
current = move_ship(*dire, *current)
print(abs(current[0]) + abs(current[1]))
ship_position = 0+0j
waypoint_position = 10+1j
for dire in directions:
ship_position, waypoint_position = move_ship_waypoint(*dire, ship_position, waypoint_position)
print(abs(ship_position.real) + abs(ship_position.imag))
+54
View File
@@ -0,0 +1,54 @@
from numpy import prod
def find_earliest_bus(start, buses):
i = start
next = 0
for time in buses:
if time == 'x':
pass
elif int(time) - start % int(time) < i:
i = int(time) - (start % int(time))
next = int(time)
return next * i
def time_to_bus(start, bus):
i = -start % bus
return i
def find_earliest_timestamp(timestamps):
dict = {}
for i in range(0, len(timestamps)):
if timestamps[i] == 'x':
pass
else:
line = int(timestamps[i])
dict[line] = i
t = 0
kgV = 1
for bus, wait in dict.items():
while not time_to_bus(t, bus) == (wait % bus):
t += kgV
kgV = kgV * bus
return t
#for n in range(1352008337, 1000000000000000000):
# ar = [(n + dict[b]) % b == 0 for b in dict.keys()]
# if False not in ar:
# return n
# print(n)
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input13") as f:
lines = f.readlines()
lines[0] = int(lines[0])
lines[1] = lines[1].split(",")
print(find_earliest_bus(lines[0], lines[1]))
print(find_earliest_timestamp(lines[1]))
+92
View File
@@ -0,0 +1,92 @@
import re
def decode_line(line):
mask = re.search(r"mask = (?P<mask>[1 | 0 | X]{36})", line)
mem = re.search(r"mem\[(?P<address>\d+)] = (?P<value>\d+)", line)
if mask:
return 'm', mask.group('mask')
if mem:
return mem.group('address', 'value')
def to_int(num):
new = 0
for j in range(0, len(num)):
if num[j] == '1':
new += 2**(len(num) - j - 1)
return new
def comb(num, mas):
num = f'{num:036b}'
mas = list(mas)
num = list(num)
for i in range(0, len(num)):
if mas[i] == '0':
num[i] = '0'
if mas[i] == '1':
num[i] = '1'
return to_int(num)
def execute(arg, val):
if arg == 'm':
global mask
mask = val
else:
memory[arg] = comb(int(val), mask)
def comb_address(num, mas):
mas = list(mas)
m = to_int(mas)
x = ['0'] * 36
for i in range(0, len(mas)):
if mas[i] == 'X':
x[i] = '1'
x = to_int(x)
num = num & ~x
comb = 0
for y in range(0, 2**mask.count('X')):
yield comb | num | m
comb = ((comb | ~x) + 1) & x
def execute_v2(arg, val):
if arg == 'm':
global mask
mask = val
else:
for ad in comb_address(int(arg), mask):
memory2[ad] = int(val)
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input14") as f:
lines = f.readlines()
dec = [decode_line(l) for l in lines]
memory = {}
mask = 0
for d in dec:
execute(*d)
print(sum(memory.values()))
memory2 = {}
mask = 0
for d in dec:
execute_v2(*d)
print(sum(memory2.values()))
+19
View File
@@ -0,0 +1,19 @@
def next(num, turn, memo):
if num not in memo:
memo[num] = (0, turn)
return 0
memo[num] = (memo[num][1], turn)
return memo[num][1] - memo[num][0]
def solve(input, target):
memo, n = {}, input[-1]
for i, j in enumerate(input):
memo[j] = (0, i)
for t in range(len(input)-1, target-1):
n = next(n, t, memo)
return n
print(solve([16, 12, 1, 0, 15, 7, 11], 2020))
print(solve([16, 12, 1, 0, 15, 7, 11], 30000000))
+87
View File
@@ -0,0 +1,87 @@
import re
def decode_lines(l):
rules = re.compile(r"(?P<rule>\w+ ?\w+): (?P<lb1>\d+)-(?P<ub1>\d+) or (?P<lb2>\d+)-(?P<ub2>\d+)").findall(l)
my_ticket = re.compile(r"your ticket:\n(?P<ticket>\d+[,\d+]+)").search(l)
nearby_tickets = re.compile(r"\n(?P<ticket>\d+[,\d+]+)").findall(l)
return rules, my_ticket.group('ticket'), nearby_tickets[1:]
def is_valid(ticket):
count = 0
for value in ticket:
valid = False
for rule in rules:
if int(rule[1]) <= int(value) <= int(rule[2]) or int(rule[3]) <= int(value) <= int(rule[4]):
valid = True
if not valid:
count += int(value)
return count
def find_rule(rule):
for i in range(0, len(nt[0])):
valid = True
for ticket in nt:
value = ticket[i]
if not (int(rule[1]) <= int(value) <= int(rule[2]) or int(rule[3]) <= int(value) <= int(rule[4])):
valid = False
if valid: yield i
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input16") as f:
lines = f.readlines()
lines = "".join(lines)
rules, mine, nearby = decode_lines(lines)
nt = [tick.split(',') for tick in nearby]
mine = mine.split(',')
print(mine)
scanning_error = 0
for ticket in nt:
scanning_error += is_valid(ticket)
print(scanning_error)
for ticket in nt:
if is_valid(ticket) > 0:
nt.remove(ticket)
for ticket in nt:
if is_valid(ticket) > 0:
nt.remove(ticket)
mem = {}
for rule in rules:
mem[rule[0]] = [False] * 20
for r in find_rule(rule):
mem[rule[0]][r] = True
rulebook = {}
while True:
if len(rulebook) == 15: break
for i in range(0, 20):
count = 0
for rule, value in mem.items():
if value[i]:
count += 1
if count == 1:
for rule, value in mem.items():
if value[i]:
rulebook[i] = rule
mem.pop(rule)
print(rule)
break
print(rulebook)
print(int(mine[19])*int(mine[17])*int(mine[11])*int(mine[8])*int(mine[10])*int(mine[0]))
+122
View File
@@ -0,0 +1,122 @@
from copy import deepcopy
from termcolor import colored
def show(array):
for z in range(0, len(array)):
print(colored('\nz = ' + str(z), 'yellow'))
for i in range(0, len(array[z])):
print(colored(''.join(array[z][i]), 'blue'))
def show4(array):
for w in range(0, len(array)):
print(colored('\nw = ' + str(w), 'magenta'))
for z in range(0, len(array[w])):
print(colored('\nz = ' + str(z), 'yellow'))
for i in range(0, len(array[w][z])):
print(colored(''.join(array[w][z][i]), 'blue'))
def oob(space, z, y, x):
if z < 0: return True
if z >= len(space): return True
if y < 0: return True
if y >= len(space[z]): return True
if x < 0: return True
if x >= len(space[z][y]): return True
return False
def oob4(space, w, z, y, x):
if w < 0: return True
if w >= len(space): return True
if z < 0: return True
if z >= len(space[w]): return True
if y < 0: return True
if y >= len(space[w][z]): return True
if x < 0: return True
if x >= len(space[w][z][y]): return True
return False
def neighbors(space, z, y, x):
dirs = [(z, y, x) for x in [0, 1, -1] for y in [0, 1, -1] for z in [0, 1, -1]]
dirs.remove((0, 0, 0))
for d in dirs:
if not oob(space, z+d[0], y+d[1], x+d[2]):
yield space[z+d[0]][y+d[1]][x+d[2]]
def neighbors4(space, w, z, y, x):
dirs = [(w, z, y, x) for x in [0, 1, -1] for y in [0, 1, -1] for z in [0, 1, -1] for w in [0, 1, -1]]
dirs.remove((0, 0, 0, 0))
for d in dirs:
if not oob4(space, w+d[0], z+d[1], y+d[2], x+d[3]):
yield space[w+d[0]][z+d[1]][y+d[2]][x+d[3]]
def step(array):
next = deepcopy(array)
for z in range(0, len(array)):
for y in range(0, len(array[z])):
for x in range(0, len(array[z][y])):
c = [i for i in neighbors(array, z, y, x)].count('#')
if array[z][y][x] == '#' and not (c == 2 or c == 3):
next[z][y][x] = '.'
if array[z][y][x] == '.' and c == 3:
next[z][y][x] = '#'
return next
def step4(array):
next = deepcopy(array)
for w in range(0, len(array)):
for z in range(0, len(array[w])):
for y in range(0, len(array[w][z])):
for x in range(0, len(array[w][z][y])):
c = [i for i in neighbors4(array, w, z, y, x)].count('#')
if array[w][z][y][x] == '#' and not (c == 2 or c == 3):
next[w][z][y][x] = '.'
if array[w][z][y][x] == '.' and c == 3:
next[w][z][y][x] = '#'
return next
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input17") as f:
lines = f.readlines()
lines = [list(l) for l in lines]
empty = [[['.' for i in range(20)] for j in range(20)] for k in range(13)]
for i in range(0, 8):
for j in range(0, 8):
empty[6][j+6][i+6] = lines[j][i]
show(empty)
for i in range(0, 6):
print(colored('\n\nstep: ' + str(i+1) + '\n', 'red'))
empty = step(empty)
show(empty)
count = 0
for i in range(0, 13):
for j in range(0, 20):
count += empty[i][j].count('#')
print(colored(count, 'cyan'))
hypercube = [[[['.' for i in range(20)] for j in range(20)] for k in range(13)] for l in range(13)]
for i in range(0, 8):
for j in range(0, 8):
hypercube[6][6][j+6][i+6] = lines[j][i]
for i in range(0, 6):
print(colored('\n\nstep: ' + str(i + 1) + '\n', 'red'))
hypercube = step4(hypercube)
show4(hypercube)
count4 = 0
for i in range(0, 13):
for j in range(0, 13):
for k in range(0, 20):
count4 += hypercube[i][j][k].count('#')
print(colored(count4, 'cyan'))
+23
View File
@@ -0,0 +1,23 @@
import ast
class RewriteName(ast.NodeTransformer):
def visit_Mult(self, node):
return ast.Add()
def visit_Sub(self, node):
return ast.Mult()
def evaluate(line):
line = line.replace("*", "-")[:-1]
line = line.replace("+", "*")
new = RewriteName().visit(ast.parse(line, mode='eval'))
return eval(compile(new, '<string>', 'eval'))
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input18") as f:
lines = f.readlines()
print(evaluate(lines[0]))
print(sum([evaluate(l) for l in lines]))
+53 -37
View File
@@ -1,44 +1,60 @@
#!/usr/bin/env python3
import sys
import re
def parse_rules(lines):
rules = {}
for line in lines:
rule, pattern = line.split(':')
alternatives = pattern.split('|')
rules[int(rule)] = [a.strip(' ').split(' ') for a in alternatives]
return rules
def memo(f):
mem = {}
def helper(x):
if x not in mem: mem[x] = f(x)
return mem[x]
return helper
memo = {}
def f2(x):
if x not in memo:
memo[x] = f(x)
return memo[x]
return f2
def parse(l):
srules = re.compile(r"(?P<rule>\d+): (?P<num1>\d+)\n").findall(l)
rules = re.compile(r"(?P<rule>\d+): (?P<num1>\d+) (?P<num2>\d+)\n").findall(l)
wrules = re.compile(r"(?P<rule>\d+): \"(?P<body>\w)\"\n").findall(l)
crules = re.compile(r"(?P<rule>\d+): (?P<num11>\d+) (?P<num12>\d+) \| (?P<num21>\d+) (?P<num22>\d+)\n").findall(l)
csrules = re.compile(r"(?P<rule>\d+): (?P<num11>\d+) \| (?P<num21>\d+)\n").findall(l)
messages = re.compile(r"(?P<message>\w+)\n").findall(l)
return rules, wrules, crules, srules, csrules, messages
def check_if_valid(message):
return 0
@memo
def expr(rule):
if rule == 8:
return expr(42) + '+'
if rule == 11:
return '(?:' + expr(42) + '(?=a*(\1?+b)b*(\2?+c)))+\1' + expr(31) + ')'
global rules
if type(rule) is str:
try: rule = int(rule)
except ValueError: return rule[1]
pattern = rules[rule]
res = ["".join(expr(dep) for dep in alt) for alt in pattern]
if len(res) == 1: return res[0]
return "(" + "|".join(res) + ")"
def build_regex(rule):
if dict[rule] == 'a' or dict[rule] == 'b':
return dict[rule]
if type(dict[rule]) == int:
return build_regex(dict[rule])
s = "("
for i, r in enumerate(dict[rule]):
if i == 2:
s += ")|("
s += build_regex(r)
return s + ")"
if __name__ == '__main__':
rules, messages = open(sys.argv[1]).read().split('\n\n')
rules = parse_rules(rules.split('\n'))
count = 0
messages = messages.split("\n")
for m in messages:
if bool(re.match('^' + expr(0) + '$', m)):
count += 1
print(count)
with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input19") as f:
lines = f.readlines()
lines = "".join(lines)
r, w, c, s, messages = parse(lines)
rules = r + w + c + s
dict = {}
for rule in rules:
if len(rule) == 2:
try: dict[int(rule[0])] = int(rule[1])
except ValueError: dict[int(rule[0])] = rule[1]
if len(rule) == 3:
dict[int(rule[0])] = int(rule[1]), int(rule[2])
if len(rule) == 5:
dict[int(rule[0])] = int(rule[1]), int(rule[2]), int(rule[3]), int(rule[4])
print(dict)
print(build_regex(0))
print(sum([check_if_valid(m) for m in messages]))
+44
View File
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
import sys
import re
def parse_rules(lines):
rules = {}
for line in lines:
rule, pattern = line.split(':')
alternatives = pattern.split('|')
rules[int(rule)] = [a.strip(' ').split(' ') for a in alternatives]
return rules
def memo(f):
mem = {}
def helper(x):
if x not in mem: mem[x] = f(x)
return mem[x]
return helper
@memo
def expr(rule):
if rule == 8:
return expr(42) + '+'
if rule == 11:
return '(?:' + expr(42) + '(?=a*(\1?+b)b*(\2?+c)))+\1' + expr(31) + ')'
global rules
if type(rule) is str:
try: rule = int(rule)
except ValueError: return rule[1]
pattern = rules[rule]
res = ["".join(expr(dep) for dep in alt) for alt in pattern]
if len(res) == 1: return res[0]
return "(" + "|".join(res) + ")"
if __name__ == '__main__':
rules, messages = open(sys.argv[1]).read().split('\n\n')
rules = parse_rules(rules.split('\n'))
count = 0
messages = messages.split("\n")
for m in messages:
if bool(re.match('^' + expr(0) + '$', m)):
count += 1
print(count)
+45
View File
@@ -0,0 +1,45 @@
#!/bin/env python3
from copy import deepcopy
import sys
def show(space):
for row in space: print("".join(row))
print()
def neighbors(space, y, x):
dirs = [(y, x) for x in [0, 1, -1] for y in [0, 1, -1]][1:]
neighbors = []
for a, b in dirs:
try:
neighbors.append(space[y + a][x + b])
except IndexError:
pass
return neighbors
def next(space):
new = deepcopy(space)
for y in range(len(space)):
for x in range(len(space[y])):
occ = neighbors(space, y, x).count("#")
if space[y][x] == "L":
if occ == 0: new[y][x] = "#"
if space[y][x] == "#":
if occ >= 4: new[y][x] = "L"
return new
def count(space):
return sum(row.count('#') for row in space)
if __name__ == '__main__':
space = [list(line) for line in open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input11")]
while space != next(next(space)):
space = next(space)
print(count(space))
print(count(next(space)))
print(count(next(next(space))))