Last active
December 4, 2018 03:05
-
-
Save mpnordland/3e7419b3ea2169f7c89a9e1e90fb928d to your computer and use it in GitHub Desktop.
Advent of Code 2018 Day 3 solution and visualizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
from os.path import join, dirname | |
from collections import namedtuple | |
class Claim(namedtuple("Claim", ['id', 'x', 'y', 'width', 'height'])): | |
__slots__ = () | |
def find(self, array): | |
return array[self.x:self.x+self.width, self.y:self.y+self.height] | |
def find_input(file_name): | |
return join(dirname(sys.argv[0]), file_name) | |
def parse_claim(claim_string): | |
return Claim(*([int(field) for field in re.match(r'#(\d+) @ (\d+),(\d+): (\d+)x(\d+)', claim_string).groups()])) | |
def parse_claims(claim_list): | |
return [parse_claim(c) for c in claim_list] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
from common import parse_claims, find_input | |
def plot_claims(claims_list): | |
fabric = numpy.zeros((1000, 1000), dtype=int) | |
for claim in claims_list: | |
claim_area = claim.find(fabric) | |
claim_area += 1 | |
return fabric | |
if __name__ == "__main__": | |
input_path = find_input("input.txt") | |
with open(input_path) as input_file: | |
puzzle_input = input_file.readlines() | |
fabric = plot_claims(parse_claims(puzzle_input)) | |
print((1 < fabric).sum()) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy | |
from common import parse_claims, find_input | |
from problem1 import plot_claims | |
def find_free_claim(claims, fabric): | |
for claim in claims: | |
claim_area = claim.find(fabric) | |
if (claim_area == 1).all(): | |
return claim | |
return -1 | |
if __name__ == "__main__": | |
input_path = find_input("input.txt") | |
with open(input_path) as input_file: | |
puzzle_input = input_file.readlines() | |
claims = parse_claims(puzzle_input) | |
fabric = plot_claims(claims) | |
result = find_free_claim(claims, fabric) | |
print(result.id) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pygame | |
import math | |
from pygame.locals import * | |
from pygame.transform import scale | |
from common import find_input, parse_claims | |
from problem1 import plot_claims | |
from problem2 import find_free_claim | |
try: | |
import numpy | |
import pygame.surfarray as surfarray | |
except ImportError: | |
raise ImportError("NumPy and Surfarray are required.") | |
pygame.init() | |
screen_size = (2000, 2000) | |
img_size = (1000, 1000) | |
screen = pygame.display.set_mode(screen_size, 0, 32) | |
done = False | |
pygame.display.set_caption("Santa's suit plot") | |
input_path = find_input("input.txt") | |
with open(input_path) as input_file: | |
puzzle_input = input_file.readlines() | |
claims = parse_claims(puzzle_input) | |
fabric = plot_claims(claims) | |
free_claim = find_free_claim(claims, fabric) | |
img = numpy.zeros((*img_size, 3)) | |
img[:] = (30, 30, 30) | |
img *= fabric[:, :, numpy.newaxis] | |
free_claim.find(img)[:] = (0, 255, 0) | |
temp_surface = pygame.Surface(img_size) | |
while not done: | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
done = True | |
surfarray.blit_array(temp_surface, img) | |
#HiDPI support | |
scale(temp_surface, screen_size, screen) | |
pygame.display.flip() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment