From 7fb03d975972887cdc42259bd849e9b808f72b7a Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 2 Dec 2021 09:58:43 +0100 Subject: Merge both solutions into one file --- 2015/18/.gitignore | 1 + 2015/18/Makefile | 8 ++++++++ 2015/18/puzzle-1.py | 50 --------------------------------------------- 2015/18/puzzle-2.py | 53 ------------------------------------------------ 2015/18/puzzles.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 103 deletions(-) create mode 100644 2015/18/.gitignore create mode 100644 2015/18/Makefile delete mode 100755 2015/18/puzzle-1.py delete mode 100755 2015/18/puzzle-2.py create mode 100644 2015/18/puzzles.py (limited to '2015/18') diff --git a/2015/18/.gitignore b/2015/18/.gitignore new file mode 100644 index 0000000..ffc46fe --- /dev/null +++ b/2015/18/.gitignore @@ -0,0 +1 @@ +puzzle-[12].py diff --git a/2015/18/Makefile b/2015/18/Makefile new file mode 100644 index 0000000..9f0c9fb --- /dev/null +++ b/2015/18/Makefile @@ -0,0 +1,8 @@ +all: + sed '/# START PART 2/,/# END PART 2/d' puzzles.py >puzzle-1.py + sed '/# START PART 1/,/# END PART 1/d' puzzles.py >puzzle-2.py + chmod +x puzzle-[12].py + +.PHONY: clean +clean: + rm -f puzzle-[12].py diff --git a/2015/18/puzzle-1.py b/2015/18/puzzle-1.py deleted file mode 100755 index 3fe9c8a..0000000 --- a/2015/18/puzzle-1.py +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/env python3 - - -def neighbours(data: list[list[str]], x: int, y: int) -> int: - acc = 0 - - for x1, y1 in [ - (x - 1, y - 1), - (x - 1, y), - (x - 1, y + 1), - (x, y - 1), - (x, y + 1), - (x + 1, y - 1), - (x + 1, y), - (x + 1, y + 1), - ]: - try: - if x1 >= 0 and y1 >= 0 and data[x1][y1] == "#": - acc += 1 - except IndexError: - pass - - return acc - - -def simulate(data: list[list[str]]) -> list[list[str]]: - ndata = [["." for i in range(100)] for j in range(100)] - - for i in range(100): - for j in range(100): - if (data[i][j] == "#" and neighbours(data, i, j) in [2, 3]) or ( - data[i][j] == "." and neighbours(data, i, j) == 3 - ): - ndata[i][j] = "#" - - return ndata - - -def main() -> None: - with open("input", "r", encoding="utf-8") as f: - data = [list(l.strip()) for l in f.readlines()] - - for i in range(100): - data = simulate(data) - - print(sum(data[i].count("#") for i in range(100))) - - -if __name__ == "__main__": - main() diff --git a/2015/18/puzzle-2.py b/2015/18/puzzle-2.py deleted file mode 100755 index b57edb9..0000000 --- a/2015/18/puzzle-2.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python3 - - -def neighbours(data: list[list[str]], x: int, y: int) -> int: - acc = 0 - - for x1, y1 in [ - (x - 1, y - 1), - (x - 1, y), - (x - 1, y + 1), - (x, y - 1), - (x, y + 1), - (x + 1, y - 1), - (x + 1, y), - (x + 1, y + 1), - ]: - try: - if x1 >= 0 and y1 >= 0 and data[x1][y1] == "#": - acc += 1 - except IndexError: - pass - - return acc - - -def simulate(data: list[list[str]]) -> list[list[str]]: - ndata = [["." for i in range(100)] for j in range(100)] - - for i in range(100): - for j in range(100): - if ( - ((i, j) in [(0, 0), (0, 99), (99, 0), (99, 99)]) - or (data[i][j] == "#" and neighbours(data, i, j) in [2, 3]) - or (data[i][j] == "." and neighbours(data, i, j) == 3) - ): - ndata[i][j] = "#" - - return ndata - - -def main() -> None: - with open("input", "r", encoding="utf-8") as f: - data = [list(l.strip()) for l in f.readlines()] - data[0][0], data[0][99], data[99][0], data[99][99] = "#", "#", "#", "#" - - for i in range(100): - data = simulate(data) - - print(sum(data[i].count("#") for i in range(100))) - - -if __name__ == "__main__": - main() diff --git a/2015/18/puzzles.py b/2015/18/puzzles.py new file mode 100644 index 0000000..5fbe572 --- /dev/null +++ b/2015/18/puzzles.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + + +def neighbours(data: list[list[str]], x: int, y: int) -> int: + acc = 0 + + for x1, y1 in [ + (x - 1, y - 1), + (x - 1, y), + (x - 1, y + 1), + (x, y - 1), + (x, y + 1), + (x + 1, y - 1), + (x + 1, y), + (x + 1, y + 1), + ]: + try: + if x1 >= 0 and y1 >= 0 and data[x1][y1] == "#": + acc += 1 + except IndexError: + pass + + return acc + + +def simulate(data: list[list[str]]) -> list[list[str]]: + # START PART 1 + cond = lambda x, y: (data[x][y] == "#" and neighbours(data, x, y) in [2, 3]) or ( + data[x][y] == "." and neighbours(data, x, y) == 3 + ) + # END PART 1 + # START PART 2 + cond = lambda x, y: ( + ((i, j) in [(0, 0), (0, 99), (99, 0), (99, 99)]) + or (data[x][y] == "#" and neighbours(data, x, y) in [2, 3]) + or (data[x][y] == "." and neighbours(data, x, y) == 3) + ) + # END PART 2 + + return [["#" if cond(i, j) else "." for j in range(100)] for i in range(100)] + + +def main() -> None: + with open("input", "r", encoding="utf-8") as f: + data = [list(l.strip()) for l in f.readlines()] + + # START PART 2 + data[0][0], data[0][99], data[99][0], data[99][99] = "#", "#", "#", "#" + # END PART 2 + + for i in range(100): + data = simulate(data) + + print(sum(data[i].count("#") for i in range(100))) + + +if __name__ == "__main__": + main() -- cgit v1.2.3