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