Last active
December 10, 2020 03:11
-
-
Save skreuzer/ecb0e142ce79dae058c16ca52457df64 to your computer and use it in GitHub Desktop.
Advent of Code 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
from itertools import combinations | |
from functools import reduce | |
n = [] | |
with open('input.txt') as f: | |
for l in f.readlines(): | |
n.append(int(l.rstrip())) | |
for i in (2, 3): | |
for x in combinations(n, i): | |
if sum(list(x)) == 2020: | |
print(reduce(lambda x, y: x * y, list(x))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import re | |
p = re.compile(r"(\d{1,2})-(\d{1,2})\s([a-z]):\s(.*)") | |
with open("input.txt") as f: | |
lines = f.readlines() | |
prob1_valid_password = 0 | |
prob2_valid_password = 0 | |
for line in lines: | |
m = p.match(line) | |
(pmin, pmax, letter, passwd) = ( | |
int(m.group(1)), | |
int(m.group(2)), | |
m.group(3), | |
m.group(4), | |
) | |
x = passwd.count(letter) | |
if x >= pmin and x <= pmax: | |
prob1_valid_password += 1 | |
if bool(passwd[pmin - 1] == letter) != bool(passwd[pmax - 1] == letter): | |
prob2_valid_password += 1 | |
print(prob1_valid_password, prob2_valid_password) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
from functools import reduce | |
with open("input.txt") as f: | |
lines = f.readlines() | |
a = [] | |
total = [] | |
for line in lines: | |
x = line.rstrip() | |
for i in range(1, 15): | |
x += x | |
a.append(x) | |
max_x = len(a[0]) - 1 | |
max_y = len(a) - 1 | |
for i in ((1, 1), (1, 3), (1, 5), (1, 7), (2, 1)): | |
cord = [0, 0] | |
tc = 0 | |
while cord[0] < max_y: | |
cord[0] = cord[0] + i[0] # down | |
cord[1] = cord[1] + i[1] # right | |
if a[cord[0]][cord[1]] == "#": | |
tc = tc + 1 | |
if i == (1, 3): | |
# problem 1 | |
print(tc) | |
total.append(tc) | |
print(reduce(lambda x, y: x * y, list(total))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import re | |
rs = lambda r, x: True if re.match(r, x) else False | |
expected_fields = { | |
"byr": lambda x: rs(r"(19[2-9][0-9])|(200[0-3])", x), | |
"iyr": lambda x: rs(r"20(1[0-9]|20)", x), | |
"eyr": lambda x: rs(r"20(2[0-9]|30)", x), | |
"hgt": lambda x: rs(r"((59|6[0-9]|7[0-6])in)|1(([5678][0-9])|(9[0-3]))cm", x), | |
"hcl": lambda x: rs(r"^#[0-9a-f]{6}$", x), | |
"ecl": lambda x: rs(r"(amb|b[lr][un]|gr[yn]|hzl|oth)", x), | |
"pid": lambda x: rs(r"^\d{9}$", x), | |
} | |
records = [] | |
with open("input.txt", "r") as f: | |
contents = f.read() | |
for i in re.split(r"\n\n", contents): | |
record = {} | |
for r in i.rstrip().replace("\n", " ").split(" "): | |
(k, v) = r.split(":") | |
if k != 'cid': | |
record[k] = v | |
records.append(record) | |
valid_passport = [] | |
for r in records: | |
if set(r.keys()) == set(expected_fields.keys()): | |
valid_passport.append(r) | |
validated_passport = [] | |
for r in valid_passport: | |
valid = True | |
for e in expected_fields.keys(): | |
x = expected_fields[e](r[e]) | |
if not x: | |
valid = False | |
if valid: | |
validated_passport.append(r) | |
print(len(valid_passport)) | |
print(len(validated_passport)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
from math import floor, ceil | |
with open("input.txt", "r") as f: | |
boarding_passes = [line.rstrip() for line in f] | |
seats = [] | |
midpoint = lambda x, y: (x + y) / 2 | |
for boarding_pass in boarding_passes: | |
row_min, row_max = (0, 127) | |
seat_min, seat_max = (0, 7) | |
for i in range(0, 7): | |
if boarding_pass[i] == "F": | |
row_max = floor(midpoint(row_min, row_max)) | |
else: | |
row_min = ceil(midpoint(row_min, row_max)) | |
for i in range(7, 10): | |
if boarding_pass[i] == "R": | |
seat_min = ceil(midpoint(seat_min, seat_max)) | |
else: | |
seat_max = floor(midpoint(seat_min, seat_max)) | |
seat_id = row_min * 8 + seat_min | |
seats.append(seat_id) | |
sorted_seats = sorted(seats) | |
min_seat_id, max_seat_id = (sorted_seats[0], sorted_seats[-1]) | |
all_seats = [] | |
for i in range(min_seat_id, max_seat_id): | |
all_seats.append(i) | |
print(max_seat_id) | |
print(list(set(all_seats) - set(sorted_seats))[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import re | |
with open('input.txt', 'r') as f: | |
contents = f.read().rstrip() | |
# Problem 1 | |
records = [] | |
for i in re.split(r"\n\n", contents): | |
xyzzy = [] | |
x = i.rstrip().replace('\n', ' ').split(" ") | |
for a in x: | |
for c in a: | |
xyzzy.append(c) | |
dropdups = list( dict.fromkeys(xyzzy) ) | |
records.append(dropdups) | |
total = 0 | |
for record in records: | |
total += len(record) | |
print(total) | |
# Problem 2 | |
count = 0 | |
for groups in re.split(r"\n\n", contents): | |
group = groups.rstrip().replace('\n', ' ').split(" ") | |
count += len(set(group[0]).intersection(*group)) | |
print(count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import re | |
with open("input.txt", "r") as f: | |
contents = f.read().splitlines() | |
# Problem 1 | |
bag_type = "shiny gold" | |
count = 0 | |
hold_bag = [] | |
def bags(bag_type): | |
global hold_bag | |
is_regex = fr"(?<!^){bag_type}" | |
for i in contents: | |
if re.search(is_regex, i) is not None: | |
extract = re.search(r"^(\w+\s\w+)", i) | |
hold_bag.append(extract.group(1)) | |
bags(extract.group(1)) | |
bags(bag_type) | |
print(len(list(dict.fromkeys(hold_bag)))) | |
# Problem 2 | |
bags = {} | |
for line in contents: | |
color_regex = re.compile(r"^(\w+\s\w+)\sbags\scontain") | |
child_bags_regex = re.compile(r"(\d\s\w+\s\w+)") | |
color = color_regex.match(line) | |
bags[color.group(1)] = {} | |
if "contain no other bags." in line: | |
continue | |
kv = {} | |
child_bags = child_bags_regex.findall(line) | |
for found in child_bags: | |
s = found.split(" ") | |
kv[" ".join(s[1:])] = int(s[0]) | |
bags[color.group(1)] = kv | |
def get_bag(bag): | |
total = 1 | |
if len(bags[bag]) == 0: | |
return 1 | |
for color in bags[bag].keys(): | |
total += int(bags[bag][color]) * get_bag(color) | |
return total | |
print(get_bag("shiny gold") - 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
import copy | |
def run_program(asm, opchange=None, printbreak=True): | |
seen_location = [] | |
sp = 0 | |
acc = 0 | |
if opchange is not None: | |
printbreak = False | |
pos = opchange[0] | |
oldop = opchange[1] | |
newop = opchange[2] | |
asm[pos] = asm[pos].replace(oldop, newop) | |
while True: | |
if sp in seen_location: | |
if printbreak: | |
print(f"{acc}") | |
break | |
seen_location.append(sp) | |
(op, v) = asm[sp].split(" ") | |
if op == "nop": | |
sp += 1 | |
if op == "acc": | |
acc += int(v) | |
sp += 1 | |
if op == "jmp": | |
sp += int(v) | |
if(sp >= len(asm)): | |
print(acc) | |
break | |
with open('input.txt', 'r') as f: | |
ins = f.read().splitlines() | |
# Problem 1 | |
run_program(ins) | |
# Problem 2 | |
index = [] | |
for i in range(len(ins)): | |
t = ins[i] | |
if 'jmp' in ins[i]: | |
index.append((i, "jmp", "nop")) | |
if 'nop' in ins[i]: | |
index.append((i, "nop", "jmp")) | |
for opchange in index: | |
run_program(copy.deepcopy(ins), opchange, False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
with open("input.txt", "r") as f: | |
lines = f.read().splitlines() | |
# Problem 1 | |
preamble_length = 25 | |
pos = 0 | |
while True: | |
preamble = lines[pos : preamble_length + pos] | |
next_up = lines[pos + preamble_length] | |
sum_table = [] | |
for x in preamble: | |
for y in preamble: | |
if x != y: | |
sum_table.append(str(int(x) + int(y))) | |
if next_up not in sum_table: | |
break | |
pos += 1 | |
print(next_up) | |
# Problem 2 | |
array_len = len(lines) | |
for x in range(array_len): | |
block = [] | |
block.append(int(lines[x])) | |
for y in range(x, array_len - 1): | |
block.append(int(lines[y + 1])) | |
if sum(block) == int(next_up): | |
print(min(block) + max(block)) | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment