12
submitted 11 months ago* (last edited 11 months ago) by Ategon@programming.dev to c/advent_of_code@programming.dev

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

you are viewing a single comment's thread
view the rest of the comments
[-] purplemonkeymad@programming.dev 3 points 11 months ago* (last edited 11 months 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.

Pythonfrom 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()

this post was submitted on 11 Dec 2023
12 points (80.0% liked)

Advent Of Code

736 readers
1 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 2023

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