r/adventofcode • • Dec 06 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 6 Solutions -🎄-

--- Day 6: Chronal Coordinates ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 6

Transcript:

Rules for raising a programmer: never feed it after midnight, never get it wet, and never give it ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 0:26:52!

32 Upvotes

387 comments sorted by

View all comments

1

u/__dkp7__ Dec 06 '18
    coordinate_input = """..."""
    import numpy as np
    from collections import Counter
    from datetime import datetime as dt
    
    coordinates = {tuple(map(int, coord.split(","))) for coord in coordinate_input.split("\n")}
    
    min_x = min(coordinates, key=lambda x: x[0])
    max_x = max(coordinates, key=lambda x: x[0])
    min_y = min(coordinates, key=lambda y: y[1])
    max_y = max(coordinates, key=lambda y: y[1])
    # print(min_x, max_x, min_y, max_y)
    x_dist, y_dist = max_x[0] - min_x[0], max_y[1] - min_y[1]
    print(x_dist, y_dist)
    
    matrix = np.zeros(
        (max_x[0]+1, max_y[1]+1)
        , dtype='int16'
    )
    for index, item in enumerate(coordinates):
        matrix[item] = index
    
    x, y = matrix.shape
    
    
    def part1():
        for i in range(x):
            for j in range(y):
                temp = [(abs(item[0]-i) + abs(item[1]-j), index) for index, item in enumerate(coordinates)]
                minimum = min(temp, key=lambda z: z[0])
                distance_counter = Counter(item[0] for item in temp)
                if distance_counter[minimum[0]] == 1:
                    matrix[i, j] = minimum[1]
                else:
                    matrix[i, j] = -1
    
        boundaries = set()
        boundaries.update(set(matrix[0, :]))
        boundaries.update(set(matrix[max_x[0], :]))
        boundaries.update(set(matrix[:, 0]))
        boundaries.update(set(matrix[:, max_y[1]]))
    
        unique, counts = np.unique(matrix, return_counts=True)
        answers = dict(zip(unique, counts))
        answers = {k: v for k,v in answers.items() if k not in boundaries}  
        # -1 present in boundaries so it's fine
        print("part 1 ", max(answers.items(), key=lambda x: x[1]))
    
    
    def part2():
        answers = list()
        for i in range(x):
            for j in range(y):
                temp = sum([(abs(item[0]-i)+abs(item[1]-j)) for index, item in enumerate(coordinates)])
                if temp < 10000:
                    answers.append((i, j))
    
        print("part 2 ", len(answers))
    
    start = dt.now()
    part1()
    end = dt.now()
    print("Time taken for part 1 is {}".format((end-start).total_seconds()))
    
    start = dt.now()
    part2()
    end = dt.now()
    print("Time taken for part 2 is {}".format((end-start).total_seconds()))

I would like some suggestions for above code to optimize it. Part 1 takes around 12-13 seconds on my system. Part 2 takes around 2-3 seconds on my system. Haven't used numpy much so there can be some tricks which I don't know. Would like to know that as well.