generated from public/aoc_template
day8
This commit is contained in:
parent
ec5deff7b1
commit
e240f48f52
64
day08.py
Normal file
64
day08.py
Normal file
@ -0,0 +1,64 @@
|
||||
from itertools import combinations
|
||||
from tools.aoc import AOCDay
|
||||
from typing import Any
|
||||
from tools.grid import Grid
|
||||
|
||||
|
||||
def get_antinode_count(grid: Grid, repeat: bool = False) -> int:
|
||||
antinodes = set()
|
||||
frequencies = set(grid.get(x) for x in grid.getActiveCells())
|
||||
for frequency in frequencies:
|
||||
antennas = set(x for x in grid.getActiveCells() if grid.get(x) == frequency)
|
||||
if len(antennas) < 2:
|
||||
continue
|
||||
if repeat:
|
||||
antinodes |= antennas
|
||||
|
||||
for pair in combinations(antennas, 2):
|
||||
a, b = (pair[0], pair[1]) if pair[0] < pair[1] else (pair[1], pair[0])
|
||||
diff = b - a
|
||||
|
||||
while grid.isWithinBoundaries(a - diff):
|
||||
antinodes.add(a - diff)
|
||||
if not repeat:
|
||||
break
|
||||
a -= diff
|
||||
|
||||
while grid.isWithinBoundaries(b + diff):
|
||||
antinodes.add(b + diff)
|
||||
if not repeat:
|
||||
break
|
||||
b += diff
|
||||
|
||||
return len(antinodes)
|
||||
|
||||
|
||||
class Day(AOCDay):
|
||||
inputs = [
|
||||
[
|
||||
(14, "input8_test"),
|
||||
(413, "input8"),
|
||||
],
|
||||
[
|
||||
(34, "input8_test"),
|
||||
(1417, "input8"),
|
||||
],
|
||||
]
|
||||
|
||||
def parse_input(self) -> Grid:
|
||||
grid = Grid.from_data(self.getInput(), default=False, translate={".": False})
|
||||
grid.minX, grid.minY, grid.maxX, grid.maxY = 0, 0, len(self.getInput()[0]) - 1, len(self.getInput()) - 1
|
||||
return grid
|
||||
|
||||
def part1(self) -> Any:
|
||||
grid = self.parse_input()
|
||||
return get_antinode_count(grid, repeat=False)
|
||||
|
||||
def part2(self) -> Any:
|
||||
grid = self.parse_input()
|
||||
return get_antinode_count(grid, repeat=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
day = Day(2024, 8)
|
||||
day.run(verbose=True)
|
||||
50
inputs/input8
Normal file
50
inputs/input8
Normal file
@ -0,0 +1,50 @@
|
||||
.....E........................s.......n.........g.
|
||||
.............................c............4.......
|
||||
........................................4.........
|
||||
....................................5.....e.......
|
||||
...............p...........c......................
|
||||
....h................s...................e........
|
||||
h..................1...........s..Ke............C.
|
||||
.......................1...............g.....KC...
|
||||
........8.......B.....p..kc................K..e.X.
|
||||
...b.........pI...k..................r.........C.X
|
||||
...........................5.n............R.r.....
|
||||
j......Z....tApE..............c....5..g.X.........
|
||||
............E..L......5............X..............
|
||||
b...................D...................K.....R..4
|
||||
..k..D.....h..A...........L.1.....................
|
||||
.j...........h......B.......A.....................
|
||||
.........I......b..................4.......r....0.
|
||||
.................B.n..........G...................
|
||||
..........9.I...............U...................2.
|
||||
.........Doy........s...............U....R........
|
||||
..........................G.....V............R....
|
||||
...z.o.......I..E....t.....G..n....3..............
|
||||
.Z.........Aj..................W.......M.U........
|
||||
..Z......k......O....W.....U........M.......0.....
|
||||
.....z......o.O..........a....ZG..................
|
||||
........L..........Y............a.................
|
||||
......D8t...S.......WO............................
|
||||
......1P..........WO.9..F.w........Q..d....0......
|
||||
..........y............................x..........
|
||||
............z..........w.........J................
|
||||
.o...t..P.........w..B......F....v........x....2..
|
||||
y..8...........v.......M.................x.......2
|
||||
.....y..........z..N...H.......6........a.........
|
||||
....N.S............H...................a..........
|
||||
N........S..........v........m....................
|
||||
......8...........H........7x....6.l..............
|
||||
.............q.P...............w..m...............
|
||||
.....S......................7.6.......T...........
|
||||
...............................0....3.6....J......
|
||||
...N..........v.................m.......3.l.J.....
|
||||
...........................F..d....7.3............
|
||||
...............u..................................
|
||||
.V....Y..u..........H.......J.............T.......
|
||||
.......V...q...................d..fF.............T
|
||||
..u................................f.....T......l.
|
||||
..................................i...............
|
||||
...Y......M.........................7.............
|
||||
............Y...........9............f2..m..Q.....
|
||||
.....................i.9........fd.......l....Q...
|
||||
V.........q................i......................
|
||||
12
inputs/input8_test
Normal file
12
inputs/input8_test
Normal file
@ -0,0 +1,12 @@
|
||||
............
|
||||
........0...
|
||||
.....0......
|
||||
.......0....
|
||||
....0.......
|
||||
......A.....
|
||||
............
|
||||
............
|
||||
........A...
|
||||
.........A..
|
||||
............
|
||||
............
|
||||
Loading…
Reference in New Issue
Block a user