From 3b8deabf5368f84ca91e6ce50b3ae88f7f3c3e1f Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Tue, 10 Dec 2024 19:02:13 -0500 Subject: [PATCH] Commiting the past couple days --- 2024/day10/main.py | 74 ++++++++++++++++++++++++++++++++++++++++++++ 2024/day7/main.py | 66 +++++++++++++++++++++++++++++++++++++++ 2024/day8/main.py | 64 ++++++++++++++++++++++++++++++++++++++ 2024/day9/main.py | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 281 insertions(+) create mode 100644 2024/day10/main.py create mode 100644 2024/day7/main.py create mode 100644 2024/day8/main.py create mode 100644 2024/day9/main.py diff --git a/2024/day10/main.py b/2024/day10/main.py new file mode 100644 index 0000000..a72f9b9 --- /dev/null +++ b/2024/day10/main.py @@ -0,0 +1,74 @@ +import sys +import os +sys.path.insert(1, os.path.abspath('../../')) +from python_tools.aoc_utils import * + +TH = [] +LIMIT = [] +def find_trail_heads(input): + for ci, i in enumerate(input): + for cj, j in enumerate(i): + if j == 0: + TH.append([ci, cj]) +VISITED = set() +def trail_scorep2(node, map): + score = 0 + for k in [[-1,0],[1,0],[0,-1],[0,1]]: + i = k[0] + j = k[1] + if ((0 <= node[0]+i < len(map)) and (0 <= node[1]+j < len(map[0]))): + if(tuple([node[0]+i, node[1]+j]) not in VISITED): + if map[node[0]][node[1]] + 1 == map[node[0]+i][node[1]+j]: + if map[node[0]+i][node[1]+j] == 9: + score += 1 + else: + score += trail_scorep2([node[0]+i, node[1]+j], map) + return score + +def trail_score(node, map): + VISITED.add(tuple(node)) + score = 0 + for k in [[-1,0],[1,0],[0,-1],[0,1]]: + i = k[0] + j = k[1] + if ((0 <= node[0]+i < len(map)) and (0 <= node[1]+j < len(map[0]))): + if(tuple([node[0]+i, node[1]+j]) not in VISITED): + if map[node[0]][node[1]] + 1 == map[node[0]+i][node[1]+j]: + if map[node[0]+i][node[1]+j] == 9: + VISITED.add(tuple([node[0]+i, node[1]+j])) + score += 1 + else: + score += trail_score([node[0]+i, node[1]+j], map) + return score + +def part_one(input): + p1 = 0 + find_trail_heads(input) + for i in TH: + VISITED.clear() + out = trail_score(i, input) + p1 += out + # print(out) + print("Part One: ", p1) + + +def part_two(input): + p2 = 0 + for i in TH: + VISITED.clear() + out = trail_scorep2(i, input) + p2 += out + # print(out) + print("Part Two: ", p2) + +def main(): + f = open('input.txt', 'r') + contents = [list(map(int, list(x.strip()))) for x in f.readlines()] + # for i in contents: + # print(''.join(str(i))) + part_one(contents) + part_two(contents) + +if __name__ == "__main__": + main() + diff --git a/2024/day7/main.py b/2024/day7/main.py new file mode 100644 index 0000000..0cbbbe8 --- /dev/null +++ b/2024/day7/main.py @@ -0,0 +1,66 @@ +import sys +from math import log10 +import os +sys.path.insert(1, os.path.abspath('../../')) +from python_tools.aoc_utils import * + +def look_for_solution_p1(ans, input): + if (len(input) == 1): + return (ans == input[0]) + else: + ans1 = look_for_solution_p1(ans-input[-1], input[:-1]) + ans2 = look_for_solution_p1(ans/input[-1], input[:-1]) + return ans1 or ans2 + +def digits(n): + return int(log10(n)) + 1 + +def look_for_solution_p2(ans, input): + if (len(input) == 1): + return (ans == input[0]) + else: + ans1, ans2, ans3 = False, False, False + if (ans-input[-1]) >= 0: + ans1 = look_for_solution_p2(ans-input[-1], input[:-1]) + if int(ans/input[-1]) == ans/input[-1]: + ans2 = look_for_solution_p2(ans/input[-1], input[:-1]) + #first check ans is a int + if int(ans) == ans: + if input[-1] == int(str(int(ans))[-digits(input[-1]):]): + new_ans = str(int(ans))[:-digits(input[-1])] + if new_ans == '': + ans = look_for_solution_p2(0, input[:-1]) + # print("SD:LFKJSDF", input, ans) + # return True + else: + ans3 = look_for_solution_p2(int(new_ans), input[:-1]) + return ans1 or ans2 or ans3 + +def part_one(g): + p1 = 0 + for i in g: + if look_for_solution_p1(i, g[i]): + p1+=i + print("Part One: ", p1) + +def part_two(g): + p2 = 0 + for i in g: + if look_for_solution_p2(i, g[i]): + p2+=i + print("Part Two: ", p2) + +G = {} + +def main(): + contents = file2list("input.txt") + for i in contents: + x = i.split(":") + G[int(x[0])] = list(map(int, x[1].split())) + # print(G) + part_one(G) + part_two(G) + +if __name__ == "__main__": + main() + diff --git a/2024/day8/main.py b/2024/day8/main.py new file mode 100644 index 0000000..9931170 --- /dev/null +++ b/2024/day8/main.py @@ -0,0 +1,64 @@ +import sys +import os +sys.path.insert(1, os.path.abspath('../../')) +from python_tools.aoc_utils import * +import collections as C +import math + +G = C.defaultdict(list) +A = set() +B = set() +limits = [0,0] + +def add_antinodesp1(pos1, pos2): + N1 = [2*pos1[0]-pos2[0], 2*pos1[1]-pos2[1]] + if 0 <= N1[0] < limits[0] and 0 <= N1[1] < limits[1]: + A.add(tuple(N1)) + +def add_antinodesp2(pos1, pos2): + diff = [pos1[0]-pos2[0], pos1[1]-pos2[1]] + step = 1 + N1 = [pos1[0]- step*diff[0], pos1[1]-step*diff[1]] + while 0 <= N1[0] < limits[0] and 0 <= N1[1] < limits[1]: + B.add(tuple(N1)) + step+=1 + N1 = [pos1[0]- step*diff[0], pos1[1]-step*diff[1]] + + +def part_one(g): + for i in g: + for j in range(len(g[i])-1): + for k in range(j+1, len(g[i])): + add_antinodesp1(g[i][j], g[i][k]) + add_antinodesp1(g[i][k], g[i][j]) + print("Part1: ", len(A)) + +def part_two(g): + for i in g: + for j in range(len(g[i])-1): + for k in range(j+1, len(g[i])): + add_antinodesp2(g[i][j], g[i][k]) + add_antinodesp2(g[i][k], g[i][j]) + print("Part2: ", len(B)) + +def main(): + contents = file2listoflists("test.txt") + limits[0] = len(contents) + limits[1] = len(contents[0]) + for row, line in enumerate(contents): + for col, character in enumerate(line): + if character != '.': + G[character].append([row, col]) + # print(G) + part_one(G) + part_two(G) + + # for i in A: + # contents[i[0]][i[1]] = '#' + # for i in contents: + # print(''.join(i)) + + +if __name__ == "__main__": + main() + diff --git a/2024/day9/main.py b/2024/day9/main.py new file mode 100644 index 0000000..9c36c51 --- /dev/null +++ b/2024/day9/main.py @@ -0,0 +1,77 @@ +import sys +import os +sys.path.insert(1, os.path.abspath('../../')) +from python_tools.aoc_utils import * + + +L = [] + +def part_one(input): + p1 = 0 + empty = False + id = 0 + for i in input: + if empty: + L.extend(int(i)*['.']) + else: + L.extend(int(i)*[id]) + id = (id + 1) + empty = not empty + idx = 0 + while(idx < len(L)): + if L[idx] == '.': + end = L.pop() + while end == '.' and idx 0: + if len(L2)+end_idx < idx + 1: + break + elif L2[end_idx].count('.') == 0: + if len(L2[end_idx]) <= L2[idx].count('.'): + temp = L2[end_idx] + for c, i in enumerate(temp): + L2[idx][L2[idx].index('.')] = i + L2[end_idx][c] = '.' + + end_idx -= 1 + idx+=1 + + Flat_L2 = [j for i in L2 for j in i] + for count, i in enumerate(Flat_L2): + if i != '.': + p2 += count*i + print("Part Two: ", p2) + + +def main(): + f = open('input.txt', 'r') + contents = f.read().strip() + + part_one(contents) + part_two(contents) + +if __name__ == "__main__": + main() +