diff --git a/04/04-2.py b/04/04-2.py new file mode 100644 index 0000000..90e79a4 --- /dev/null +++ b/04/04-2.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +from copy import deepcopy + +# file = "./ex.txt" +file = "./input.txt" + +def less_than_four(x,y,field): + count = 0 + for posx,posy in [(x-1,y-1),(x,y-1),(x+1,y-1),(x-1,y),(x+1,y),(x-1,y+1),(x,y+1),(x+1,y+1)]: + try: + if posx >= 0 and posy >= 0 and field[posx][posy] == "@": + count += 1 + except IndexError: + pass + if count < 4: + return True + else: + return False + +if __name__ == "__main__": + solution = 0 + game_field = [] + input_file = open(file, "r") + for line in input_file: + line = line.strip() + if line == "": + continue + tmp = [] + for b in line: + tmp.append(b) + game_field.append(tmp) + input_file.close() + + while True: + rolls_removed = 0 + game_field_copy = deepcopy(game_field) + for i in range(len(game_field)): + for j in range(len(game_field[0])): + if game_field[i][j] == "@" and less_than_four(i,j,game_field): + solution += 1 + rolls_removed += 1 + game_field_copy[i][j] = "x" + if rolls_removed > 0: + game_field = deepcopy(game_field_copy) + else: + break + print(f"Solution: {solution}")