Created
December 5, 2018 00:52
-
-
Save JoshOrndorff/9ebe5e1d233572de50f7b55e2dca213f to your computer and use it in GitHub Desktop.
Brad's AOC2018 Problem 3b
This file contains hidden or 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
linelist = f.readlines() # readlines makes a list of each line... linelist is an array | |
listnum = len(linelist) # listnum is the number of lines | |
# Creates a map of dimensions init x init with all zeroes in it. Double Array | |
init = 1100 # Map size | |
map = [] | |
### Minor performance penalty for the first non-nested loop. | |
''' | |
for row in range(init): | |
for col in range(init): | |
newRow = [] | |
newRow.append(0) | |
map.append(newRow) | |
''' | |
for row in range(init): | |
map.append([]) | |
for row in map: | |
for col in range(init): | |
row.append(0) | |
# Go through the list and put the number of the item in each coordinate | |
### A real gangsta would use regular expressions for this | |
### https://www.youtube.com/watch?v=ZdDOauFIDkw (21min tutorial) | |
for line in linelist: | |
num = int(line[1:line.index("@")]) | |
startx = int(line[line.index("@")+2:line.index(",")]) | |
starty = int(line[line.index(",")+1:line.index(":")]) | |
lenx = int(line[line.index(":")+2:line.index("x")]) | |
leny = int(line[line.index("x")+1:-1]) | |
for x in range(lenx): | |
for y in range(leny): | |
map[starty + y][startx + x] += num | |
# Go through the list and see if the number in the cell is the original | |
### Don't parse these again; those string ops are expensive | |
### Make function that parses a single claim into a datastructure (dictionary would be perfect) | |
### Then in your setup phase loop through the original list of lines | |
### once saving a new list of parsed claims. | |
### See also the map function https://youtu.be/Z8Cu-8brldc?t=78 (2min example) | |
for line in linelist: | |
num = int(line[1:line.index("@")]) | |
startx = int(line[line.index("@") + 2:line.index(",")]) | |
starty = int(line[line.index(",") + 1:line.index(":")]) | |
lenx = int(line[line.index(":") + 2:line.index("x")]) | |
leny = int(line[line.index("x") + 1:-1]) | |
### out = False? Then I know immediately that it's a flag | |
### out = 0 makes me think you're about to count something. | |
out = 0 | |
for x in range(lenx): | |
for y in range(leny): | |
if map[starty + y][startx + x] != num: | |
out = 1 | |
if out == 0: | |
print(num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment