From 7e2e395314d99aa267c019a66404d42eafe9b1a6 Mon Sep 17 00:00:00 2001 From: Andrew Ekstedt Date: Sun, 8 Dec 2024 05:20:33 +0000 Subject: [PATCH] day 8 --- day08/input | 50 +++++++++++++++++++++++++++++++++++ day08/sample1.in | 12 +++++++++ day08/sample2.in | 10 +++++++ day08/sample3.in | 10 +++++++ day08/sample4.in | 10 +++++++ day08/sample5.in | 12 +++++++++ day08/sol.py | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 172 insertions(+) create mode 100644 day08/input create mode 100644 day08/sample1.in create mode 100644 day08/sample2.in create mode 100644 day08/sample3.in create mode 100644 day08/sample4.in create mode 100644 day08/sample5.in create mode 100644 day08/sol.py diff --git a/day08/input b/day08/input new file mode 100644 index 0000000..1a7b6f2 --- /dev/null +++ b/day08/input @@ -0,0 +1,50 @@ +.AU..Z.....8.......................t..C.6......... +.................................................. +.....K.U....................v..................... +...Z..A.............................v8.....t...... +p..................a8...........b...t............. +..pU.....A..4..................................... +..........................E....................... +...........K..V..............v8.....Cb............ +....V................b...p........................ +....7............................................. +....4.....A..........V......K..E.....6............ +.4.........................Vb...........0..C...... +..................................k........N...... +K....7...........9...........6.....kE............. +......7......1...................k.......C........ +...p....................9....0.....N6............. +..........Z........e..1........................... +.............................E................N... +...4...............................v0..........z.. +........U.....Z......1................z..a........ +.....5.......7......................N............. +....................n............................. +.......................0.9...c..........z.d.T..... +...................n.W......a...t......D....d..... +..........I.....e......................o9......... +....5..2................e...........D............. +...........................n......D............... +......25I...1..................c......W.......o... +................n..............D.................. +...........I........i..e.......................... +......5......2.....P..............a............... +...........................z..................T... +..........j.....................Wd...........O..o. +................................................c. +.................I................B............... +...........u.............................T.d...... +.............................................J.... +.....3.i....u......................o.............. +3...i............................................. +.................................................. +...........j...............W....O............w.... +...P........................J..................... +.....u............................................ +.............................w.................... +......u.................2...w...J................. +.....j.....B3......................O.............. +P....B..............................c............. +................B.............w................... +.....i.............3.............................. +..P.j....................J..........O............. diff --git a/day08/sample1.in b/day08/sample1.in new file mode 100644 index 0000000..78a1e91 --- /dev/null +++ b/day08/sample1.in @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ diff --git a/day08/sample2.in b/day08/sample2.in new file mode 100644 index 0000000..c811684 --- /dev/null +++ b/day08/sample2.in @@ -0,0 +1,10 @@ +.......... +...#...... +.......... +....a..... +.......... +.....a.... +.......... +......#... +.......... +.......... diff --git a/day08/sample3.in b/day08/sample3.in new file mode 100644 index 0000000..e9aaca3 --- /dev/null +++ b/day08/sample3.in @@ -0,0 +1,10 @@ +.......... +...#...... +#......... +....a..... +........a. +.....a.... +..#....... +......#... +.......... +.......... diff --git a/day08/sample4.in b/day08/sample4.in new file mode 100644 index 0000000..092d8ff --- /dev/null +++ b/day08/sample4.in @@ -0,0 +1,10 @@ +.......... +...#...... +#......... +....a..... +........a. +.....a.... +..#....... +......A... +.......... +.......... diff --git a/day08/sample5.in b/day08/sample5.in new file mode 100644 index 0000000..a8ed437 --- /dev/null +++ b/day08/sample5.in @@ -0,0 +1,12 @@ +......#....# +...#....0... +....#0....#. +..#....0.... +....0....#.. +.#....A..... +...#........ +#......#.... +........A... +.........A.. +..........#. +..........#. diff --git a/day08/sol.py b/day08/sol.py new file mode 100644 index 0000000..1801527 --- /dev/null +++ b/day08/sol.py @@ -0,0 +1,68 @@ +def parse(file): + grid = [] + for line in file: + grid.append(line.strip()) + return grid + +import itertools + +import string +signal_chars = string.ascii_letters + string.digits + +def solve(file): + grid = parse(file) + ny = len(grid) + nx = len(grid[0]) + + antennae = {} + for y in range(ny): + for x in range(nx): + c = grid[y][x] + if c in signal_chars: + antennae.setdefault(c, []).append((x,y)) + + poles = set() + for c, locations in antennae.items(): + for (x0,y0), (x1,y1) in itertools.combinations(locations, 2): + x = x1 + (x1-x0) + y = y1 + (y1-y0) + #print(c, (x0,y0),(x1,y1), "=>", (x,y)) + if 0 <= x < nx and 0 <= y < ny: + poles.add((x,y)) + + x = x0 + (x0-x1) + y = y0 + (y0-y1) + if 0 <= x < nx and 0 <= y < ny: + poles.add((x,y)) + + #for y,line in enumerate(grid): + # print("".join(c if (x,y) not in poles else '#' for (x,c) in enumerate(line))) + + print(len(poles)) + + + poles = set() + for c, locations in antennae.items(): + for (x0,y0), (x1,y1) in itertools.combinations(locations, 2): + for _ in 1,2: + dx = (x1-x0) + dy = (y1-y0) + x = x1 + y = y1 + while 0 <= x < nx and 0 <= y < ny: + #print(c, (x0,y0),(x1,y1), "=>", (x,y)) + poles.add((x,y)) + x += dx + y += dy + + x0,y0, x1,y1 = x1,y1, x0,y0 + + #for y,line in enumerate(grid): + # print("".join(c if (x,y) not in poles else '#' for (x,c) in enumerate(line))) + + print(len(poles)) + + + +solve(open("sample1.in")) +solve(open("input"))