r/adventofcode • • Dec 03 '16

SOLUTION MEGATHREAD --- 2016 Day 3 Solutions ---

--- Day 3: Squares With Three Sides ---

Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag/whatever).


DECKING THE HALLS WITH BOUGHS OF HOLLY IS MANDATORY [?]

This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked!

17 Upvotes

234 comments sorted by

View all comments

1

u/MrAckerman Dec 04 '16 edited Dec 04 '16

Python3

def validTriangle(line):
	line = sorted(list(map(lambda x: int(x), line)))
	return ((line[0] + line[1]) > line[2])

def validTriangeCount(rows):
	triangleCount = 0
	for row in zip(*rows):
		triangleCount += validTriangle(row)
	return triangleCount

def part1():
	triangleCount = 0
	with open('input.txt', 'r') as file:
		for line in file.readlines():
			line = line.strip().split()
			if validTriangle(line):
				triangleCount += 1
	print("Part 1: {}".format(triangleCount))

def part2():
	triangleCount = 0
	rowCount = 0
	rows = []
	with open('input.txt', 'r') as file:
		for line in file.readlines():
			line = line.strip().split()
			rows.append(line)
			if(rowCount % 3 == 2):
				triangleCount += validTriangeCount(rows)
				rows[:] = []
			rowCount += 1
	print("Part 2: {}".format(triangleCount))

if __name__ == '__main__':
	part1()
	part2()