days 3 and some of 4
This commit is contained in:
		
							
								
								
									
										33
									
								
								2018/03.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								2018/03.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					from collections import defaultdict
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def iter_claim(c):
 | 
				
			||||||
 | 
					    ident, left, top, width, height = c
 | 
				
			||||||
 | 
					    for v in range(height):
 | 
				
			||||||
 | 
					        for h in range(width):
 | 
				
			||||||
 | 
					            yield left + h, top + v
 | 
				
			||||||
 | 
					            
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					with open('data/03.txt') as f:
 | 
				
			||||||
 | 
					    data = [r for r in f]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					regex = re.compile(r'#(\d+) @ (\d+),(\d+): (\d+)x(\d+)')
 | 
				
			||||||
 | 
					pixels = defaultdict(set)
 | 
				
			||||||
 | 
					claims = []
 | 
				
			||||||
 | 
					for line in data:
 | 
				
			||||||
 | 
					    claim = tuple(int(x) for x in regex.match(line).groups())
 | 
				
			||||||
 | 
					    claims.append(claim)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for coords in iter_claim(claim):
 | 
				
			||||||
 | 
					        pixels[coords].add(claim[0])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(f'Part 1: {sum(1 for i in pixels.values() if len(i) > 1)}\n')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for claim in claims:
 | 
				
			||||||
 | 
					    if all(len(pixels[coords]) == 1 for coords in iter_claim(claim)):
 | 
				
			||||||
 | 
					        print(f'Part 2: {claim[0]}\n')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
							
								
								
									
										26
									
								
								2018/04.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								2018/04.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
				
			|||||||
 | 
					from collections import defaultdict
 | 
				
			||||||
 | 
					from datetime import datetime
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					with open('data/04.txt') as f:
 | 
				
			||||||
 | 
					    regex = re.compile(r'\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2})\](?: Guard #(\d+)){0,1} (.+ .+)')
 | 
				
			||||||
 | 
					    events = []
 | 
				
			||||||
 | 
					    for line in f:
 | 
				
			||||||
 | 
					        dt, guard_id, event_type = regex.match(line).groups()
 | 
				
			||||||
 | 
					        dt = datetime.strptime(dt, '%Y-%m-%d %H:%M')
 | 
				
			||||||
 | 
					        guard_id = 0 if guard_id is None else int(guard_id)
 | 
				
			||||||
 | 
					        events.append([dt, guard_id, event_type])
 | 
				
			||||||
 | 
					    events.sort(key=lambda e: e[0])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					by_minute = defaultdict(lambda: defaultdict(list))
 | 
				
			||||||
 | 
					by_guard = defaultdict(lambda: defaultdict(list))
 | 
				
			||||||
 | 
					total_time_asleep = defaultdict(int)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					current_guard_id = None
 | 
				
			||||||
 | 
					spans = []
 | 
				
			||||||
 | 
					for event in events:
 | 
				
			||||||
 | 
					    dt, guard_id, event_type = event
 | 
				
			||||||
 | 
					    if event_type = 'begins shift':
 | 
				
			||||||
 | 
					        current_guard_id = guard_id
 | 
				
			||||||
							
								
								
									
										1247
									
								
								2018/data/03.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1247
									
								
								2018/data/03.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										1041
									
								
								2018/data/04.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1041
									
								
								2018/data/04.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user