day4 done
This commit is contained in:
parent
b2190a7776
commit
3ebcb19749
File diff suppressed because it is too large
Load Diff
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue