Day 11: Cosmic Expansion

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)
  • Code block support is not fully rolled out yet but likely will be in the middle of the event. Try to share solutions as both code blocks and using something such as https://topaz.github.io/paste/ , pastebin, or github (code blocks to future proof it for when 0.19 comes out and since code blocks currently function in some apps and some instances as well if they are running a 0.19 beta)

FAQ


🔒 Thread is locked until there’s at least 100 2 star entries on the global leaderboard

🔓 Unlocked after 9 mins

  • purplemonkeymad
    link
    fedilink
    arrow-up
    3
    ·
    edit-2
    1 year ago

    I saw that coming, but decided to do it the naive way for part 1, then fixed that up for part 2. Thanks to AoC I can also recognise a Manhattan distance written in a complex manner.

    Python
    from __future__ import annotations
    
    import re
    import math
    import argparse
    import itertools
    
    def print_sky(sky:list):
        for r in sky:
            print("".join(r))
    
    class Point:
        def __init__(self,x:int,y:int) -> None:
            self.x = x
            self.y = y
    
        def __repr__(self) -> str:
            return f"Point({self.x},{self.y})"
    
        def distance(self,point:Point):
            # Manhattan dist
            x = abs(self.x - point.x)
            y = abs(self.y - point.y)
            return x + y
    
    def expand_galaxies(galaxies:list,position:int,amount:int,index:str):
        for g in galaxies:
            if getattr(g,index) > position:
                c = getattr(g,index)
                setattr(g,index, c + amount)
    
    def main(line_list:list,part:int):
        ## list of lists is the plan for init idea
    
        expand_value = 2 -1
        if part == 2:
            expand_value = 1e6 -1
        if part > 2:
            expand_value = part -1
    
        sky = list()
        for l in line_list:
            row_data = [*l]
            sky.append(row_data)
        
        print_sky(sky)
        
        # get galaxies
        gal_list = list()
        for r in range(0,len(sky)):
            for c in range(0,len(sky[r])):
                if sky[r][c] == '#':
                    gal_list.append(Point(r,c))
    
        print(gal_list)
    
        col_indexes = list(reversed(range(0,len(sky))))
        # expand rows
        for i in col_indexes:
            if not '#' in sky[i]:
                expand_galaxies(gal_list,i,expand_value,'x')
    
        # check for expanding columns
        for i in reversed( range(0, len( sky[0] )) ):
            col = [sky[x][i] for x in col_indexes]
            if not '#' in col:
                expand_galaxies(gal_list,i,expand_value,'y')
    
        print(gal_list)
    
        # find all unique pair distance sum, part 1
        sum = 0
        for i in range(0,len(gal_list)):
            for j in range(i+1,len(gal_list)):
                sum += gal_list[i].distance(gal_list[j])
    
        print(f"Sum distances: {sum}")
    
    if __name__ == "__main__":
        parser = argparse.ArgumentParser(description="template for aoc solver")
        parser.add_argument("-input",type=str)
        parser.add_argument("-part",type=int)
        args = parser.parse_args()
        filename = args.input
        if filename == None:
            parser.print_help()
            exit(1)
        part = args.part
        file = open(filename,'r')
        main([line.rstrip('\n') for line in file.readlines()],part)
        file.close()