Day 4
This commit is contained in:
		
							
								
								
									
										37
									
								
								day4/day4.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								day4/day4.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | |||||||
|  | with open("./day4.txt", "r") as f: | ||||||
|  |     file = f.read() | ||||||
|  |  | ||||||
|  | pairs = file.split("\n") | ||||||
|  |  | ||||||
|  | contain = 0 | ||||||
|  |  | ||||||
|  | for pair in pairs: | ||||||
|  |     if pair == "": | ||||||
|  |         continue | ||||||
|  |     p = pair.split(",") | ||||||
|  |     first = p[0].split("-") | ||||||
|  |     second = p[1].split("-") | ||||||
|  |     if int(first[0]) <= int(second[0]) and int(first[1]) >= int(second[1]): | ||||||
|  |         contain += 1 | ||||||
|  |     elif int(first[0]) >= int(second[0]) and int(first[1]) <= int(second[1]): | ||||||
|  |         contain += 1 | ||||||
|  |  | ||||||
|  |  | ||||||
|  | overlap = 0 | ||||||
|  |  | ||||||
|  | for pair in pairs: | ||||||
|  |     if pair == "": | ||||||
|  |         continue | ||||||
|  |     p = pair.split(",") | ||||||
|  |     first = p[0].split("-") | ||||||
|  |     second = p[1].split("-") | ||||||
|  |     if int(first[0]) in range(int(second[0]), int(second[1])+1): | ||||||
|  |         overlap += 1 | ||||||
|  |     elif int(first[1]) in range(int(second[0]), int(second[1])+1): | ||||||
|  |         overlap += 1 | ||||||
|  |     elif int(second[0]) in range(int(first[0]), int(first[1])+1): | ||||||
|  |         overlap += 1 | ||||||
|  |     elif int(second[1]) in range(int(first[0]), int(first[1])+1): | ||||||
|  |         overlap += 1 | ||||||
|  |  | ||||||
|  | print(overlap) | ||||||
							
								
								
									
										1000
									
								
								day4/day4.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1000
									
								
								day4/day4.txt
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
		Reference in New Issue
	
	Block a user