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

  • TunaCowboy@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    4 days 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()