Day 6 part 2
This commit is contained in:
		
							parent
							
								
									a5626235d3
								
							
						
					
					
						commit
						7db14d6368
					
				
					 1 changed files with 43 additions and 0 deletions
				
			
		
							
								
								
									
										43
									
								
								6/6_2.py
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										43
									
								
								6/6_2.py
									
									
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,43 @@
 | 
				
			||||||
 | 
					#!/usr/bin/env python
 | 
				
			||||||
 | 
					# 2021 - Advent Of Code - 6 part 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def parse_file(file):
 | 
				
			||||||
 | 
					    with open(file) as f:
 | 
				
			||||||
 | 
					        line = f.readline()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    list_state = [int(x) for x in line.split(',')]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    state = dict(zip(range(0, 9), [0]*9))
 | 
				
			||||||
 | 
					    for c in range(0, 9):
 | 
				
			||||||
 | 
					        state[c] = list_state.count(c)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return state
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def iterate(state):
 | 
				
			||||||
 | 
					    new_fish = state[0]
 | 
				
			||||||
 | 
					    state[0] = state[1]
 | 
				
			||||||
 | 
					    state[1] = state[2]
 | 
				
			||||||
 | 
					    state[2] = state[3]
 | 
				
			||||||
 | 
					    state[3] = state[4]
 | 
				
			||||||
 | 
					    state[4] = state[5]
 | 
				
			||||||
 | 
					    state[5] = state[6]
 | 
				
			||||||
 | 
					    state[6] = state[7] + new_fish
 | 
				
			||||||
 | 
					    state[7] = state[8]
 | 
				
			||||||
 | 
					    state[8] = new_fish
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# init_state = parse_file('input_example.txt')
 | 
				
			||||||
 | 
					init_state = parse_file('input.txt')
 | 
				
			||||||
 | 
					print(f'Initial state: {init_state}')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					DAYS = 256
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for d in range(DAYS):
 | 
				
			||||||
 | 
					    iterate(init_state)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					lanternfish = 0
 | 
				
			||||||
 | 
					for k, v in init_state.items():
 | 
				
			||||||
 | 
					    lanternfish += v
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(f'There\'s {lanternfish} lanternfish after {DAYS} days')
 | 
				
			||||||
		Loading…
	
		Reference in a new issue