2023 - Day 4 part 2
This commit is contained in:
		
							parent
							
								
									70dac80d5b
								
							
						
					
					
						commit
						44d7773430
					
				
					 1 changed files with 29 additions and 0 deletions
				
			
		
							
								
								
									
										29
									
								
								2023/4/4_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								2023/4/4_2.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,29 @@
 | 
				
			||||||
 | 
					#!/usr/bin/env python
 | 
				
			||||||
 | 
					# 2023 - Advent Of Code 4 - part 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# file = 'input_example.txt'
 | 
				
			||||||
 | 
					file = 'input.txt'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					accum = 0
 | 
				
			||||||
 | 
					# pylint: disable=consider-using-with
 | 
				
			||||||
 | 
					input_lines = [line.strip('\n') for line in open(file, encoding="utf-8")]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					copies = [1] * len(input_lines)
 | 
				
			||||||
 | 
					for line in input_lines:
 | 
				
			||||||
 | 
					    card = line.split(':')
 | 
				
			||||||
 | 
					    card_num = int(card[0][4:])
 | 
				
			||||||
 | 
					    winning, got = card[1].split('|')
 | 
				
			||||||
 | 
					    winning_num = [x for x in winning.split(' ') if x != '']
 | 
				
			||||||
 | 
					    got_num = [x for x in got.split(' ') if x != '']
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    card_score = 0
 | 
				
			||||||
 | 
					    for num in got_num:
 | 
				
			||||||
 | 
					        if num in winning_num:
 | 
				
			||||||
 | 
					            card_score += 1
 | 
				
			||||||
 | 
					    print(f'{card_num}: {card_score}')
 | 
				
			||||||
 | 
					    for score in range(0, card_score):
 | 
				
			||||||
 | 
					        copies[card_num+score] += copies[card_num-1]
 | 
				
			||||||
 | 
					    # print(copies)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(f"sum: {sum(copies)}")
 | 
				
			||||||
		Loading…
	
		Reference in a new issue