Advertisement
CrayonCrayoff

AoC 2024 Day 8

Dec 8th, 2024 (edited)
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. from itertools import combinations
  2. from collections import defaultdict
  3.  
  4.  
  5. with open("day8input.txt") as f:
  6.     input_lines = f.read().splitlines()
  7.  
  8.  
  9. # part 1
  10. def find_num_antennas(grid: list[str]) -> int:
  11.     antenna_locations = defaultdict(list)
  12.  
  13.     row_len = len(grid)
  14.     col_len = len(grid[0])
  15.  
  16.     # find all antenna locations, group them in the dictionary by same value
  17.     for y in range(row_len):
  18.         for x in range(col_len):
  19.             if input_lines[y][x] != '.':
  20.                 antenna_locations[grid[y][x]].append((y, x))
  21.  
  22.     # set is used to store found antinode locations to avoid double counting
  23.     output = set()
  24.     # loop through each found frequency
  25.     for key in antenna_locations:
  26.         # find all combinations of two antennas with this frequency
  27.         all_combos = list(combinations(antenna_locations[key], 2))
  28.         for combo in all_combos:
  29.             # find the offset between the two points and the antinodes on each side
  30.             diff = combo[1][0] - combo[0][0], combo[1][1] - combo[0][1]
  31.             point1 = combo[0][0] - diff[0], combo[0][1] - diff[1]
  32.             point2 = combo[1][0] + diff[0], combo[1][1] + diff[1]
  33.             # increment output if antinode location is in bounds
  34.             if 0 <= point1[0] < row_len and 0 <= point1[1] < col_len:
  35.                 output.add(point1)
  36.             if 0 <= point2[0] < row_len and 0 <= point2[1] < col_len:
  37.                 output.add(point2)
  38.  
  39.     return len(output)
  40.  
  41.  
  42. print(find_num_antennas(input_lines))
  43.  
  44.  
  45. # part 2
  46. def find_num_antennas2(grid: list[str]) -> int:
  47.     antenna_locations = defaultdict(list)
  48.  
  49.     row_len = len(grid)
  50.     col_len = len(grid[0])
  51.  
  52.     # find all antenna locations, group them in the dictionary by same value
  53.     for y in range(row_len):
  54.         for x in range(col_len):
  55.             if input_lines[y][x] != '.':
  56.                 antenna_locations[grid[y][x]].append((y, x))
  57.  
  58.     # set is used to store found antinode locations to avoid double counting
  59.     output = set()
  60.     # loop through each found frequency
  61.     for key in antenna_locations:
  62.         # find all combinations of two antennas with this frequency
  63.         all_combos = list(combinations(antenna_locations[key], 2))
  64.         for combo in all_combos:
  65.             # find the offset between the two points
  66.             diff = combo[1][0] - combo[0][0], combo[1][1] - combo[0][1]
  67.             point1 = combo[0]
  68.             point2 = combo[1]
  69.             # for all points that are the same offset away from one of the original two points or newly found ones,
  70.             # count it if it is in bounds
  71.             while 0 <= point1[0] < row_len and 0 <= point1[1] < col_len:
  72.                 output.add(point1)
  73.                 point1 = point1[0] - diff[0], point1[1] - diff[1]
  74.             while 0 <= point2[0] < row_len and 0 <= point2[1] < col_len:
  75.                 output.add(point2)
  76.                 point2 = point2[0] + diff[0], point2[1] + diff[1]
  77.  
  78.     return len(output)
  79.  
  80.  
  81. print(find_num_antennas2(input_lines))
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement