Add solution to problem 5
This commit is contained in:
57
Problem5_1.py
Normal file
57
Problem5_1.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
def isInRange(num: int) -> bool:
|
||||||
|
for r in ranges :
|
||||||
|
if num >= r[0] and num <= r[1] :
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
testInput = [
|
||||||
|
"3-5",
|
||||||
|
"10-14",
|
||||||
|
"16-20",
|
||||||
|
"12-18",
|
||||||
|
"",
|
||||||
|
"1",
|
||||||
|
"5",
|
||||||
|
"8",
|
||||||
|
"11",
|
||||||
|
"17",
|
||||||
|
"32"
|
||||||
|
]
|
||||||
|
|
||||||
|
def readFile() :
|
||||||
|
ary = []
|
||||||
|
with open("files/Problem5.txt", "r") as file :
|
||||||
|
for line in file :
|
||||||
|
ary.append(line.replace("\n", ""))
|
||||||
|
return ary
|
||||||
|
|
||||||
|
def parseInput(input: list[str]):
|
||||||
|
ranges = []
|
||||||
|
numbers = []
|
||||||
|
readingRanges = True
|
||||||
|
for line in input:
|
||||||
|
if line == "":
|
||||||
|
readingRanges = False
|
||||||
|
continue
|
||||||
|
if readingRanges:
|
||||||
|
parts = line.split("-")
|
||||||
|
ranges.append((int(parts[0]), int(parts[1])))
|
||||||
|
else:
|
||||||
|
numbers.append(int(line))
|
||||||
|
return ranges, numbers
|
||||||
|
|
||||||
|
|
||||||
|
#rawInput = testInput
|
||||||
|
rawInput = readFile()
|
||||||
|
ranges, numbers = parseInput(rawInput)
|
||||||
|
freshCount = 0
|
||||||
|
spoiledCount = 0
|
||||||
|
|
||||||
|
for number in numbers :
|
||||||
|
if isInRange(number):
|
||||||
|
freshCount += 1
|
||||||
|
else:
|
||||||
|
spoiledCount += 1
|
||||||
|
print(f"Fresh count: {freshCount}")
|
||||||
|
|
||||||
|
#Fresh count: 848
|
||||||
70
Problem5_2.py
Normal file
70
Problem5_2.py
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
def reduceRanges() :
|
||||||
|
sortedRanges = sorted(ranges, key=lambda r: r[0])
|
||||||
|
reducedRanges = []
|
||||||
|
currentRange = sortedRanges[0]
|
||||||
|
|
||||||
|
for r in sortedRanges[1:] :
|
||||||
|
if r[0] <= currentRange[1] + 1 :
|
||||||
|
#Overlapping or contiguous ranges, extend current range if needed
|
||||||
|
if r[1] > currentRange[1] :
|
||||||
|
currentRange = (currentRange[0], r[1])
|
||||||
|
else :
|
||||||
|
#No overlap, save current range and start a new one
|
||||||
|
reducedRanges.append(currentRange)
|
||||||
|
currentRange = r
|
||||||
|
#Add the last range
|
||||||
|
reducedRanges.append(currentRange)
|
||||||
|
return reducedRanges
|
||||||
|
|
||||||
|
testInput = [
|
||||||
|
"3-5",
|
||||||
|
"10-14",
|
||||||
|
"16-20",
|
||||||
|
"12-18",
|
||||||
|
"",
|
||||||
|
"1",
|
||||||
|
"5",
|
||||||
|
"8",
|
||||||
|
"11",
|
||||||
|
"17",
|
||||||
|
"32"
|
||||||
|
]
|
||||||
|
|
||||||
|
def readFile() :
|
||||||
|
ary = []
|
||||||
|
with open("files/Problem5.txt", "r") as file :
|
||||||
|
for line in file :
|
||||||
|
ary.append(line.replace("\n", ""))
|
||||||
|
return ary
|
||||||
|
|
||||||
|
def parseInput(input: list[str]):
|
||||||
|
ranges = []
|
||||||
|
numbers = []
|
||||||
|
readingRanges = True
|
||||||
|
for line in input:
|
||||||
|
if line == "":
|
||||||
|
readingRanges = False
|
||||||
|
continue
|
||||||
|
if readingRanges:
|
||||||
|
parts = line.split("-")
|
||||||
|
ranges.append((int(parts[0]), int(parts[1])))
|
||||||
|
else:
|
||||||
|
numbers.append(int(line))
|
||||||
|
return ranges, numbers
|
||||||
|
|
||||||
|
|
||||||
|
#rawInput = testInput
|
||||||
|
rawInput = readFile()
|
||||||
|
ranges, number = parseInput(rawInput)
|
||||||
|
reducedRanges = reduceRanges()
|
||||||
|
freshCount = 0
|
||||||
|
spoiledCount = 0
|
||||||
|
|
||||||
|
for r in reducedRanges :
|
||||||
|
diff = r[1] - r[0] + 1
|
||||||
|
#print(f"Range {r[0]}-{r[1]} adds {diff} fresh")
|
||||||
|
freshCount += diff
|
||||||
|
|
||||||
|
print(f"Fresh count: {freshCount}")
|
||||||
|
|
||||||
|
#Fresh count: 334714395325710
|
||||||
1183
files/Problem5.txt
Normal file
1183
files/Problem5.txt
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user