Skip to content

Commit

Permalink
2017 day 16 solution
Browse files Browse the repository at this point in the history
Signed-off-by: Lance-Drane <ldraneutk@gmail.com>
  • Loading branch information
Lance-Drane committed Jan 15, 2024
1 parent 45d6b55 commit 0541128
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
25 changes: 25 additions & 0 deletions 2017/16/1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
programs = list('abcdefghijklmnop')


def swap(x: int, y: int):
tmp = programs[y]
programs[y] = programs[x]
programs[x] = tmp


for dance in input().split(','):
move = dance[0]
if move == 's':
rotation = int(dance[1:])
programs = programs[-rotation:] + programs[:-rotation]
else:
x, y = dance[1:].split('/')
if move == 'x':
x, y = int(x), int(y)
swap(x, y)
else: # partner
x, y = programs.index(x), programs.index(y)
swap(x, y)
print(programs)

print(''.join(programs))
1 change: 1 addition & 0 deletions 2017/16/1_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ebjpfdgmihonackl
42 changes: 42 additions & 0 deletions 2017/16/2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from itertools import count

base_programs = list('abcdefghijklmnop')
dances = input().split(',')


def dance(programs: list[str]) -> list[str]:
def swap(x: int, y: int):
tmp = programs[y]
programs[y] = programs[x]
programs[x] = tmp

for dance in dances:
move = dance[0]
if move == 's':
rotation = int(dance[1:])
programs = programs[-rotation:] + programs[:-rotation]
else:
x, y = dance[1:].split('/')
if move == 'x':
x, y = int(x), int(y)
swap(x, y)
else: # partner
x, y = programs.index(x), programs.index(y)
swap(x, y)

return programs


programs = base_programs.copy()

# find cycle
for dance_iteration in count(start=1): # noqa: B007 (we use the value later)
programs = dance(programs)
if programs == base_programs:
break

# execute the cycle the appropriate number of times
for _ in range(1_000_000_000 % dance_iteration):
programs = dance(programs)

print(''.join(programs))
1 change: 1 addition & 0 deletions 2017/16/2_out.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abocefghijklmndp
1 change: 1 addition & 0 deletions 2017/16/in.txt

Large diffs are not rendered by default.

0 comments on commit 0541128

Please sign in to comment.