From 87b0f107765e070b4e100eece96f7612ad599f9c Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Thu, 5 Dec 2024 06:19:13 -0500 Subject: [PATCH] Adding day3-5 --- 2024/day3/main.py | 43 +++++++++++++++++++++++ 2024/day4/main.py | 74 +++++++++++++++++++++++++++++++++++++++ 2024/day5/main.py | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 2024/day3/main.py create mode 100644 2024/day4/main.py create mode 100644 2024/day5/main.py diff --git a/2024/day3/main.py b/2024/day3/main.py new file mode 100644 index 0000000..247fd71 --- /dev/null +++ b/2024/day3/main.py @@ -0,0 +1,43 @@ +import sys +import os +import re +sys.path.insert(1, os.path.abspath('../../')) +from python_tools.aoc_utils import * + +def part_one(input): + pt_1=0 + list_of_mults = re.findall(r"mul\(\d{1,3},\d{1,3}\)", input) + for i in list_of_mults: + numbers = re.findall(r"\d{1,3}", i) + pt_1 += int(numbers[0]) * int(numbers[1]) + # print(numbers) + print("Part One: ", pt_1) + +def part_two(input): + off=False + pt_2=0 + re1 = r"mul\(\d{1,3},\d{1,3}\)" + re2 = r"do\(\)" + re3 = r"don't\(\)" + list_of_mults = re.compile("(%s|%s|%s)" % (re1, re2, re3)).findall(input) + # list_of_mults = re.findall(r"do\(\)"+r"mul\(\d{1,3},\d{1,3}\)", input) + for i in list_of_mults: + if "don't()" == i: + off = True + elif "do()" == i: + off = False + else: + if not off: + numbers = re.findall(r"\d{1,3}", i) + pt_2 += int(numbers[0]) * int(numbers[1]) + print("Part One: ", pt_2) + +def main(): + f = open("input.txt", 'r') + input = f.read() + part_one(input) + part_two(input) + +if __name__ == "__main__": + main() + diff --git a/2024/day4/main.py b/2024/day4/main.py new file mode 100644 index 0000000..ec82b1b --- /dev/null +++ b/2024/day4/main.py @@ -0,0 +1,74 @@ +import sys +import os +sys.path.insert(1, os.path.abspath('../../')) +from python_tools.aoc_utils import * + + +row_limit=0 +col_limit=0 + +def check_if_spells_xmas(input, base_row, base_col, dir_row, dir_col): + if ((base_row+dir_row*4 > row_limit) or (base_row+dir_row*3 < 0) or + (base_col+dir_col*4 > col_limit) or (base_col+dir_col*3 < 0)): + return False + for count, i in enumerate('XMAS'): + if input[base_row + count*dir_row][base_col + count*dir_col] != i: + return False + + return True + +def get_string(input, row, col, dir): + x = "" + for count in [-1,0,1]: + x += input[row+(dir*count)][col+count] + return x + +def check_if_spells_mas(input, base_row, base_col): + if ((base_row+1 >= row_limit) or (base_row-1 < 0) or + (base_col+1 >= col_limit) or (base_col-1 < 0)): + return False + cris = False + cross = False + test_str = get_string(input, base_row, base_col, 1) + cris = (test_str == 'MAS' or test_str == 'SAM') + test_str = get_string(input, base_row, base_col, -1) + cross = (test_str == 'MAS' or test_str == 'SAM') + return (cris and cross) + + +def part_one(input): + pt_1 = 0 + global row_limit + global col_limit + row_limit=len(input) + col_limit=len(input[0]) + for row in range(len(input)): + for col in range(len(input[row])): + if input[row][col] == 'X': + for i in [-1,0,1]: + for j in [-1,0,1]: + if check_if_spells_xmas(input, row, col, i, j): + pt_1 += 1 + print("Part One: ", pt_1) + +def part_two(input): + pt_2 = 0 + global row_limit + global col_limit + row_limit=len(input) + col_limit=len(input[0]) + for row in range(len(input)): + for col in range(len(input[row])): + if input[row][col] == 'A': + if check_if_spells_mas(input, row, col): + pt_2 += 1 + print("Part One: ", pt_2) + +def main(): + contents = file2list("input.txt") + part_one(contents) + part_two(contents) + +if __name__ == "__main__": + main() + diff --git a/2024/day5/main.py b/2024/day5/main.py new file mode 100644 index 0000000..36988c6 --- /dev/null +++ b/2024/day5/main.py @@ -0,0 +1,89 @@ +import sys +import os +sys.path.insert(1, os.path.abspath('../../')) +from python_tools.aoc_utils import * +import collections as C + +rules = [] +updates = [] + +def get_middle(u): + assert(len(u) % 2 == 1) + return u[int((len(u)-1)/2)] + +def check_rule_break(r, visited): + for i in visited: + if i in r: + return True + return False + +def part_one(rules, updates): + pt_1 = 0 + G = C.defaultdict(list) + for r in rules: + G[r[0]].append(r[1]) + + count = 0 + for u in updates: + update_valid = True + visited = [] + for i in u: + if check_rule_break(G[i], visited): + update_valid = False + break + else: + visited.append(i) + if update_valid: + # print(f"{count} valid {get_middle(u)}, {u}") + pt_1 += get_middle(u) + count += 1 + + print("Part One: ", pt_1) + + +def swap_order(u, G): + for count, i in enumerate(u): + for c in range(count): + if i in G[u[c]]: + u.insert(c, u.pop(count)) + break + +def part_two(input): + pt_2 = 0 + G = C.defaultdict(list) + for r in rules: + G[r[0]].append(r[1]) + + count = 0 + for u in updates: + order_needs_to_be_swapped = False + visited = [] + for i in u: + if check_rule_break(G[i], visited): + order_needs_to_be_swapped = True + break + else: + visited.append(i) + if order_needs_to_be_swapped: + swap_order(u, G) + pt_2 += get_middle(u) + count += 1 + + print("Part One: ", pt_2) + + +def main(): + f = open("input.txt") + contents = f.read().split() + for c in contents: + if "|" in c: + rules.append(list(map(int, (c.split("|"))))) + elif "," in c: + updates.append(list(map(int,c.split(",")))) + part_one(rules, updates) + + part_two(contents) + +if __name__ == "__main__": + main() +