Abstraction
This commit is contained in:
@@ -21,18 +21,28 @@
|
||||
# Starting with the digits in your puzzle input, apply this process 40 times.
|
||||
# What is the length of the result?
|
||||
|
||||
from itertools import groupby
|
||||
|
||||
with open("files/P10.txt", "r") as f:
|
||||
number = f.read().strip().split()[0]
|
||||
number = f.read().strip()
|
||||
|
||||
|
||||
def part_1() -> None:
|
||||
num = number
|
||||
for _ in range(40):
|
||||
num = "".join(str(len(list(g))) + k for k, g in groupby(num))
|
||||
def look_and_say(iterations: int) -> None:
|
||||
value = number
|
||||
for _ in range(iterations):
|
||||
result = []
|
||||
current = value[0]
|
||||
count = 1
|
||||
for ch in value[1:]:
|
||||
if ch == current:
|
||||
count += 1
|
||||
else:
|
||||
result.append(f"{count}{current}")
|
||||
current = ch
|
||||
count = 1
|
||||
result.append(f"{count}{current}")
|
||||
value = "".join(result)
|
||||
|
||||
print(f"After 40 iterations, the length is {len(num)}")
|
||||
print(f"After {iterations} iterations, the length is {len(value)}")
|
||||
|
||||
|
||||
# --- Part Two ---
|
||||
@@ -44,14 +54,6 @@ def part_1() -> None:
|
||||
# 50 times. What is the length of the new result?
|
||||
|
||||
|
||||
def part_2() -> None:
|
||||
num = number
|
||||
for _ in range(50):
|
||||
num = "".join(str(len(list(g))) + k for k, g in groupby(num))
|
||||
|
||||
print(f"After 50 iterations, the length is {len(num)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
part_1()
|
||||
part_2()
|
||||
look_and_say(40)
|
||||
look_and_say(50)
|
||||
|
||||
Reference in New Issue
Block a user