[py] Port 2017_06
This commit is contained in:
parent
6712417f6a
commit
dc35256b28
4 changed files with 49 additions and 51 deletions
|
|
@ -1,49 +0,0 @@
|
||||||
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 +0,0 @@
|
||||||
0 2 7 0
|
|
||||||
|
|
@ -2,7 +2,7 @@ import sys
|
||||||
import argparse
|
import argparse
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .y2017 import d01, d02, d03, d04, d05
|
from .y2017 import d01, d02, d03, d04, d05, d06
|
||||||
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
from .y2018 import d01, d02, d03, d04, d05, d06, d07, d08, d09, d10, d11
|
||||||
from .y2020 import d10
|
from .y2020 import d10
|
||||||
from .y2021 import d14
|
from .y2021 import d14
|
||||||
|
|
@ -14,6 +14,7 @@ DAYS = {
|
||||||
"2017_03": y2017.d03.solve,
|
"2017_03": y2017.d03.solve,
|
||||||
"2017_04": y2017.d04.solve,
|
"2017_04": y2017.d04.solve,
|
||||||
"2017_05": y2017.d05.solve,
|
"2017_05": y2017.d05.solve,
|
||||||
|
"2017_06": y2017.d06.solve,
|
||||||
"2018_01": y2018.d01.solve,
|
"2018_01": y2018.d01.solve,
|
||||||
"2018_02": y2018.d02.solve,
|
"2018_02": y2018.d02.solve,
|
||||||
"2018_03": y2018.d03.solve,
|
"2018_03": y2018.d03.solve,
|
||||||
|
|
|
||||||
47
py/aoc/y2017/d06.py
Normal file
47
py/aoc/y2017/d06.py
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
# PART 1
|
||||||
|
|
||||||
|
|
||||||
|
def load_buckets(inputstr):
|
||||||
|
return tuple(map(int, inputstr[:-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 solve(inputstr):
|
||||||
|
buckets = load_buckets(inputstr)
|
||||||
|
cycles, _ = find_repeat(buckets)
|
||||||
|
print(f"Part 1: {cycles}")
|
||||||
|
cycles_2 = loop_cycles(buckets)
|
||||||
|
print(f"Part 2: {cycles_2}")
|
||||||
Loading…
Add table
Add a link
Reference in a new issue