import pandas as pd
Day 1
Part 1
import requests as r
= (
nums "https://filebin.net/qjkb2njdu0gl7kxz/input.txt?t=kjulapu7")
r.get(
.text.strip()"\n")
.split(
)= sorted(map(int, nums))
nums 10] + ["-----"] + nums[-10:] nums[:
def main_two():
= len(nums)
n for i in range(n):
for j in range(i + 1, n):
= nums[i], nums[n - j]
a, b # print(a,b, a+b)
if a + b < 2020:
break # break J
if a + b == 2020:
return a * b
main_two()
def main_three():
= len(nums)
n for i in range(n):
for j in range(i + 1, n):
for k in range(n):
= nums[i]
a = nums[n - j]
b
# iterate from middle(n//2) to +1, -1, +2, -2, and so on
= n // 2 + (-1) ** k * k // 2
idx_middle = nums[idx_middle]
c
if a + b + c == 2020:
return a * b * c
main_three()
Day 2
# part 1
def valid_pass(line):
= line.strip()
line if line == "":
return False
= re.split("-| |: ", line)
MIN, MAX, s, pwd = int(MIN), int(MAX)
MIN, MAX return MIN <= pwd.count(s) <= MAX
import re
with open("data/advent_of_code/2020/day2.txt") as f:
= f.readline()
line = int(valid_pass(line))
cnt while line:
= f.readline()
line += valid_pass(line)
cnt cnt
# part 2
def valid_pass(line):
= line.strip()
line if line == "":
return False
= re.split("-| |: ", line)
i, j, s, pwd = int(i), int(j)
i, j return (pwd[i - 1] == s) ^ (pwd[j - 1] == s)
import re
with open("data/advent_of_code/2020/day2.txt") as f:
= f.readline()
line = int(valid_pass(line))
cnt while line:
= f.readline()
line += valid_pass(line)
cnt cnt
Day 3
import numpy as np
= """..##.......
s #...#...#..
.#....#..#.
..#.#...#.#
.#...##..#.
..#.##.....
.#.#.#....#
.#........#
#.##...#...
#...##....#
.#..#...#.#"""
= [[int(c) for c in row] for row in s.replace(".", "0").replace("#", "1").split()]
lst = np.array(lst)
sarr sarr
= len(sarr) - 1
n_steps n_steps
= np.hstack([sarr] * n_steps)
full_map full_map.shape
11, :11] full_map[:
= 1
delta_y = 3 delta_x
= coord_y = 0 coord_x
def make_step(delta_x, delta_y):
global coord_x
global coord_y
+= delta_x
coord_x += delta_y coord_y
full_map
= 0
num_trees for i in range(n_steps):
make_step(delta_x, delta_y)+= full_map[coord_y, coord_x]
num_trees if full_map[coord_y, coord_x]:
print(f"Tree encountered at step {i}. coords_xy = {coord_x, coord_y}")
= 4
full_map[coord_y, coord_x]
print(f"Num Trees = {num_trees}")
Part 1
import numpy as np
with open("data/advent_of_code/2020/day3.txt") as f:
= f.read()
s
= [[int(c) for c in row] for row in s.replace(".", "0").replace("#", "1").split()]
lst = np.array(lst)
sarr = np.hstack([sarr] * len(sarr))
full_map
class Plane:
def __init__(self, delta_x, delta_y):
self.coord_x = 0
self.coord_y = 0
self.delta_x = delta_x
self.delta_y = delta_y
def step(self):
self.coord_x += self.delta_x
self.coord_y += self.delta_y
def calc_for_slope(delta_x, delta_y):
= Plane(delta_x, delta_y)
p = 0
num_trees
while p.coord_y < len(sarr) - delta_y:
p.step()+= full_map[p.coord_y, p.coord_x]
num_trees if p.coord_y >= len(sarr) - delta_y:
print("wow")
print(f"Num Trees = {num_trees} for slope right={delta_x}, down={delta_y}")
return num_trees
3, 1) calc_for_slope(
Part 2
= [
slopes 3, 1),
(1, 2),
(1, 1),
(5, 1),
(7, 1),
(
]
= {slope: calc_for_slope(*slope) for slope in slopes}
results
= 1
ans for k, v in results.items():
*= v
ans ans
Day 4
= """ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
batch byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in"""
import re
= batch.split("\n\n")
passports = passports[0] p
= "byr iyr eyr hgt hcl ecl pid".split(" ")
necessary_keys necessary_keys
with open("data/advent_of_code/2020/day4.txt") as f:
= f.read() batch
= 0
n_valid
for p in batch.split("\n\n"):
= [elem for elem in re.split(" |\n", p.strip())]
p_elems for e in p_elems:
if e == "":
print("wow")
pass
= dict()
dpass
for kv in [e.split(":") for e in p_elems]:
= kv
k, v = v
dpass[k] # print(dpass)
= []
missing_keys for k in necessary_keys:
if k not in dpass:
missing_keys.append(k)
if len(missing_keys):
print(f"invalid, keys missing: {missing_keys}")
pass
else:
# print(f"valid")
+= 1
n_valid
n_valid
def validate_pass(dpass):
# step 1: all keys are present
= []
missing_keys for k in necessary_keys:
if k not in dpass:
return False
# byr
= dpass["byr"]
byr if len(byr) != 4:
return False
= int(byr)
byr if byr < 1920 or byr > 2002:
return False
# iyr
= dpass["iyr"]
iyr if len(iyr) != 4:
return False
= int(iyr)
iyr if iyr < 2010 or iyr > 2020:
return False
# eyr
= dpass["eyr"]
eyr if len(eyr) != 4:
return False
= int(eyr)
eyr if eyr < 2020 or eyr > 2030:
return False
# hgt
= dpass["hgt"]
hgt if hgt[-2:] not in ["cm", "in"]:
return False
= int(hgt[:-2]), hgt[-2:]
n, unit if unit == "cm":
if n < 150 or n > 193:
return False
elif unit == "in":
if n < 59 or n > 76:
return False
else:
raise ValueError()
# hcl
= dpass["hcl"]
hcl = "#[0-9a-f]{6}"
pat if re.fullmatch(pat, hcl) is None:
return False
# ecl
= dpass["ecl"]
ecl if ecl not in ["amb", "blu", "brn", "gry", "grn", "hzl", "oth"]:
return False
# pid
= dpass["pid"]
pid = "\d{9}"
pat if re.fullmatch(pat, pid) is None:
return False
return True
validate_pass(
{"hgt": "176cm",
"iyr": "2013",
"hcl": "#fffffd",
"ecl": "amb",
"byr": "2000",
"eyr": "2030",
"cid": "89",
"pid": "934693255",
} )
= """eyr:1972 cid:100
batch hcl:#18171d ecl:amb hgt:170 pid:186cm iyr:2018 byr:1926
iyr:2019
hcl:#602927 eyr:1967 hgt:170cm
ecl:grn pid:012533040 byr:1946
hcl:dab227 iyr:2012
ecl:brn hgt:182cm pid:021572410 eyr:2020 byr:1992 cid:277
hgt:59cm ecl:zzz
eyr:2038 hcl:74454a iyr:2023
pid:3556412378 byr:2007"""
= 0
n_valid
for p in batch.split("\n\n"):
# build dict
= [elem for elem in re.split(" |\n", p.strip())]
p_elems = dict()
dpass for kv in [e.split(":") for e in p_elems]:
= kv
k, v = v
dpass[k]
# validate
+= validate_pass(dpass)
n_valid
n_valid
= """pid:087499704 hgt:74in ecl:grn iyr:2012 eyr:2030 byr:1980
batch hcl:#623a2f
eyr:2029 ecl:blu cid:129 byr:1989
iyr:2014 pid:896056539 hcl:#a97842 hgt:165cm
hcl:#888785
hgt:164cm byr:2001 iyr:2015 cid:88
pid:545766238 ecl:hzl
eyr:2022
iyr:2010 hgt:158cm hcl:#b6652a ecl:blu byr:1944 eyr:2021 pid:093154719"""
= 0
n_valid
for p in batch.split("\n\n"):
# build dict
= [elem for elem in re.split(" |\n", p.strip())]
p_elems = dict()
dpass for kv in [e.split(":") for e in p_elems]:
= kv
k, v = v
dpass[k]
# validate
+= validate_pass(dpass)
n_valid
n_valid
with open("data/advent_of_code/2020/day4.txt") as f:
= f.read()
batch
= 0
n_valid
for p in batch.split("\n\n"):
# build dict
= [elem for elem in re.split(" |\n", p.strip())]
p_elems = dict()
dpass for kv in [e.split(":") for e in p_elems]:
= kv
k, v = v
dpass[k]
# validate
+= validate_pass(dpass)
n_valid
n_valid
Day 5
def search(arr, op, vrb=False):
= len(arr)
n if op == "lower":
return arr[: n // 2]
elif op == "upper":
return arr[n // 2 :]
def locate_row(seq):
assert len(seq) == 7, f"Len = {len(seq)}, expected 7"
= list(range(128))
rows
= {"F": "lower", "B": "upper"}
mapper
for op in seq:
= search_rows(rows, mapper[op])
rows
assert len(rows) == 1
return rows[0]
"FBFBBFF") locate_row(
def locate_col(seq):
assert len(seq) == 3, f"Len = {len(seq)}, expected 3"
= list(range(8))
cols
= {"L": "lower", "R": "upper"}
mapper
for op in seq:
= search_rows(cols, mapper[op])
cols
assert len(rows) == 1
return cols[0]
"RLR") locate_col(
def decode(seq):
= locate_row(seq[:7])
row = locate_col(seq[7:])
col return {"row": row, "col": col, "sid": row * 8 + col, "code": seq}
"FBFBBFFRLR") decode(
"BFFFBBFRRR") decode(
"FFFBBBFRRR") decode(
"BBFFBBFRLL") decode(
with open("data/advent_of_code/2020/day5.txt") as f:
= f.read().strip().split("\n")
codes
= sorted([decode(c) for c in codes], key=lambda x: x["sid"], reverse=True)[0]
myTicket myTicket
= [decode(s)["sid"] for s in codes]
seat_ids = min(seat_ids)
lo = max(seat_ids)
hi print([x for x in range(lo, hi) if x not in seat_ids]) # find empty seat
Day 6
= """abc
s
a
b
c
ab
ac
a
a
a
a
b
"""
sum([len(set(group.replace("\n", ""))) for group in s.split("\n\n")])
"\n") for group in s.strip().split("\n\n")] [group.split(
def common_chars_in_group(people):
= set(people[0])
common for person in people:
&= set(person)
common return common
= [group.split("\n") for group in s.strip().split("\n\n")]
groups = [common_chars_in_group(g) for g in groups]
commons commons
sum([len(c) for c in commons])
with open("data/advent_of_code/2020/day6.txt") as f:
= f.read() s
sum([len(set(group.replace("\n", ""))) for group in s.split("\n\n")])
= [group.split("\n") for group in s.strip().split("\n\n")]
groups = [common_chars_in_group(g) for g in groups]
commons sum([len(c) for c in commons])
Day 7
import re
= """light red bags contain 1 bright white bag, 2 muted yellow bags.
s_rules dark orange bags contain 3 bright white bags, 4 muted yellow bags.
bright white bags contain 1 shiny gold bag.
muted yellow bags contain 2 shiny gold bags, 9 faded blue bags.
shiny gold bags contain 1 dark olive bag, 2 vibrant plum bags.
dark olive bags contain 3 faded blue bags, 4 dotted black bags.
vibrant plum bags contain 5 faded blue bags, 6 dotted black bags.
faded blue bags contain no other bags.
dotted black bags contain no other bags.
"""
with open("data/advent_of_code/2020/day7.txt") as f:
= f.read() s_rules
= [l for l in re.split("\.|\n", s_rules.strip()) if l != ""]
lines -3:] lines[
['bright green bags contain 4 drab green bags, 3 drab indigo bags, 5 dull blue bags',
'posh violet bags contain 5 wavy white bags',
'clear brown bags contain no other bags']
= {}
graph = [l for l in re.split("\.|\n", s_rules.strip()) if l != ""]
lines for line in lines:
= line.strip("\n.")
line = re.split("bags|bag|,|contain", line)
logics = [x.strip() for x in logics if x.strip() != ""]
logics
= logics[0]
target = logics[1:]
can_contain
target, can_contain
= dict()
graph[target] for rule in can_contain:
if rule == "no other":
break
else:
= int(rule[0])
nbr = rule[2:]
color = nbr graph[target][color]
"shiny gold"] # can contain graph[
{'clear brown': 5, 'plaid fuchsia': 5, 'bright teal': 4, 'striped white': 1}
from collections import defaultdict
= defaultdict(list)
inverted_graph
for bag, can_contain in graph.items():
for color in can_contain:
inverted_graph[color].append(bag)
"shiny gold"] # can be stored in inverted_graph[
['shiny orange',
'mirrored crimson',
'drab blue',
'dark aqua',
'mirrored purple']
= list() variations
def dfs(color, current_path=""):
if current_path == "":
= color
current_path else:
+= f" <- {color}"
current_path
if outer_bags := inverted_graph[color]:
for bag in outer_bags:
dfs(bag, current_path)else:
variations.append(current_path)
"shiny gold") dfs(
variations
['shiny gold <- shiny orange <- pale aqua <- mirrored gold <- dark maroon',
'shiny gold <- shiny orange <- pale aqua <- mirrored gold <- dotted olive <- bright silver',
'shiny gold <- shiny orange <- pale aqua <- mirrored gold <- faded chartreuse',
'shiny gold <- shiny orange <- pale aqua <- plaid lime',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- posh lavender',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- muted violet <- light lime <- light gold <- mirrored red',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- muted violet <- light lime <- bright gold',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- muted violet <- light lime <- dull lime <- striped red <- bright silver',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- muted violet <- light lime <- dull lime <- striped red <- plaid white <- posh coral <- dim aqua',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- muted violet <- light lime <- dull lime <- drab olive <- dotted salmon <- wavy beige <- striped cyan <- wavy lavender',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- muted violet <- dotted purple',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- muted violet <- faded silver',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- dotted black',
'shiny gold <- shiny orange <- pale aqua <- dim salmon <- mirrored lavender <- plaid yellow',
'shiny gold <- shiny orange <- pale aqua <- faded orange',
'shiny gold <- shiny orange <- pale aqua <- wavy gold <- mirrored purple <- bright lavender <- posh purple <- shiny purple <- faded olive',
'shiny gold <- shiny orange <- pale aqua <- wavy gold <- mirrored purple <- bright lavender <- vibrant lavender <- vibrant orange',
'shiny gold <- shiny orange <- pale aqua <- wavy gold <- mirrored purple <- bright lavender <- vibrant lavender <- plaid teal',
'shiny gold <- shiny orange <- pale aqua <- wavy gold <- mirrored purple <- bright lavender <- dotted olive <- bright silver',
'shiny gold <- shiny orange <- pale aqua <- wavy gold <- mirrored purple <- bright lavender <- dull gray <- muted gray',
'shiny gold <- shiny orange <- plaid gray <- dull salmon',
'shiny gold <- shiny orange <- plaid gray <- light olive <- shiny salmon <- dull green',
'shiny gold <- shiny orange <- plaid gray <- light olive <- pale blue',
'shiny gold <- shiny orange <- plaid gray <- light olive <- vibrant purple',
'shiny gold <- shiny orange <- plaid gray <- light olive <- dark cyan <- faded blue <- plaid magenta',
'shiny gold <- shiny orange <- plaid gray <- muted gray',
'shiny gold <- shiny orange <- plaid gray <- striped aqua',
'shiny gold <- shiny orange <- plaid gray <- dotted lavender <- mirrored white <- drab red',
'shiny gold <- shiny orange <- plaid gray <- dotted lavender <- dotted aqua',
'shiny gold <- shiny orange <- plaid gray <- dotted lavender <- mirrored lime <- dim chartreuse <- mirrored chartreuse',
'shiny gold <- shiny orange <- plaid gray <- dotted lavender <- mirrored lime <- vibrant brown <- clear turquoise <- pale violet',
'shiny gold <- shiny orange <- drab maroon <- shiny cyan <- mirrored yellow',
'shiny gold <- shiny orange <- drab maroon <- dull white <- shiny magenta <- muted salmon',
'shiny gold <- shiny orange <- drab maroon <- dull white <- muted plum <- shiny gray <- mirrored beige',
'shiny gold <- shiny orange <- drab maroon <- dull white <- muted plum <- shiny gray <- plaid tomato',
'shiny gold <- shiny orange <- drab maroon <- dull white <- mirrored lime <- dim chartreuse <- mirrored chartreuse',
'shiny gold <- shiny orange <- drab maroon <- dull white <- mirrored lime <- vibrant brown <- clear turquoise <- pale violet',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- shiny beige',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- dull plum <- clear gold',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- dull plum <- wavy blue <- dotted coral <- dotted magenta',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- dull plum <- light turquoise <- dark yellow',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- dull plum <- light turquoise <- shiny tomato',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- bright turquoise',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- dull tomato',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- clear green',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- muted red <- muted aqua',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- muted red <- vibrant purple',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- pale indigo <- muted red <- dark cyan <- faded blue <- plaid magenta',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- posh beige <- plaid chartreuse',
'shiny gold <- shiny orange <- drab maroon <- plaid silver <- posh beige <- dotted violet',
'shiny gold <- shiny orange <- drab maroon <- striped lime <- faded magenta <- dotted gray',
'shiny gold <- shiny orange <- drab maroon <- striped lime <- dark brown <- muted coral <- dotted black',
'shiny gold <- shiny orange <- dim gray <- posh green <- vibrant blue',
'shiny gold <- shiny orange <- dim gray <- posh green <- muted salmon',
'shiny gold <- shiny orange <- dim gray <- posh green <- muted orange',
'shiny gold <- shiny orange <- dotted lime <- faded plum <- shiny cyan <- mirrored yellow',
'shiny gold <- mirrored crimson <- faded salmon <- muted lavender',
'shiny gold <- mirrored crimson <- faded salmon <- muted violet <- light lime <- light gold <- mirrored red',
'shiny gold <- mirrored crimson <- faded salmon <- muted violet <- light lime <- bright gold',
'shiny gold <- mirrored crimson <- faded salmon <- muted violet <- light lime <- dull lime <- striped red <- bright silver',
'shiny gold <- mirrored crimson <- faded salmon <- muted violet <- light lime <- dull lime <- striped red <- plaid white <- posh coral <- dim aqua',
'shiny gold <- mirrored crimson <- faded salmon <- muted violet <- light lime <- dull lime <- drab olive <- dotted salmon <- wavy beige <- striped cyan <- wavy lavender',
'shiny gold <- mirrored crimson <- faded salmon <- muted violet <- dotted purple',
'shiny gold <- mirrored crimson <- faded salmon <- muted violet <- faded silver',
'shiny gold <- mirrored crimson <- dull lime <- striped red <- bright silver',
'shiny gold <- mirrored crimson <- dull lime <- striped red <- plaid white <- posh coral <- dim aqua',
'shiny gold <- mirrored crimson <- dull lime <- drab olive <- dotted salmon <- wavy beige <- striped cyan <- wavy lavender',
'shiny gold <- mirrored crimson <- wavy violet <- drab orange <- wavy gray',
'shiny gold <- mirrored crimson <- wavy violet <- dark yellow',
'shiny gold <- mirrored crimson <- wavy violet <- faded olive',
'shiny gold <- mirrored crimson <- faded orange',
'shiny gold <- mirrored crimson <- striped beige <- posh black <- shiny lime',
'shiny gold <- mirrored crimson <- striped beige <- dim tomato',
'shiny gold <- mirrored crimson <- dim beige <- dark silver <- dark blue',
'shiny gold <- mirrored crimson <- dim beige <- clear silver',
'shiny gold <- drab blue <- wavy silver <- shiny aqua',
'shiny gold <- dark aqua',
'shiny gold <- mirrored purple <- bright lavender <- posh purple <- shiny purple <- faded olive',
'shiny gold <- mirrored purple <- bright lavender <- vibrant lavender <- vibrant orange',
'shiny gold <- mirrored purple <- bright lavender <- vibrant lavender <- plaid teal',
'shiny gold <- mirrored purple <- bright lavender <- dotted olive <- bright silver',
'shiny gold <- mirrored purple <- bright lavender <- dull gray <- muted gray']
len(set(variations))
82