Advent of Code 2015 day 10 part 1&2: solved
This commit is contained in:
parent
d485e4fcae
commit
c80cfc5c87
|
|
@ -0,0 +1 @@
|
||||||
|
1321131112
|
||||||
|
|
@ -0,0 +1,24 @@
|
||||||
|
--- Day 10: Elves Look, Elves Say ---
|
||||||
|
Today, the Elves are playing a game called look-and-say. They take turns making sequences by reading aloud the previous sequence and using that reading as the next sequence. For example, 211 is read as "one two, two ones", which becomes 1221 (1 2, 2 1s).
|
||||||
|
|
||||||
|
Look-and-say sequences are generated iteratively, using the previous value as input for the next step. For each step, take the previous value, and replace each run of digits (like 111) with the number of digits (3) followed by the digit itself (1).
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
1 becomes 11 (1 copy of digit 1).
|
||||||
|
11 becomes 21 (2 copies of digit 1).
|
||||||
|
21 becomes 1211 (one 2 followed by one 1).
|
||||||
|
1211 becomes 111221 (one 1, one 2, and two 1s).
|
||||||
|
111221 becomes 312211 (three 1s, two 2s, and one 1).
|
||||||
|
Starting with the digits in your puzzle input, apply this process 40 times. What is the length of the result?
|
||||||
|
|
||||||
|
Your puzzle answer was 492982.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
Neat, right? You might also enjoy hearing John Conway talking about this sequence (that's Conway of Conway's Game of Life fame).
|
||||||
|
|
||||||
|
Now, starting again with the digits in your puzzle input, apply this process 50 times. What is the length of the new result?
|
||||||
|
|
||||||
|
Your puzzle answer was 6989950.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
def next_look_and_say(previous: str) -> str:
|
||||||
|
output, last_char = "", ''
|
||||||
|
same_char = 1
|
||||||
|
for char in previous:
|
||||||
|
if last_char == '':
|
||||||
|
pass
|
||||||
|
elif char != last_char:
|
||||||
|
output += str(same_char) + last_char
|
||||||
|
same_char = 1
|
||||||
|
else:
|
||||||
|
same_char += 1
|
||||||
|
last_char = char
|
||||||
|
output += str(same_char) + last_char
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
def look_and_say(starting: str, repeats: int) -> str:
|
||||||
|
output = starting
|
||||||
|
for i in range(repeats):
|
||||||
|
output = next_look_and_say(output)
|
||||||
|
return output
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
assert next_look_and_say("1") == "11", "Error: Example 1 failed"
|
||||||
|
assert next_look_and_say("11") == "21", "Error: Example 2 failed"
|
||||||
|
assert next_look_and_say("21") == "1211", "Error: Example 3 failed"
|
||||||
|
assert next_look_and_say("1211") == "111221", "Error: Example 4 failed"
|
||||||
|
assert next_look_and_say("111221") == "312211", "Error: Example 5 failed"
|
||||||
|
print("All test passed")
|
||||||
|
|
||||||
|
puzzle_input = open("input.txt", "r").readline()
|
||||||
|
print("solution: ", len(look_and_say(puzzle_input, 40)))
|
||||||
|
|
||||||
|
print("Part2: ")
|
||||||
|
print("solution: ", len(look_and_say(puzzle_input, 50)))
|
||||||
Loading…
Reference in New Issue