aoc

Advent of Code Solutions
git clone git://git.alexkarle.com.com/aoc
Log | Files | Refs | README | LICENSE

naive.py (778B) [raw]


      1 #!/usr/bin/env python3
      2 import sys
      3 import os
      4 
      5 def parse():
      6     return [int(i) for i in next(sys.stdin).split()]
      7 
      8 def debug(msg):
      9     if os.getenv("DEBUG"):
     10         print(msg)
     11 
     12 def cycle(stones):
     13     news = []
     14     for s in stones:
     15         if s == 0:
     16             news.append(1)
     17         else:
     18             ss = str(s)
     19             mid = len(ss) // 2
     20             l, r = ss[0:mid], ss[mid:]
     21             if len(l) == len(r):
     22                 news.append(int(l))
     23                 news.append(int(r))
     24             else:
     25                 news.append(s * 2024)
     26     return news
     27 
     28 if __name__ == '__main__':
     29     stones = parse()
     30     debug(stones)
     31     for i in range(int(os.getenv("N", 25))):
     32         print(i, len(stones))
     33         stones = cycle(stones)
     34         debug(stones)
     35     print(len(stones))