You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							49 lines
						
					
					
						
							1.2 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							49 lines
						
					
					
						
							1.2 KiB
						
					
					
				| 
 | |
| import re | |
| 
 | |
| 
 | |
| # decode line into bounds and letter and password | |
| def decode_line(l): | |
|     groups = re.compile(r"(?P<lower>\d+)-(?P<higher>\d+) (?P<letter>\w): (?P<pwd>\w+)").search(l) | |
|     return groups | |
| 
 | |
| 
 | |
| # check if valid with certain number of chars | |
| def check_if_valid(lower, higher, letter, pwd): | |
|     if int(lower) <= pwd.count(letter) <= int(higher): | |
|         return True | |
|     else: | |
|         return False | |
| 
 | |
| 
 | |
| # check if valid with chars on places | |
| def check_if_valid2(lower, higher, letter, pwd): | |
|     # both = line.split(": ") | |
|     # cond = both[0].split(" ") | |
|     # times = cond[0].split("-") | |
|     # lower = int(times[0]) | |
|     # higher = int(times[1]) | |
|     # letter = cond[1] | |
|     # pwd = (both[1])[:-1] | |
|     if (pwd[int(lower) - 1] == letter) != (pwd[int(higher) - 1] == letter): | |
|         return True | |
|     else: | |
|         return False | |
| 
 | |
| 
 | |
| with open(r"C:\Users\Maya\Desktop\Uni\WS_2021\AoC\input02") as f: | |
|     lines = f.readlines() | |
| 
 | |
|     # count for challenge 1 | |
|     i = 0 | |
|     for line in lines: | |
|         if check_if_valid(*decode_line(line).groups()): | |
|             i += 1 | |
|     print(i) | |
| 
 | |
|     # count for challenge 2 | |
|     i = 0 | |
|     for line in lines: | |
|         if check_if_valid2(*decode_line(line).groups()): | |
|             i += 1 | |
|     print(i)
 | |
| 
 |