diff --git a/2024/day8/input.text b/2024/day8/input.text new file mode 100644 index 0000000..be461e7 --- /dev/null +++ b/2024/day8/input.text @@ -0,0 +1,50 @@ +..........M..........j.............y.....O........ +...B...............q......m........lGO............ +....................q......2.l.GQ...O............. +.....X.......................................4.... +.....................q............................ +....M......P...............xl.K.............2..... +....F.........L.......C.K..............m.......... +..........FM......P....jy......m..........o...r... +..X.......P.....RL..............G..x..........4... +............L..........NC.....q................... +.....C.X...............K....y..........4.......... +........S...R.............j.x.....V...4........... +.....................R..x.....V..i......m......... +...........................R.V......N.......X..... +.....F.........M......N......E.................... +................v................T.......F......O. +.............................N...V.......Q........ +...v.....................C.....i.................. +......c.....W..n.w........................E....... +3...................c.....................Q..6.... +...........h......................j............... +.......n.0......h.................E..............2 +.v.............7.......120.....c.................. +......n.0............w...........D.t.........E...r +....8..3......0.w.hP....z...D..T...............r.. +.................f........T........G......eQ...... +......f.n.....7..p................................ +.....Y..7.......f......I......D......K............ +............Uf....T..W.....D..r...i............... +......I...............................Z........... +....5....B.......b..............s..............Z.. +..........d...W..Uwh.............c..........i..... +..I.3..Y......................e................... +.....p.b..........k......7........................ +p...........k....I..b..........s.................. +.....k.......o...........W........................ +.A..Y..........U.................a........6....... +..A...Y.p...................................6..... +B......k..........................Z............u.. +...3.....................s..............a......... +......A.........................g.....a........... +.......A....8...b.U......H....sS.................. +.........................S1.............t......... +.....................9z..e.....5..1.g.u........... +.......................z....d....g....H.J....o.6.. +........B................d.....u....9.J.H......... +.8........S.................u9.............J.....H +.....................Z5.............t1...........a +.....................e..v...................o..t.. +.....8...............L.....z.............J........ \ No newline at end of file diff --git a/2024/day8/main.py b/2024/day8/main.py new file mode 100644 index 0000000..27edf39 --- /dev/null +++ b/2024/day8/main.py @@ -0,0 +1,48 @@ +from vector import Vector + +with open('input.text', 'r') as file: + data: list[str] = file.readlines() + + +groups = {} + +# Parse Data +y_bound: int = len(data) +x_bound: int = len(data[0].strip()) + +for y, line in enumerate(data): + for x, char in enumerate(line): + if char not in ['.', '\n']: + if char not in groups: + groups[char] = [Vector(x, y)] + else: + groups[char].append(Vector(x, y)) + +# Part 1 +def in_bounds(v: Vector) -> bool: + return v.x >= 0 and v.x < x_bound and v.y >= 0 and v.y < y_bound + +antinodes = [] +for char, group in groups.items(): + for i, A in enumerate(group): + for B in group[i + 1:]: + + antinode_pos = A + A - B + antinode_pos2 = B + B - A + + if antinode_pos not in antinodes and in_bounds(antinode_pos): + antinodes.append(antinode_pos) + + if antinode_pos2 not in antinodes and in_bounds(antinode_pos2): + antinodes.append(antinode_pos2) + +for antinode in antinodes: + data[antinode.y] = data[antinode.y][:antinode.x] + '#' + data[antinode.y][antinode.x + 1:] + +for line in data: + print(line, end='') + +print(f'\nPart 1: {len(antinodes)}') + +# Part 2 + diff --git a/2024/day8/test.text b/2024/day8/test.text new file mode 100644 index 0000000..de0f909 --- /dev/null +++ b/2024/day8/test.text @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ \ No newline at end of file diff --git a/2024/day8/vector.py b/2024/day8/vector.py new file mode 100644 index 0000000..c96d4a6 --- /dev/null +++ b/2024/day8/vector.py @@ -0,0 +1,31 @@ +from typing import Self + +import math + +class Vector: + x: int + y: int + + def __init__(self, x: int, y: int): + self.x = x + self.y = y + + def __add__(self, other: Self) -> Self: + return Vector(self.x + other.x, self.y + other.y) + + def __sub__(self, other: Self) -> Self: + return Vector(self.x - other.x, self.y - other.y) + + def abs(self) -> Self: + return Vector(abs(self.x), abs(self.y)) + + def __str__(self) -> str: + return f'{self.x}, {self.y}' + + def __eq__(self, value: Self) -> bool: + return self.x == value.x and self.y == value.y + + def distance(self, other: Self) -> int: + return math.sqrt((other.x - self.x)**2 + (other.y - self.y)**2) + +