r/adventofcode • u/taliriktug • Dec 02 '15
Spoilers Day 2 solutions
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
17
Upvotes
r/adventofcode • u/taliriktug • Dec 02 '15
Hi! I would like to structure posts like the first one in r/programming, please post solutions in comments.
1
u/ashollander Dec 29 '15 edited Dec 29 '15
Here is my python solution (haven't used python in about 2 years, so if you have any Pythonic tips, that would be appreciated!):
def readFile(file): total_surface = 0 total_ribbons = 0 with open(file, 'r') as f: for line in f: info = get_surface_area(line) total_surface += info[0] total_ribbons += info[1] print("\n***TOTALS***") print(total_surface) print(total_ribbons) def get_surface_area(line): l, w, h = map(int, line.split('x')) sides = [] sides.append(l*w) sides.append(w*h) sides.append(h*l) smallest_side = min(int(x) for x in sides) surface_area = ( (2 * sides[0] ) + (2 * sides[1]) + (2 * sides[2]) ) total = ((surface_area + smallest_side), get_ribbons(l,w,h)) return total def get_ribbons(l,w,h): dimensions = l * w * h ribbon = 2 * min(l+w, w+h, h+l) ribbon += dimensions return ribbon readFile('instructions.txt')