Skip to content

Commit

Permalink
Day8 2024 finished
Browse files Browse the repository at this point in the history
  • Loading branch information
ZoltePudeleczko committed Dec 8, 2024
1 parent f6af3bf commit efc78f5
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 2024/inputs/day8
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
.....8............1r.....a....................O...
.a..............4..q.........................0...9
....a.........8...................................
.................D.....................V0.........
.....d............................................
.r..........q....................................O
..................q...........................9...
..............D..............X..................V.
........D................X.................0......
.........8............X...........................
....................J....................9..0.....
..a..B............r..W........J...............R..Q
......WD...q.....1..........Q..............R..V...
.1W...................u...........................
..............................u.............R.....
....B..............d..c..................R........
.............J..............X............V........
......1...........................3...............
......B...........d...................3...........
............8..J.......u.....3....................
...........4.............6........................
.....4v.............d.......................O.....
..........................v.2.....................
.............................................s....
..................4...M..W..................s.....
......................m...........................
...........M......................................
..b..................c............................
....................Co..........KQ.......O.s......
.................C............................s...
.......x............c............................3
........o......A....U.....Q.........5.............
...............U..................j...5...........
.....K.......U................j..........2........
.......A.v.....w.....................c...5........
..K....................................j..........
...............K.yk....B.............2............
......C....b..............x...........Y...........
.....mA..C......U.................................
........M.....A.....................2..6...5......
.............................7.......Y............
.m.M......w..v....................................
............m...........x.....Y...................
....................k....w........................
......b.....w..S....7.............................
..............S..............x...........Y........
....................S...6.........................
.y...............S..........7.6.................9.
o..........k...............b......................
yo...........k....................................
12 changes: 12 additions & 0 deletions 2024/inputs/day8_ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
64 changes: 64 additions & 0 deletions 2024/src/day8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from common import *

DAY_NUMBER = 8
USE_EXAMPLE = False


with get_data(DAY_NUMBER, USE_EXAMPLE) as f:
city = [l.strip() for l in f.readlines()]


def is_out_of_board(pos):
return pos[1] < 0 or pos[0] < 0 or pos[1] >= len(city) or pos[0] >= len(city[0])


def find_other_antennas(antenna, x, y):
return [
(i, j)
for i in range(len(city))
for j in range(len(city[0]))
if city[j][i] == antenna and (i, j) != (x, y)
]


def get_antinodes(x1, y1, x2, y2, one_antinode_per_line):
x_diff = x1 - x2
y_diff = y1 - y2
antinodes = [(x1, y1), (x2, y2)] if not one_antinode_per_line else []

def add_antinodes(x, y, x_step, y_step):
while True:
x += x_step
y += y_step

if is_out_of_board((x, y)):
break
antinodes.append((x, y))

if one_antinode_per_line:
break

add_antinodes(x1, y1, x_diff, y_diff)
add_antinodes(x2, y2, -x_diff, -y_diff)

return antinodes


def count_antinodes(one_antinode_per_line):
antinodes = set()
for x, y in [
(x, y)
for x in range(len(city))
for y in range(len(city[0]))
if city[y][x] != "."
]:
for x2, y2 in find_other_antennas(city[y][x], x, y):
for anti in get_antinodes(x, y, x2, y2, one_antinode_per_line):
if not is_out_of_board(anti):
antinodes.add(anti)

return len(antinodes)


print_result(DAY_NUMBER, 1, count_antinodes(True))
print_result(DAY_NUMBER, 2, count_antinodes(False))

0 comments on commit efc78f5

Please sign in to comment.