From efc78f5842972b478f3d90c2459af64afdeffddb Mon Sep 17 00:00:00 2001 From: Szymon Zborowski Date: Sun, 8 Dec 2024 15:30:37 +0100 Subject: [PATCH] Day8 2024 finished --- 2024/inputs/day8 | 50 +++++++++++++++++++++++++++++++++++ 2024/inputs/day8_ex | 12 +++++++++ 2024/src/day8.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+) create mode 100644 2024/inputs/day8 create mode 100644 2024/inputs/day8_ex create mode 100644 2024/src/day8.py diff --git a/2024/inputs/day8 b/2024/inputs/day8 new file mode 100644 index 0000000..14bb226 --- /dev/null +++ b/2024/inputs/day8 @@ -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.................................... \ No newline at end of file diff --git a/2024/inputs/day8_ex b/2024/inputs/day8_ex new file mode 100644 index 0000000..de0f909 --- /dev/null +++ b/2024/inputs/day8_ex @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ \ No newline at end of file diff --git a/2024/src/day8.py b/2024/src/day8.py new file mode 100644 index 0000000..886c25c --- /dev/null +++ b/2024/src/day8.py @@ -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))