Solve 2017/06
This commit is contained in:
parent
d71bfae7fa
commit
7ff320faf7
3 changed files with 51 additions and 0 deletions
1
2017/06/input.txt
Normal file
1
2017/06/input.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0 5 10 0 11 14 13 4 11 8 8 7 1 4 12 11
|
||||||
49
2017/06/solve.py
Normal file
49
2017/06/solve.py
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
import sys
|
||||||
|
|
||||||
|
# PART 1
|
||||||
|
|
||||||
|
def load_buckets(filename):
|
||||||
|
with open(filename, "r") as f:
|
||||||
|
return tuple(map(int, f.read()[:-1].split("\t")))
|
||||||
|
|
||||||
|
def redistribute(buckets):
|
||||||
|
l = list(buckets)
|
||||||
|
i = l.index(max(l))
|
||||||
|
n = l[i]
|
||||||
|
l[i] = 0
|
||||||
|
|
||||||
|
while n > 0:
|
||||||
|
i = (i + 1) % len(l)
|
||||||
|
l[i] += 1
|
||||||
|
n -= 1
|
||||||
|
|
||||||
|
return tuple(l)
|
||||||
|
|
||||||
|
def find_repeat(buckets):
|
||||||
|
cycles = 0
|
||||||
|
states = {buckets: 0}
|
||||||
|
while True:
|
||||||
|
buckets = redistribute(buckets)
|
||||||
|
cycles += 1
|
||||||
|
if buckets in states:
|
||||||
|
return cycles, states[buckets]
|
||||||
|
else:
|
||||||
|
states[buckets] = cycles
|
||||||
|
|
||||||
|
# PART 2
|
||||||
|
|
||||||
|
def loop_cycles(buckets):
|
||||||
|
cycles, start_cycle = find_repeat(buckets)
|
||||||
|
return cycles - start_cycle
|
||||||
|
|
||||||
|
def main(filename):
|
||||||
|
print(f"Solutions for {filename}")
|
||||||
|
buckets = load_buckets(filename)
|
||||||
|
cycles, _ = find_repeat(buckets)
|
||||||
|
print(f"Part 1: {cycles}")
|
||||||
|
cycles_2 = loop_cycles(buckets)
|
||||||
|
print(f"Part 2: {cycles_2}")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
for filename in sys.argv[1:]:
|
||||||
|
main(filename)
|
||||||
1
2017/06/test_input.txt
Normal file
1
2017/06/test_input.txt
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0 2 7 0
|
||||||
Loading…
Add table
Add a link
Reference in a new issue