day4 done

This commit is contained in:
Thomas Hodnemyr 2022-12-04 13:57:58 +01:00
parent b2190a7776
commit 3ebcb19749
3 changed files with 1065 additions and 0 deletions

1000
day4/input.txt Normal file

File diff suppressed because it is too large Load Diff

31
day4/main1.py Normal file
View File

@ -0,0 +1,31 @@
with open("day4\input.txt") as f:
data = f.readlines()
'''
Some of the pairs have noticed that one of their assignments fully contains the other.
For example, 2-8 fully contains 3-7, and 6-6 is fully contained by 4-6.
In pairs where one assignment fully contains the other, one Elf in the pair would be
exclusively cleaning sections their partner will already be cleaning,
so these seem like the most in need of reconsideration.
In how many assignment pairs does one range fully contain the other?
'''
pair_sum = 0
line:str
for line in data:
elf1 = [int(i) for i in line.split(',')[0].split('-')]
elf2 = [int(i) for i in line.split(',')[1].split('-')]
#Find the smallest
if elf1[1] - elf1[0] > elf2[1] - elf2[0]:
if elf2[0] >= elf1[0] and elf2[1] <= elf1[1]:
pair_sum += 1
else:
if elf1[0] >= elf2[0] and elf1[1] <= elf2[1]:
pair_sum += 1
print(pair_sum) #550

34
day4/main2.py Normal file
View File

@ -0,0 +1,34 @@
with open("day4\input.txt") as f:
data = f.readlines()
'''
It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of pairs that overlap at all.
In how many assignment pairs do the ranges overlap?
'''
pair_sum = 0
line:str
for line in data:
elf1 = [int(i) for i in line.split(',')[0].split('-')]
elf2 = [int(i) for i in line.split(',')[1].split('-')]
#Example 77-88,14-78
# elf1 = 77-88
# elf2 = 14-78
#Find the smallest
if elf1[1] - elf1[0] > elf2[1] - elf2[0]:
if elf2[0] >= elf1[0] and elf2[1] <= elf1[1]:
pair_sum += 1
continue
else:
if elf1[0] >= elf2[0] and elf1[1] <= elf2[1]:
pair_sum += 1
continue
#Look at elf 2's numbers and see if they are in elf 1's range
if elf1[0] <= elf2[0] <= elf1[1] or elf1[0] <= elf2[1] <= elf1[1]:
pair_sum +=1
print(pair_sum) #931