Day 2, Year 2015: I Was Told There Would Be No Math
First read the problem description.
def wrapping_paper_area(l, w, h):
= [l*w, w*h, h*l]
side_area = 2 * sum(side_area)
surface_area = min(side_area)
slack return surface_area + slack
def total_wrapping_paper_area(iter):
return sum((wrapping_paper_area(*l) for l in iter))
def string_to_list(s):
return (map(int, l.split('x'))
for l in s.splitlines())
def ribbon_length(l, w, h):
if l >= w and l >= h:
= (w, h)
smaller_dimensions
if w >= l and w >= h:
= (l, h)
smaller_dimensions
if h >= l and h >= w:
= (l, w)
smaller_dimensions
return 2*sum(smaller_dimensions) + l*w*h
def total_ribbon_length(iter):
return sum((ribbon_length(*l) for l in iter))
assert wrapping_paper_area(2, 3, 4) == 58
assert wrapping_paper_area(1, 1, 10) == 43
import import_ipynb
import helper
= helper.read_file('2015_2.txt') s
total_wrapping_paper_area(string_to_list(s))
1586300
assert ribbon_length(2, 3, 4) == 34
assert ribbon_length(1, 1, 10) == 14
total_ribbon_length(string_to_list(s))
3737498
Source code of the solution(s):