2022 Day 4 Python

This commit is contained in:
Luke Hubmayer-Werner 2022-12-04 16:17:48 +10:30
parent 67788fadc0
commit 5c4ac1622a
2 changed files with 1030 additions and 0 deletions

30
2022/day4.py Normal file
View File

@ -0,0 +1,30 @@
import re
pattern = re.compile(r'(\d+)-(\d+),(\d+)-(\d+)')
with open('input/4', 'r') as file:
lines = file.readlines()
count = 0
for group in lines:
x1, y1, x2, y2 = (int(x) for x in pattern.findall(group)[0])
if (x2 >= x1) and (y2 <= y1): # Elf2 inside Elf1
count += 1
continue
if (x2 <= x1) and (y2 >= y1): # Elf1 inside Elf2
count += 1
continue
print(count) # Part 1
count = 0
for group in lines:
x1, y1, x2, y2 = (int(x) for x in pattern.findall(group)[0])
if (x2 >= x1) and (y2 <= y1): # Elf2 inside Elf1
count += 1
continue
if (x2 <= x1) and (y2 >= y1): # Elf1 inside Elf2
count += 1
continue
if (x1 <= x2 <= y1) or (x1 <= y2 <= y1): # Partial overlap
count += 1
continue
print(count) # Part 2

1000
2022/input/4 Normal file

File diff suppressed because it is too large Load Diff