Solve day5
This commit is contained in:
1172
day5/input.txt
Normal file
1172
day5/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
27
day5/part1.py
Normal file
27
day5/part1.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
FILE = "input.txt"
|
||||||
|
|
||||||
|
def main():
|
||||||
|
freshRanges = []
|
||||||
|
indgredients = []
|
||||||
|
complete = False
|
||||||
|
for line in open(FILE):
|
||||||
|
if line == "\n":
|
||||||
|
complete = True
|
||||||
|
continue
|
||||||
|
if not complete:
|
||||||
|
freshRanges.append(line.strip().split("-"))
|
||||||
|
else:
|
||||||
|
indgredients.append(line.strip())
|
||||||
|
freshRanges = [
|
||||||
|
range(int(start), int(stop)+1) for (start, stop) in freshRanges
|
||||||
|
]
|
||||||
|
indgredients = [ int(x) for x in indgredients ]
|
||||||
|
return len([
|
||||||
|
indgredient
|
||||||
|
for indgredient in indgredients
|
||||||
|
if any(indgredient in r for r in freshRanges)
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(main())
|
||||||
22
day5/part2.py
Normal file
22
day5/part2.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
FILE = "input.txt"
|
||||||
|
|
||||||
|
def main():
|
||||||
|
ranges = []
|
||||||
|
for line in open(FILE):
|
||||||
|
if line == "\n": break
|
||||||
|
ranges.append(line.strip().split("-"))
|
||||||
|
ranges = sorted([
|
||||||
|
(int(start), int(stop)+1) for (start, stop) in ranges
|
||||||
|
], key=lambda x: x[0])
|
||||||
|
condensed = [ranges[0]]
|
||||||
|
for r in ranges:
|
||||||
|
end = condensed[-1][1]
|
||||||
|
if r[0] <= end < r[1]:
|
||||||
|
condensed[-1] = (condensed[-1][0], r[1])
|
||||||
|
elif end < r[0]:
|
||||||
|
condensed.append(r)
|
||||||
|
return sum(len(range(r[0], r[1])) for r in condensed)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(main())
|
||||||
11
day5/test.txt
Normal file
11
day5/test.txt
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
3-5
|
||||||
|
10-14
|
||||||
|
16-20
|
||||||
|
12-18
|
||||||
|
|
||||||
|
1
|
||||||
|
5
|
||||||
|
8
|
||||||
|
11
|
||||||
|
17
|
||||||
|
32
|
||||||
Reference in New Issue
Block a user