this post was submitted on 08 Dec 2024
23 points (96.0% liked)

Advent Of Code

994 readers
3 users here now

An unofficial home for the advent of code community on programming.dev!

Advent of Code is an annual Advent calendar of small programming puzzles for a variety of skill sets and skill levels that can be solved in any programming language you like.

AoC 2024

Solution Threads

M T W T F S S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25

Rules/Guidelines

Relevant Communities

Relevant Links

Credits

Icon base by Lorc under CC BY 3.0 with modifications to add a gradient

console.log('Hello World')

founded 1 year ago
MODERATORS
 

Day 8: Resonant Collinearity

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 2 points 4 weeks ago* (last edited 3 weeks ago)

python

solution

import aoc

def setup():
    lines = aoc.get_lines(8, stripped=True)
    ll = len(lines)
    fm = {f: [(x, y) for y, r in enumerate(lines)
              for x, z in enumerate(r) if z == f]
          for f in {z for r in lines for z in r if z != '.'}}
    return ll, fm

def fa(fm, ll, rh=False):
    ans = set()
    for cd in fm.values():
        l = len(cd)
        for i in range(l):
            x1, y1 = cd[i]
            for j in range(i + 1, l):
                x2, y2 = cd[j]
                dx, dy = x2 - x1, y2 - y1
                if rh:
                    for k in range(-ll, ll):
                        x, y = x1 + k * dx, y1 + k * dy
                        if 0 <= x < ll and 0 <= y < ll:
                            ans.add((x, y))
                else:
                    x3, y3, x4, y4 = x1 - dx, y1 - dy, x2 + dx, y2 + dy
                    if 0 <= x3 < ll and 0 <= y3 < ll:
                        ans.add((x3, y3))
                    if 0 <= x4 < ll and 0 <= y4 < ll:
                        ans.add((x4, y4))
    return len(ans)

def one():
    ll, fm = setup()
    print(fa(fm, ll))

def two():
    ll, fm = setup()
    print(fa(fm, ll, rh=True))

one()
two()