Solve day2
This commit is contained in:
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())
|
||||
Reference in New Issue
Block a user