day 2 part 1

This commit is contained in:
magical 2025-12-02 05:54:52 +00:00
parent 2e6bf3a11d
commit 80b0dea13b
3 changed files with 42 additions and 0 deletions

1
day02/input Normal file
View File

@ -0,0 +1 @@
2558912-2663749,1-19,72-85,82984-100358,86-113,193276-237687,51-69,779543-880789,13004-15184,2768-3285,4002-4783,7702278-7841488,7025-8936,5858546565-5858614010,5117615-5149981,4919-5802,411-466,126397-148071,726807-764287,7454079517-7454227234,48548-61680,67606500-67729214,9096-10574,9999972289-10000034826,431250-455032,907442-983179,528410-680303,99990245-100008960,266408-302255,146086945-146212652,9231222-9271517,32295166-32343823,32138-36484,4747426142-4747537765,525-652,333117-414840,13413537-13521859,1626-1972,49829276-50002273,69302-80371,8764571787-8764598967,5552410836-5552545325,660-782,859-1056

1
day02/sample Normal file
View 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

40
day02/sol.py Normal file
View File

@ -0,0 +1,40 @@
def parse(s):
ret = []
for pair in s.split(','):
x,y = pair.split('-')
ret.append((int(x),int(y)))
return ret
def solve(input):
with open(input) as f:
ranges = parse(f.read())
t = 0
for lo,hi in ranges:
invalid = list(findinvalid(lo,hi))
if len(invalid) == 0:
print(lo,hi,invalid)
t += sum(invalid)
print(t)
def findinvalid(lo, hi):
assert len(str(hi)) - len(str(lo)) <= 1
assert lo <= hi
j = len(str(lo)) // 2
if j < 1:
# (1,19) => [11]
j = 1
for B in [10**j, 10**(j+1)]:
for i in range(lo//B, hi//B+1):
if i < B//10:
continue
if i >= B:
break
id = i*B + i
if lo <= id <= hi:
assert(str(i)+str(i) == str(id)), (i,id)
#print(lo, hi, i,B,id)
yield id
#solve("sample")
solve("input")