2022 - Day 3 part 2
This commit is contained in:
		
							parent
							
								
									451fe9e6bf
								
							
						
					
					
						commit
						87a8ee1022
					
				
					 1 changed files with 32 additions and 0 deletions
				
			
		
							
								
								
									
										32
									
								
								2022/3/3_2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								2022/3/3_2.py
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,32 @@
 | 
			
		|||
#!/usr/bin/env python
 | 
			
		||||
# 2022 - Advent Of Code 3 - part2
 | 
			
		||||
 | 
			
		||||
# file = 'input_example.txt'
 | 
			
		||||
file = 'input.txt'
 | 
			
		||||
 | 
			
		||||
prio = 0
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def find_common(b1, b2, b3):
 | 
			
		||||
    common1 = set(b1).intersection(b2)
 | 
			
		||||
    common2 = set(b1).intersection(b3)
 | 
			
		||||
    common_all = common1.intersection(common2)
 | 
			
		||||
    print(f"c1: {common1} c2: {common2} -> {common_all}")
 | 
			
		||||
    return list(common_all)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def convert_score(lnum):
 | 
			
		||||
    num = ord(lnum[0])
 | 
			
		||||
    if num > 90:  # lower case
 | 
			
		||||
        return num - 96
 | 
			
		||||
    return num - 64 + 26
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
with open(file, encoding="utf-8") as f:
 | 
			
		||||
    while line := f.readline():
 | 
			
		||||
        bag1 = line
 | 
			
		||||
        bag2 = f.readline().strip()
 | 
			
		||||
        bag3 = f.readline().strip()
 | 
			
		||||
        prio += convert_score(find_common(bag1, bag2, bag3))
 | 
			
		||||
 | 
			
		||||
print(prio)
 | 
			
		||||
		Loading…
	
		Reference in a new issue