Solve day2
This commit is contained in:
1
day2/input.txt
Normal file
1
day2/input.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
5529687-5587329,50-82,374-560,83-113,226375-287485,293169-368713,2034-2634,9945560-9993116,4872472-4904227,3218-5121,1074-1357,15451-26093,483468003-483498602,51513-85385,1466-1992,7600-13034,710570-789399,407363-480868,3996614725-3996662113,3-17,5414907798-5414992881,86274-120443,828669-909588,607353-700604,4242340614-4242556443,28750-44009,935177-1004747,20-41,74678832-74818251,8484825082-8484860878,2784096938-2784156610,5477-7589,621-952,2424167145-2424278200,147085-217900,93043740-93241586
|
||||||
26
day2/part1.py
Normal file
26
day2/part1.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
FILE = "input.txt"
|
||||||
|
|
||||||
|
def main():
|
||||||
|
line = open(FILE).readlines()[0].strip()
|
||||||
|
|
||||||
|
ranges = [
|
||||||
|
[int(n) for n in l.split("-")]
|
||||||
|
for l in line.split(",")
|
||||||
|
]
|
||||||
|
|
||||||
|
invalid_ids = [
|
||||||
|
id
|
||||||
|
for (start, end) in ranges
|
||||||
|
for id in range(start, end + 1)
|
||||||
|
if invalid(str(id))
|
||||||
|
]
|
||||||
|
|
||||||
|
return sum(invalid_ids)
|
||||||
|
|
||||||
|
def invalid(id):
|
||||||
|
if len(id) % 2 == 1: return False
|
||||||
|
a, b = id[:(len(id)//2)], id[(len(id)//2):]
|
||||||
|
return a == b
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(main())
|
||||||
27
day2/part2.py
Normal file
27
day2/part2.py
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
FILE = "input.txt"
|
||||||
|
|
||||||
|
def main():
|
||||||
|
line = open(FILE).readlines()[0].strip()
|
||||||
|
|
||||||
|
ranges = [
|
||||||
|
[int(n) for n in l.split("-")]
|
||||||
|
for l in line.split(",")
|
||||||
|
]
|
||||||
|
|
||||||
|
invalid_ids = [
|
||||||
|
id
|
||||||
|
for (start, end) in ranges
|
||||||
|
for id in range(start, end + 1)
|
||||||
|
if invalid(str(id))
|
||||||
|
]
|
||||||
|
|
||||||
|
return sum(invalid_ids)
|
||||||
|
|
||||||
|
def invalid(id: str) -> bool:
|
||||||
|
for n in range(1, len(id)//2 + 1):
|
||||||
|
sub = { id[i:i+n] for i in range(0, len(id), n) }
|
||||||
|
if len(sub) == 1: return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
print(main())
|
||||||
1
day2/test.txt
Normal file
1
day2/test.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124
|
||||||
Reference in New Issue
Block a user