[py] Simplify 2015_02
This commit is contained in:
parent
aaa074d66d
commit
455aa617ba
5 changed files with 17 additions and 40 deletions
|
|
@ -1,45 +1,16 @@
|
||||||
import re
|
def area(l, w, h):
|
||||||
|
sides = [l * w, w * h, h * l]
|
||||||
PACKET_RE = r"(\d+)x(\d+)x(\d+)"
|
return 2 * sum(sides) + min(sides)
|
||||||
|
|
||||||
|
|
||||||
def load_packets(inputstr):
|
def ribbon(l, w, h):
|
||||||
packets = []
|
half_perimeters = [l + w, w + h, h + l]
|
||||||
for line in inputstr.splitlines():
|
return 2 * min(half_perimeters) + l * w * h
|
||||||
match = re.fullmatch(PACKET_RE, line)
|
|
||||||
a, b, c = match.groups()
|
|
||||||
a, b, c = int(a), int(b), int(c)
|
|
||||||
packets.append((a, b, c))
|
|
||||||
return packets
|
|
||||||
|
|
||||||
|
|
||||||
# PART 1
|
|
||||||
|
|
||||||
|
|
||||||
def necessary_area(packet):
|
|
||||||
a, b, c = sorted(packet)
|
|
||||||
return 3 * a * b + 2 * a * c + 2 * b * c
|
|
||||||
|
|
||||||
|
|
||||||
def total_wrapping_paper(packets):
|
|
||||||
return sum(map(necessary_area, packets))
|
|
||||||
|
|
||||||
|
|
||||||
# PART 2
|
|
||||||
|
|
||||||
|
|
||||||
def ribbon_length(packet):
|
|
||||||
a, b, c = sorted(packet)
|
|
||||||
return 2 * a + 2 * b + a * b * c
|
|
||||||
|
|
||||||
|
|
||||||
def total_ribbon_length(packets):
|
|
||||||
return sum(map(ribbon_length, packets))
|
|
||||||
|
|
||||||
|
|
||||||
def solve(inputstr):
|
def solve(inputstr):
|
||||||
packets = load_packets(inputstr)
|
boxes = [tuple(map(int, line.split("x"))) for line in inputstr.splitlines()]
|
||||||
total = total_wrapping_paper(packets)
|
part1 = sum(area(l, w, h) for l, w, h in boxes)
|
||||||
print(f"Part 1: {total}")
|
print(f"Part 1: {part1}")
|
||||||
total_2 = total_ribbon_length(packets)
|
part2 = sum(ribbon(l, w, h) for l, w, h in boxes)
|
||||||
print(f"Part 2: {total_2}")
|
print(f"Part 2: {part2}")
|
||||||
|
|
|
||||||
1
sample_inputs/2015/2015_02.01.input
Normal file
1
sample_inputs/2015/2015_02.01.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
2x3x4
|
||||||
2
sample_inputs/2015/2015_02.01.solution
Normal file
2
sample_inputs/2015/2015_02.01.solution
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
Part 1: 58
|
||||||
|
Part 2: 34
|
||||||
1
sample_inputs/2015/2015_02.02.input
Normal file
1
sample_inputs/2015/2015_02.02.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
1x1x10
|
||||||
2
sample_inputs/2015/2015_02.02.solution
Normal file
2
sample_inputs/2015/2015_02.02.solution
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
Part 1: 43
|
||||||
|
Part 2: 14
|
||||||
Loading…
Add table
Add a link
Reference in a new issue