diff options
author | Thomas Voss <thomasvoss@live.com> | 2021-12-02 09:22:59 +0100 |
---|---|---|
committer | Thomas Voss <thomasvoss@live.com> | 2021-12-02 09:22:59 +0100 |
commit | 5f1cec7004dc0e4ef58662358d48635462362422 (patch) | |
tree | f3630a8ede0f83457151901bfbb7aaed7ad64133 /2015 | |
parent | 74e99d618a660ad452c31100cc3106879d04ac42 (diff) |
Compress solutions into 1 file
Diffstat (limited to '2015')
-rw-r--r-- | 2015/15/.gitignore | 1 | ||||
-rw-r--r-- | 2015/15/Makefile | 8 | ||||
-rwxr-xr-x | 2015/15/puzzle-1.py | 39 | ||||
-rw-r--r--[-rwxr-xr-x] | 2015/15/puzzles.py (renamed from 2015/15/puzzle-2.py) | 26 |
4 files changed, 26 insertions, 48 deletions
diff --git a/2015/15/.gitignore b/2015/15/.gitignore new file mode 100644 index 0000000..ffc46fe --- /dev/null +++ b/2015/15/.gitignore @@ -0,0 +1 @@ +puzzle-[12].py diff --git a/2015/15/Makefile b/2015/15/Makefile new file mode 100644 index 0000000..8ada60c --- /dev/null +++ b/2015/15/Makefile @@ -0,0 +1,8 @@ +all: + m4 -D GROUPS=5 puzzles.py | sed '/# START PART 2/,/# END PART 2/d' >puzzle-1.py + m4 -D GROUPS=6 puzzles.py | sed '/# START PART 1/,/# END PART 1/d' >puzzle-2.py + chmod +x puzzle-[12].py + +.PHONY: clean +clean: + rm -f puzzle-[12].py diff --git a/2015/15/puzzle-1.py b/2015/15/puzzle-1.py deleted file mode 100755 index 806737c..0000000 --- a/2015/15/puzzle-1.py +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env python3 - -from math import prod -import itertools -import re - - -def main() -> None: - data: list[list[int]] = [] - with open("input", "r", encoding="utf-8") as f: - for line in f.readlines(): - m = re.match( - r"[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)[^\-0-9]+([\-0-9]+)", - line, - ) - data.append([int(m.group(1)), int(m.group(2)), int(m.group(3)), int(m.group(4))]) - - total: int = 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 - - parts: list[int] = [] - parts.append(data[0][0] * i + data[1][0] * j + data[2][0] * k + data[3][0] * h) - parts.append(data[0][1] * i + data[1][1] * j + data[2][1] * k + data[3][1] * h) - parts.append(data[0][2] * i + data[1][2] * j + data[2][2] * k + data[3][2] * h) - parts.append(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() diff --git a/2015/15/puzzle-2.py b/2015/15/puzzles.py index ec03a82..506e11f 100755..100644 --- a/2015/15/puzzle-2.py +++ b/2015/15/puzzles.py @@ -1,8 +1,8 @@ #!/usr/bin/env python3 -from math import prod import itertools import re +from math import prod def main() -> None: @@ -10,26 +10,34 @@ def main() -> None: 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, 6)]))) + data.append(list(map(int, [m.group(i) for i in range (1, GROUPS)]))) - total: int = 0 + 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 - - parts: list[int] = [] - parts.append(data[0][0] * i + data[1][0] * j + data[2][0] * k + data[3][0] * h) - parts.append(data[0][1] * i + data[1][1] * j + data[2][1] * k + data[3][1] * h) - parts.append(data[0][2] * i + data[1][2] * j + data[2][2] * k + data[3][2] * h) - parts.append(data[0][3] * i + data[1][3] * j + data[2][3] * k + data[3][3] * h) + # 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)) |