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
|
||||
Reference in New Issue
Block a user