Sparrow1029

Advent of Code 2015 - Day 6 Solution - Python 3

Dec 6th, 2017
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.81 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. mem_banks = [14, 0, 15, 12, 11, 11, 3, 5, 1, 6, 8, 4, 9, 1, 8, 4]
  5.  
  6.  
  7. def increaseblk(idx):
  8.     global mem_banks
  9.     mem_banks[idx] += 1
  10.  
  11.  
  12. def join_ints(a_list):
  13.     return int(''.join(map(str, a_list)))
  14.  
  15.  
  16. previous_states = [join_ints(mem_banks)]
  17. cycles = 0
  18.  
  19. while True:
  20.     max_block = max(mem_banks)
  21.     tgt = mem_banks.index(max_block)
  22.     iter_ = list(range(tgt+1, 16)) + list(range(0, tgt))
  23.     while mem_banks[tgt] > 0:
  24.         idx = iter_.pop(0)
  25.         increaseblk(idx)
  26.         mem_banks[tgt] -= 1
  27.     cycles += 1
  28.     if join_ints(mem_banks) in previous_states:
  29.         break
  30.     else:
  31.         previous_states.append(join_ints(mem_banks))
  32.  
  33. # PART ONE
  34. print(cycles)
  35.  
  36. # PART TWO
  37. print(cycles - previous_states.index(join_ints(mem_banks)))
Advertisement
Add Comment
Please, Sign In to add comment