advent_of_code/2024/day8/main.py

65 lines
1.6 KiB
Python
Raw Normal View History

2024-12-10 19:02:13 -05:00
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()