Add solution for day 5 and adjust template
This commit is contained in:
1 parent
67b4bcbe21
commit
caf362ef2f
5 files changed
+667
-2
No files matched your search
Executable
+82
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import re
|
||||
|
||||
def parse(line):
|
||||
groups = re.compile(r"(?P<x1>\d+),(?P<y1>\d+) -> (?P<x2>\d+),(?P<y2>\d+)").search(line)
|
||||
return groups
|
||||
|
||||
def calc(x1, y1, x2, y2, mem):
|
||||
if x1 == x2:
|
||||
if y1 > y2:
|
||||
y = y2
|
||||
y2 = y1
|
||||
y1 = y
|
||||
for y in range(y1, y2+1):
|
||||
if (x1, y) not in mem:
|
||||
mem[(x1, y)] = 1
|
||||
else:
|
||||
mem[(x1, y)] += 1
|
||||
elif y1 == y2:
|
||||
if x1 > x2:
|
||||
x = x2
|
||||
x2 = x1
|
||||
x1 = x
|
||||
for x in range(x1, x2+1):
|
||||
if (x, y1) not in mem:
|
||||
mem[(x, y1)] = 1
|
||||
else:
|
||||
mem[(x, y1)] += 1
|
||||
|
||||
|
||||
def calc2(x1, y1, x2, y2):
|
||||
if x1 != x2 and y1 != y2:
|
||||
if x1 > x2:
|
||||
dif = x1-x2+1
|
||||
m1 = -1
|
||||
else:
|
||||
dif = x2-x1+1
|
||||
m1 = 1
|
||||
if y1 > y2:
|
||||
m2 = -1
|
||||
else:
|
||||
m2 = 1
|
||||
for i in range(dif):
|
||||
if (x1+m1*i, y1+m2*i) not in memo2:
|
||||
memo2[(x1+m1*i, y1+m2*i)] = 1
|
||||
else:
|
||||
memo2[(x1+m1*i, y1+m2*i)] += 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
lines = open(sys.argv[1]).readlines()
|
||||
|
||||
coords = [tuple(map(int,parse(line).groups())) for line in lines]
|
||||
|
||||
# challenge 1
|
||||
|
||||
memo = {}
|
||||
for c in coords:
|
||||
calc(*c,memo)
|
||||
|
||||
res1 = 0
|
||||
for v in memo.values():
|
||||
if v > 1:
|
||||
res1 += 1
|
||||
|
||||
print("challenge 1:" + "\n" + str(res1) + "\n")
|
||||
|
||||
# challenge 2
|
||||
|
||||
memo2 = {}
|
||||
for c in coords:
|
||||
calc(*c,memo2)
|
||||
calc2(*c)
|
||||
|
||||
res2 = 0
|
||||
for v in memo2.values():
|
||||
if v > 1:
|
||||
res2 += 1
|
||||
print("challenge 2:" + "\n" + str(res2) + "\n")
|
||||
|
||||
Reference in new issue
Block a user