aboutsummaryrefslogtreecommitdiff
path: root/2015/15/puzzles.py
blob: 1f5fb32228f4326e698ca9583c35e93cb929d2b0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python3

import itertools
import re
from math import prod


def main() -> None:
	# START PART 1
	GROUPS = 5
	# END PART 1 START PART 2
	GROUPS = 6
	# END PART 2
 
	data: list[list[int]] = []
	with open("input", "r", encoding="utf-8") as f:
		for line in f.readlines():
			m = re.match(
				# START PART 1
				r"[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)",
				# END PART 1 START PART 2
				r"[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)[^\-0-9]+([0-9]+)",
				# END PART 2
				line,
			)
			data.append(list(map(int, [m.group(i) for i in range (1, GROUPS)])))

	total = 0
	for i in range(100):
		for j in range(100 - i):
			for k in range(100 - i - j):
				if i + j + k > 100:
					continue
				h = 100 - i - j - k
				# START PART 2
				if data[0][4] * i + data[1][4] * j + data[2][4] * k + data[3][4] * h != 500:
					continue
				# END PART 2

				parts = [
					data[0][0] * i + data[1][0] * j + data[2][0] * k + data[3][0] * h,
					data[0][1] * i + data[1][1] * j + data[2][1] * k + data[3][1] * h,
					data[0][2] * i + data[1][2] * j + data[2][2] * k + data[3][2] * h,
					data[0][3] * i + data[1][3] * j + data[2][3] * k + data[3][3] * h,
				]

				if not any(map(lambda x: x < 0, parts)):
					total = max(total, prod(parts))

	print(total)


if __name__ == "__main__":
	main()