Updated
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
## \-\-- Day 16: Permutation Promenade \-\--
|
||||
|
||||
You come upon a very unusual sight; a group of programs here appear to
|
||||
be [dancing](https://www.youtube.com/watch?v=lyZQPjUT5B4&t=53).
|
||||
|
||||
There are sixteen programs in total, named `a` through `p`. They start
|
||||
by standing in a line: `a` stands
|
||||
in position `0`, `b` stands in position `1`, and so on until `p`, which
|
||||
stands in position `15`.
|
||||
|
||||
The programs\' *dance* consists of a sequence of *dance moves*:
|
||||
|
||||
- *Spin*, written `sX`, makes `X` programs move from the end to the
|
||||
front, but maintain their order otherwise. (For example, `s3` on
|
||||
`abcde` produces `cdeab`).
|
||||
- *Exchange*, written `xA/B`, makes the programs at positions `A` and
|
||||
`B` swap places.
|
||||
- *Partner*, written `pA/B`, makes the programs named `A` and `B` swap
|
||||
places.
|
||||
|
||||
For example, with only five programs standing in a line (`abcde`), they
|
||||
could do the following dance:
|
||||
|
||||
- `s1`, a spin of size `1`: `eabcd`.
|
||||
- `x3/4`, swapping the last two programs: `eabdc`.
|
||||
- `pe/b`, swapping programs `e` and `b`: `baedc`.
|
||||
|
||||
After finishing their dance, the programs end up in order `baedc`.
|
||||
|
||||
You watch the dance for a while and record their dance moves (your
|
||||
puzzle input). *In what order are the programs standing* after their
|
||||
dance?
|
||||
|
||||
Your puzzle answer was `ehdpincaogkblmfj`.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: \*
|
||||
|
||||
## \-\-- Part Two \-\-- {#part2}
|
||||
|
||||
Now that you\'re starting to get a feel for the dance moves, you turn
|
||||
your attention to *the dance as a whole*.
|
||||
|
||||
Keeping the positions they ended up in from their previous dance, the
|
||||
programs perform it again and again: including the first dance, a total
|
||||
of *one billion* (`1000000000`) times.
|
||||
|
||||
In the example above, their second dance would *begin* with the order
|
||||
`baedc`, and use the same dance moves:
|
||||
|
||||
- `s1`, a spin of size `1`: `cbaed`.
|
||||
- `x3/4`, swapping the last two programs: `cbade`.
|
||||
- `pe/b`, swapping programs `e` and `b`: `ceadb`.
|
||||
|
||||
*In what order are the programs standing* after their billion dances?
|
||||
|
||||
Answer:
|
||||
|
||||
Although it hasn\'t changed, you can still [get your puzzle
|
||||
input](16/input).
|
||||
@@ -0,0 +1,132 @@
|
||||
#!/bin/python3
|
||||
import sys,re,collections
|
||||
|
||||
from pprint import pprint
|
||||
sys.path.insert(0, '../../')
|
||||
from fred import list2int
|
||||
|
||||
input_f = 'input'
|
||||
|
||||
part = 2
|
||||
#########################################
|
||||
# #
|
||||
# Part 1 #
|
||||
# #
|
||||
#########################################
|
||||
|
||||
inst = []
|
||||
|
||||
programs = 'abcdefghijklmnop'
|
||||
#programs = 'abcde'
|
||||
|
||||
def parse_input(input_str):
|
||||
pattern = r"^(s(\d+)|x(\d+)/(\d+)|p([a-zA-Z])/([a-zA-Z]))$"
|
||||
|
||||
match = re.match(pattern, input_str)
|
||||
if match:
|
||||
if match.group(2):
|
||||
return ('s', int(match.group(2)))
|
||||
elif match.group(3) and match.group(4):
|
||||
return ('x', int(match.group(3)), int(match.group(4)))
|
||||
elif match.group(5) and match.group(6):
|
||||
return ('p', match.group(5), match.group(6))
|
||||
return None
|
||||
|
||||
def swap_pos(d, index1, index2):
|
||||
if not (0 <= index1 < len(d)) or not (0 <= index2 < len(d)):
|
||||
raise IndexError("Index out of range")
|
||||
d[index1], d[index2] = d[index2], d[index1]
|
||||
|
||||
def swap_items(d, item1, item2):
|
||||
try:
|
||||
index1 = d.index(item1)
|
||||
index2 = d.index(item2)
|
||||
|
||||
d[index1], d[index2] = d[index2], d[index1]
|
||||
except ValueError as e:
|
||||
raise ValueError(f"One or both items not found in deque: {e}")
|
||||
|
||||
if part == 1:
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
instructions = line.rstrip().split(',')
|
||||
|
||||
programs = collections.deque(list(programs))
|
||||
|
||||
#print(programs)
|
||||
#print(instructions)
|
||||
print(len(instructions))
|
||||
|
||||
for idx,i in enumerate(instructions):
|
||||
inst = parse_input(i)
|
||||
print(idx,end=' ')
|
||||
#print(i)
|
||||
#rint(inst)
|
||||
if inst[0] == 's':
|
||||
#print('Spin', i)
|
||||
programs.rotate(inst[1])
|
||||
|
||||
elif inst[0] == 'x':
|
||||
#print('Exchange',i)
|
||||
swap_pos(programs,inst[1],inst[2])
|
||||
|
||||
elif inst[0] == 'p':
|
||||
#print('Partner',i)
|
||||
swap_items(programs,inst[1],inst[2])
|
||||
else:
|
||||
print(inst)
|
||||
input()
|
||||
|
||||
|
||||
|
||||
for i in programs:
|
||||
print(i,end='')
|
||||
print()
|
||||
|
||||
|
||||
#########################################
|
||||
# #
|
||||
# Part 2 #
|
||||
# #
|
||||
#########################################
|
||||
if part == 2:
|
||||
|
||||
|
||||
with open(input_f) as file:
|
||||
for line in file:
|
||||
instructions = line.rstrip().split(',')
|
||||
|
||||
programs = collections.deque(list(programs))
|
||||
|
||||
#print(programs)
|
||||
#print(instructions)
|
||||
print(len(instructions))
|
||||
|
||||
for r in range(0,1000000000):
|
||||
for idx,i in enumerate(instructions):
|
||||
inst = parse_input(i)
|
||||
#print(idx,end=' ')
|
||||
#print(i)
|
||||
#rint(inst)
|
||||
if inst[0] == 's':
|
||||
#print('Spin', i)
|
||||
programs.rotate(inst[1])
|
||||
|
||||
elif inst[0] == 'x':
|
||||
#print('Exchange',i)
|
||||
swap_pos(programs,inst[1],inst[2])
|
||||
|
||||
elif inst[0] == 'p':
|
||||
#print('Partner',i)
|
||||
swap_items(programs,inst[1],inst[2])
|
||||
else:
|
||||
print(inst)
|
||||
input()
|
||||
if r % 10000 == 0:
|
||||
print(r)
|
||||
|
||||
|
||||
|
||||
for i in programs:
|
||||
print(i,end='')
|
||||
print()
|
||||
Reference in New Issue
Block a user