This commit is contained in:
		
							parent
							
								
									2c309668f3
								
							
						
					
					
						commit
						04bbeec472
					
				
					 1 changed files with 106 additions and 0 deletions
				
			
		
							
								
								
									
										106
									
								
								11/11_2.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										106
									
								
								11/11_2.py
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,106 @@
 | 
				
			||||||
 | 
					#!/usr/bin/env python
 | 
				
			||||||
 | 
					# 2021 - Advent Of Code - 11 part 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					GRID_SIZE = 10
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def parse_file(file):
 | 
				
			||||||
 | 
					    with open(file) as f:
 | 
				
			||||||
 | 
					        columns = []
 | 
				
			||||||
 | 
					        for line in f.readlines():
 | 
				
			||||||
 | 
					            columns.append([int(c) for c in line.strip()])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return columns
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def print_grid(grid):
 | 
				
			||||||
 | 
					    for y in range(GRID_SIZE):
 | 
				
			||||||
 | 
					        for x in range(GRID_SIZE):
 | 
				
			||||||
 | 
					            print(grid[y][x], end='')
 | 
				
			||||||
 | 
					        print()
 | 
				
			||||||
 | 
					    print()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def check_flash(grid, f):
 | 
				
			||||||
 | 
					    for y in range(GRID_SIZE):
 | 
				
			||||||
 | 
					        for x in range(GRID_SIZE):
 | 
				
			||||||
 | 
					            if grid[y][x] > 9:
 | 
				
			||||||
 | 
					                f = flash(grid, x, y, f)
 | 
				
			||||||
 | 
					    return f
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def flash(grid, x, y, f):
 | 
				
			||||||
 | 
					    grid[y][x] = 0
 | 
				
			||||||
 | 
					    f += 1
 | 
				
			||||||
 | 
					    if y > 0:   # upper neighbours
 | 
				
			||||||
 | 
					        if x > 0:
 | 
				
			||||||
 | 
					            if grid[y-1][x-1] != 0:  # 1
 | 
				
			||||||
 | 
					                grid[y-1][x-1] += 1
 | 
				
			||||||
 | 
					                if grid[y-1][x-1] > 9:
 | 
				
			||||||
 | 
					                    f = flash(grid, x-1, y-1, f)
 | 
				
			||||||
 | 
					        if grid[y-1][x] != 0:  # 2
 | 
				
			||||||
 | 
					            grid[y-1][x] += 1
 | 
				
			||||||
 | 
					            if grid[y-1][x] > 9:
 | 
				
			||||||
 | 
					                f = flash(grid, x, y-1, f)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if x < GRID_SIZE-1:
 | 
				
			||||||
 | 
					            if grid[y-1][x+1] != 0:  # 3
 | 
				
			||||||
 | 
					                grid[y-1][x+1] += 1
 | 
				
			||||||
 | 
					                if grid[y-1][x+1] > 9:
 | 
				
			||||||
 | 
					                    f = flash(grid, x+1, y-1, f)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if x > 0:  # middle neighbours
 | 
				
			||||||
 | 
					        if grid[y][x-1] != 0:  # 4
 | 
				
			||||||
 | 
					            grid[y][x-1] += 1
 | 
				
			||||||
 | 
					            if grid[y][x-1] > 9:
 | 
				
			||||||
 | 
					                f = flash(grid, x-1, y, f)
 | 
				
			||||||
 | 
					    if x < GRID_SIZE - 1:  # 6
 | 
				
			||||||
 | 
					        if grid[y][x+1] != 0:
 | 
				
			||||||
 | 
					            grid[y][x+1] += 1
 | 
				
			||||||
 | 
					            if grid[y][x+1] > 9:
 | 
				
			||||||
 | 
					                f = flash(grid, x+1, y, f)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if y < GRID_SIZE-1:  # down neighbours
 | 
				
			||||||
 | 
					        if x > 0:
 | 
				
			||||||
 | 
					            if grid[y+1][x-1] != 0:  # 7
 | 
				
			||||||
 | 
					                grid[y+1][x-1] += 1
 | 
				
			||||||
 | 
					                if grid[y+1][x-1] > 9:
 | 
				
			||||||
 | 
					                    f = flash(grid, x-1, y+1, f)
 | 
				
			||||||
 | 
					        if grid[y+1][x] != 0:    # 8
 | 
				
			||||||
 | 
					            grid[y+1][x] += 1
 | 
				
			||||||
 | 
					            if grid[y + 1][x] > 9:
 | 
				
			||||||
 | 
					                f = flash(grid, x, y+1, f)
 | 
				
			||||||
 | 
					        if x < GRID_SIZE-1:
 | 
				
			||||||
 | 
					            if grid[y+1][x+1] != 0:      # 9
 | 
				
			||||||
 | 
					                grid[y+1][x+1] += 1
 | 
				
			||||||
 | 
					                if grid[y+1][x+1] > 9:
 | 
				
			||||||
 | 
					                    f = flash(grid, x+1, y+1, f)
 | 
				
			||||||
 | 
					    return f
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					octopus_map = parse_file('input.txt')
 | 
				
			||||||
 | 
					# octopus_map = parse_file('input_example.txt')
 | 
				
			||||||
 | 
					print_grid(octopus_map)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					nbflash = 0
 | 
				
			||||||
 | 
					# for step in range(1000):
 | 
				
			||||||
 | 
					full_flash = False
 | 
				
			||||||
 | 
					step = 0
 | 
				
			||||||
 | 
					while not full_flash:
 | 
				
			||||||
 | 
					    full_flash = True
 | 
				
			||||||
 | 
					    # increase all energy levels
 | 
				
			||||||
 | 
					    for mapy in range(GRID_SIZE):
 | 
				
			||||||
 | 
					        for mapx in range(GRID_SIZE):
 | 
				
			||||||
 | 
					            if octopus_map[mapy][mapx] != 0:
 | 
				
			||||||
 | 
					                full_flash = False
 | 
				
			||||||
 | 
					            octopus_map[mapy][mapx] += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    nbflash = check_flash(octopus_map, nbflash)
 | 
				
			||||||
 | 
					    # check full flash
 | 
				
			||||||
 | 
					    if full_flash:
 | 
				
			||||||
 | 
					        print(f'Full flash at {step}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    step += 1
 | 
				
			||||||
 | 
					    print_grid(octopus_map)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(f'number of flash: {nbflash}')
 | 
				
			||||||
		Loading…
	
		Reference in a new issue