Created
December 3, 2018 16:56
-
-
Save phanirithvij/bc521f692de5aa0b0804cc7312f3e2bb to your computer and use it in GitHub Desktop.
Adventofcode D3-P1
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 as np #using numpy | |
empty = np.empty((1000,1000)) #empty grid | |
empty[:,:] = 0 #initializing the whole thing to zreoes | |
def get_input(): | |
with open("input3-1.txt") as file: #input3-1.txt file contains the input | |
for data in file.read().split("\n"): | |
if data != "": | |
parser(data) | |
def parser(string): | |
id_, _, pos, dimens = string.split(" ") #gets ['#123', '@', '348,782:', '32x36'] from '#123 @ 348,782: 32x36' | |
id_ = id_[1:] #gets '123' out of '#123' | |
posx = pos.split(',')[0] #gets '348' out of '348,782:' | |
posy = pos.split(',')[1][:-1] #gets '782' out of '348,782:' | |
dimensx = dimens.split("x")[0] #gets '32' out of '32x36' | |
dimensy = dimens.split("x")[1] #gets '36' out of '32x36' | |
# print(id_, posx, posy, dimensx, dimensy) | |
id_, posx, posy, dimensx, dimensy = int(id_), int(posx), int(posy), int(dimensx), int(dimensy) #str to int | |
empty[posy:posy + dimensy,posx: posx + dimensx] += 1 #increasing the overlap regions by 1 each time in the whole region by using numpy | |
# print(empty[posy:posy + dimensy,posx: posx + dimensx]) | |
get_input() | |
print(np.where(empty > 1)) # gives all the overlapping elements as > 1 means two or more overlapped | |
print(np.count_nonzero(np.where(empty > 1))) #print no. of elements whcih are non-zero and > 1 | |
#this is the answer as the area is the number of elements in that 2d array where it's > 1 (overlap count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment