Solve 2016/03
This commit is contained in:
parent
10e81c9331
commit
f1be074b5f
2 changed files with 1947 additions and 0 deletions
1902
2016/03/input.txt
Normal file
1902
2016/03/input.txt
Normal file
File diff suppressed because it is too large
Load diff
45
2016/03/solve.py
Normal file
45
2016/03/solve.py
Normal file
|
|
@ -0,0 +1,45 @@
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
TRIANGLE_RE = r"\s*(\d+)\s+(\d+)\s+(\d+)\n"
|
||||||
|
|
||||||
|
def load_triangles(filename):
|
||||||
|
triangles = []
|
||||||
|
with open(filename, "r") as f:
|
||||||
|
for line in f:
|
||||||
|
match = re.fullmatch(TRIANGLE_RE, line)
|
||||||
|
a, b, c = match.groups()
|
||||||
|
a, b, c = int(a), int(b), int(c)
|
||||||
|
triangles.append((a, b, c))
|
||||||
|
return triangles
|
||||||
|
|
||||||
|
# PART 1
|
||||||
|
|
||||||
|
def is_valid(triangle):
|
||||||
|
a, b, c = sorted(triangle)
|
||||||
|
return a + b > c
|
||||||
|
|
||||||
|
def count_valid(triangles):
|
||||||
|
return len(list(filter(is_valid, triangles)))
|
||||||
|
|
||||||
|
# PART 2
|
||||||
|
|
||||||
|
def transform_triangles(triangles):
|
||||||
|
new_triangles = []
|
||||||
|
while triangles:
|
||||||
|
t1, t2, t3 = triangles[:3]
|
||||||
|
triangles = triangles[3:]
|
||||||
|
new_triangles.extend(zip(t1, t2, t3))
|
||||||
|
return new_triangles
|
||||||
|
|
||||||
|
def main(filename):
|
||||||
|
triangles = load_triangles(filename)
|
||||||
|
print(f"Solutions for {filename}")
|
||||||
|
valid = count_valid(triangles)
|
||||||
|
print(f"Part 1: {valid}")
|
||||||
|
valid_2 = count_valid(transform_triangles(triangles))
|
||||||
|
print(f"Part 2: {valid_2}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
for filename in sys.argv[1:]:
|
||||||
|
main(filename)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue