#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 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 game_field.append(line) input_file.close() 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 print(f"Solution: {solution}")