|  | 
|  | 1 | +import sys | 
|  | 2 | + | 
|  | 3 | +data = open(sys.argv[1]).read().strip() | 
|  | 4 | +lines = data.split('\n') | 
|  | 5 | + | 
|  | 6 | +stones = [int(n) for n in lines[0].split()] | 
|  | 7 | + | 
|  | 8 | +for i in range(25): | 
|  | 9 | +    new_stones = [] | 
|  | 10 | +    for j, stone in enumerate(stones): | 
|  | 11 | +        if stone == 0: | 
|  | 12 | +            new_stones.append(1) | 
|  | 13 | +        elif len(str(stone)) % 2 == 0: | 
|  | 14 | +            left_half = int(str(stone)[:len(str(stone))//2]) | 
|  | 15 | +            right_half = int(str(stone)[len(str(stone))//2:]) | 
|  | 16 | +            new_stones.append(left_half) | 
|  | 17 | +            new_stones.append(right_half) | 
|  | 18 | +        else: | 
|  | 19 | +            new_stones.append(stone*2024) | 
|  | 20 | +    stones = new_stones | 
|  | 21 | +print(len(stones)) | 
|  | 22 | + | 
|  | 23 | +stones = [(int(n), 0) for n in lines[0].split()] | 
|  | 24 | +print(stones) | 
|  | 25 | +ans = 0 | 
|  | 26 | +blinks = 75 | 
|  | 27 | +while len(stones) > 0: | 
|  | 28 | +    stone, depth = stones.pop(0) | 
|  | 29 | +    assert (depth <= blinks) | 
|  | 30 | +    if depth == blinks: | 
|  | 31 | +        ans += 1 | 
|  | 32 | +        continue | 
|  | 33 | +    if stone == 0: | 
|  | 34 | +        if depth + 4 > blinks: | 
|  | 35 | +            stones.append((1, depth+1)) | 
|  | 36 | +        else: | 
|  | 37 | +            new_num = str(2024) | 
|  | 38 | +            for n in new_num: | 
|  | 39 | +                stones.append((int(n), depth+4)) | 
|  | 40 | +    elif len(str(stone)) % 2 == 0: | 
|  | 41 | +        curcount = 0 | 
|  | 42 | +        curlen = len(str(stone)) | 
|  | 43 | +        while curlen % 2 == 0 and curcount+depth < blinks: | 
|  | 44 | +            curlen = curlen//2 | 
|  | 45 | +            curcount += 1 | 
|  | 46 | +        new_nums_len = len(str(stone))//(2**curcount) | 
|  | 47 | +        new_nums = [str(stone)[i:i+new_nums_len] for i in range(0, len(str(stone)), new_nums_len)] | 
|  | 48 | +        for new_num in new_nums: | 
|  | 49 | +            stones.append((int(new_num), depth+curcount)) | 
|  | 50 | +    else: | 
|  | 51 | +        if (1 <= stone <= 4) and depth <= blinks-3: | 
|  | 52 | +            new_num = str(2024*stone) | 
|  | 53 | +            assert (len(new_num) == 4) | 
|  | 54 | +            for n in new_num: | 
|  | 55 | +                stones.append((int(n), depth+3)) | 
|  | 56 | +        elif stone in [5, 6, 7, 9] and depth <= blinks-5: | 
|  | 57 | +            new_num = str(stone*2024*2024) | 
|  | 58 | +            assert (len(new_num) == 8) | 
|  | 59 | +            for n in new_num: | 
|  | 60 | +                stones.append((int(n), depth+5)) | 
|  | 61 | +        elif stone == 8 and depth <= blinks-5: | 
|  | 62 | +            new_stones = [3, 2, 7, 7, 2, 6] | 
|  | 63 | +            for n in new_stones: | 
|  | 64 | +                stones.append((n, depth+5)) | 
|  | 65 | +            stones.append((8, depth+4)) | 
|  | 66 | +        else: | 
|  | 67 | +            new_num = str(stone*2024) | 
|  | 68 | +            new_depth = depth+1 | 
|  | 69 | + | 
|  | 70 | +            stones.append((stone*2024, depth+1)) | 
|  | 71 | + | 
|  | 72 | +print(ans) | 
0 commit comments