#!/usr/bin/env python # coding: utf-8 # In[66]: def wrapping_paper_area(l, w, h): side_area = [l*w, w*h, h*l] surface_area = 2 * sum(side_area) slack = min(side_area) 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: smaller_dimensions = (w, h) if w >= l and w >= h: smaller_dimensions = (l, h) if h >= l and h >= w: smaller_dimensions = (l, w) return 2*sum(smaller_dimensions) + l*w*h def total_ribbon_length(iter): return sum((ribbon_length(*l) for l in iter)) # In[67]: assert wrapping_paper_area(2, 3, 4) == 58 assert wrapping_paper_area(1, 1, 10) == 43 # In[68]: import import_ipynb import helper s = helper.read_file('2015_2.txt') # In[69]: total_wrapping_paper_area(string_to_list(s)) # In[70]: assert ribbon_length(2, 3, 4) == 34 assert ribbon_length(1, 1, 10) == 14 # In[71]: total_ribbon_length(string_to_list(s))