From 948009891a094b0b2efa6d07aba887e612b63595 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 6 Dec 2020 20:38:44 -0700 Subject: [PATCH] Add advent code for 2020 days 1-4 --- day_1/info.md | 1 + day_1/part_1.py | 12 +++++++ day_1/part_2.py | 14 +++++++++ day_2/info.md | 1 + day_2/part_1.py | 18 +++++++++++ day_2/part_2.py | 21 +++++++++++++ day_3/info.md | 1 + day_3/part_1.py | 30 ++++++++++++++++++ day_3/part_2.py | 41 ++++++++++++++++++++++++ day_4/info.md | 8 +++++ day_4/part_1.py | 25 +++++++++++++++ day_4/part_2.py | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 256 insertions(+) create mode 100644 day_1/info.md create mode 100644 day_1/part_1.py create mode 100644 day_1/part_2.py create mode 100644 day_2/info.md create mode 100644 day_2/part_1.py create mode 100644 day_2/part_2.py create mode 100644 day_3/info.md create mode 100644 day_3/part_1.py create mode 100644 day_3/part_2.py create mode 100644 day_4/info.md create mode 100644 day_4/part_1.py create mode 100644 day_4/part_2.py diff --git a/day_1/info.md b/day_1/info.md new file mode 100644 index 0000000..46264b8 --- /dev/null +++ b/day_1/info.md @@ -0,0 +1 @@ +https://adventofcode.com/2020/day/1 \ No newline at end of file diff --git a/day_1/part_1.py b/day_1/part_1.py new file mode 100644 index 0000000..0536c1f --- /dev/null +++ b/day_1/part_1.py @@ -0,0 +1,12 @@ +filename = input("Filename: ") + +with open(filename) as file: + data = file.read().split("\n") + +for number_1 in data: + n1 = int(number_1) + for number_2 in data: + n2 = int(number_2) + if number_1 != number_2: + if n1 + n2 == 2020: + print(f"{n1} * {n2} = {n1 * n2}") \ No newline at end of file diff --git a/day_1/part_2.py b/day_1/part_2.py new file mode 100644 index 0000000..07f5872 --- /dev/null +++ b/day_1/part_2.py @@ -0,0 +1,14 @@ +filename = input("Filename: ") + +with open(filename) as file: + data = file.read().split("\n") + +for number_1 in data: + n1 = int(number_1) + for number_2 in data: + n2 = int(number_2) + for number_3 in data: + n3 = int(number_3) + if number_1 != number_2 and number_2 != number_3: + if n1 + n2 + n3 == 2020: + print(f"{n1} * {n2} * {n3} = {n1 * n2 * n3}") \ No newline at end of file diff --git a/day_2/info.md b/day_2/info.md new file mode 100644 index 0000000..9e90759 --- /dev/null +++ b/day_2/info.md @@ -0,0 +1 @@ +https://adventofcode.com/2020/day/2 \ No newline at end of file diff --git a/day_2/part_1.py b/day_2/part_1.py new file mode 100644 index 0000000..9adc63d --- /dev/null +++ b/day_2/part_1.py @@ -0,0 +1,18 @@ +with open("input") as file: + data = file.read().split("\n") + +import re + +valid_passwords = 0 + + +for password_data in data: + + rule, password = password_data.split(": ") + rules = rule.split(" ") + min_len, max_len = rules[0].split("-") + + if int(min_len) <= len(re.findall(rules[1], password)) <= int(max_len): + valid_passwords += 1 + +print(valid_passwords) \ No newline at end of file diff --git a/day_2/part_2.py b/day_2/part_2.py new file mode 100644 index 0000000..278a219 --- /dev/null +++ b/day_2/part_2.py @@ -0,0 +1,21 @@ +with open("input") as file: + data = file.read().split("\n") + +import re + +valid_passwords = 0 + + +for password_data in data: + + rule, password = password_data.split(": ") + rules = rule.split(" ") + char = rules[1] + pos1, pos2 = rules[0].split("-") + + if password[int(pos1)-1] == char and not password[int(pos2)-1] == char: + valid_passwords += 1 + if not password[int(pos1)-1] == char and password[int(pos2)-1] == char: + valid_passwords += 1 + +print(valid_passwords) \ No newline at end of file diff --git a/day_3/info.md b/day_3/info.md new file mode 100644 index 0000000..7eac9c6 --- /dev/null +++ b/day_3/info.md @@ -0,0 +1 @@ +https://adventofcode.com/2020/day/3 \ No newline at end of file diff --git a/day_3/part_1.py b/day_3/part_1.py new file mode 100644 index 0000000..a7872de --- /dev/null +++ b/day_3/part_1.py @@ -0,0 +1,30 @@ +with open("input") as f: + hill = f.read().split("\n") + +# Index deltas +slope = [ + 1, # the change in vertical height of the toboggan + 3 # the change in horizontal distance of the toboggan +] + +# The starting position of the toboggan +position = [0, 0] + +tree_count = 0 + + +while position[0] < len(hill): + + # Wrap the pattern as needed + if position[1] >= len(hill[position[0]]): + position[1] -= len(hill[position[0]]) + + # Check for trees + if hill[position[0]][position[1]] == "#": + tree_count += 1 + + + position[0] += slope[0] + position[1] += slope[1] + +print(f"Trees Hit: {tree_count}") \ No newline at end of file diff --git a/day_3/part_2.py b/day_3/part_2.py new file mode 100644 index 0000000..f67e3c8 --- /dev/null +++ b/day_3/part_2.py @@ -0,0 +1,41 @@ +with open("input") as f: + hill = f.read().split("\n") + +# Index deltas +slopes = [ + [1, 1], # right 1, down 1 + [1, 3], # right 3, down 1 + [1, 5], # right 5, down 1 + [1, 7], # right 7, down 1 + [2, 1] # right 1, down 2 +] + +# The starting position of the toboggan + +tree_counts = [] + +for slope in slopes: + + position = [0, 0] + tree_count = 0 + + while position[0] < len(hill): + + # Wrap the pattern as needed + if position[1] >= len(hill[position[0]]): + position[1] -= len(hill[position[0]]) + + # Check for trees + if hill[position[0]][position[1]] == "#": + tree_count += 1 + + + position[0] += slope[0] + position[1] += slope[1] + + tree_counts.append(tree_count) + +total = 1 +for x in tree_counts: + total *= x +print(tree_counts, total) \ No newline at end of file diff --git a/day_4/info.md b/day_4/info.md new file mode 100644 index 0000000..83ca44d --- /dev/null +++ b/day_4/info.md @@ -0,0 +1,8 @@ +https://adventofcode.com/2020/day/4 + + +Part 1 Wrong Answers: + - `260` (too low) + +Part 2 Wrong Answers: + - `225` (too high) \ No newline at end of file diff --git a/day_4/part_1.py b/day_4/part_1.py new file mode 100644 index 0000000..9b5f261 --- /dev/null +++ b/day_4/part_1.py @@ -0,0 +1,25 @@ +with open("input") as f: + data = [x.replace("\n", " ") for x in f.read().split("\n\n")] + +# The fields that passports have, if True, it's required, if False, it's an +# optional field +fields = { + "byr": True, + "iyr": True, + "eyr": True, + "hgt": True, + "hcl": True, + "ecl": True, + "pid": True, + "cid": False +} + +valid_passports = len(data) + +for passport in data: + for field, required in fields.items(): + if field not in passport and required: + valid_passports -= 1 + break + +print(f"Valid Passports: {valid_passports}") \ No newline at end of file diff --git a/day_4/part_2.py b/day_4/part_2.py new file mode 100644 index 0000000..ce84190 --- /dev/null +++ b/day_4/part_2.py @@ -0,0 +1,84 @@ +import re + +with open("input") as f: + data = [x.replace("\n", " ") for x in f.read().split("\n\n")] + +# The fields that passports have +fields = [ + "byr", + "iyr", + "eyr", + "hgt", + "hcl", + "ecl", + "pid" +] + +def year_validate(min, provided, max): + return int(min) <= int(provided) <= int(max) + +def size_validate(height): + if height.endswith("cm"): + return 150 <= int(height[:-2]) <= 193 + elif height.endswith("in"): + return 59 <= int(height[:-2]) <= 76 + return False + +def hair_validate(colour): + return re.match(r"#[\w]{6}", str(colour)) != None + +def eye_validate(colour): + return colour in ("amb", "blu", "brn", "gry", "grn", "hzl", "oth") + +def pid_validate(id): + return re.match(r"^[0-9]{9}$", str(id)) != None + +valid_passports = 0 + +# Check all the passports for valid data +for passport in data: + valid = True + + # Check each required field + for field in fields: + + # Ensure passport has field + if (i := passport.find(field)) != -1: + value = passport[i:].split(" ")[0].split(":")[1] + + if field == "byr": + if not year_validate(1920, value, 2002): + valid = False + break + elif field == "iyr": + if not year_validate(2010, value, 2020): + valid = False + break + elif field == "eyr": + if not year_validate(2020, value, 2030): + valid = False + break + elif field == "hgt": + if not size_validate(value): + valid = False + break + elif field == "hcl": + if not hair_validate(value): + valid = False + break + elif field == "ecl": + if not eye_validate(value): + valid = False + break + elif field == "pid": + if not pid_validate(value): + valid = False + break + else: + valid = False + + if valid: + valid_passports += 1 + + +print(f"Valid Passports: {valid_passports}") \ No newline at end of file