Skip to content
This repository was archived by the owner on Jan 10, 2024. It is now read-only.

Commit 5658eb1

Browse files
committed
21
1 parent 89ded4f commit 5658eb1

File tree

3 files changed

+200
-0
lines changed

3 files changed

+200
-0
lines changed

21.input

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
rotate right 3 steps
2+
swap position 7 with position 0
3+
rotate left 3 steps
4+
reverse positions 2 through 5
5+
move position 6 to position 3
6+
reverse positions 0 through 4
7+
swap position 4 with position 2
8+
rotate based on position of letter d
9+
rotate right 0 steps
10+
move position 7 to position 5
11+
swap position 4 with position 5
12+
swap position 3 with position 5
13+
move position 5 to position 3
14+
swap letter e with letter f
15+
swap position 6 with position 3
16+
swap letter a with letter e
17+
reverse positions 0 through 1
18+
reverse positions 0 through 4
19+
swap letter c with letter e
20+
reverse positions 1 through 7
21+
rotate right 1 step
22+
reverse positions 6 through 7
23+
move position 7 to position 1
24+
move position 4 to position 0
25+
move position 4 to position 6
26+
move position 6 to position 3
27+
swap position 1 with position 6
28+
swap position 5 with position 7
29+
swap position 2 with position 5
30+
swap position 6 with position 5
31+
swap position 2 with position 4
32+
reverse positions 2 through 6
33+
reverse positions 3 through 5
34+
move position 3 to position 5
35+
reverse positions 1 through 5
36+
rotate left 1 step
37+
move position 4 to position 5
38+
swap letter c with letter b
39+
swap position 2 with position 1
40+
reverse positions 3 through 4
41+
swap position 3 with position 4
42+
reverse positions 5 through 7
43+
swap letter b with letter d
44+
reverse positions 3 through 4
45+
swap letter c with letter h
46+
rotate based on position of letter b
47+
rotate based on position of letter e
48+
rotate right 3 steps
49+
rotate right 7 steps
50+
rotate left 2 steps
51+
move position 6 to position 1
52+
reverse positions 1 through 3
53+
rotate based on position of letter b
54+
reverse positions 0 through 4
55+
swap letter g with letter c
56+
move position 1 to position 5
57+
rotate right 4 steps
58+
rotate left 2 steps
59+
move position 7 to position 2
60+
rotate based on position of letter c
61+
move position 6 to position 1
62+
swap letter f with letter g
63+
rotate right 6 steps
64+
swap position 6 with position 2
65+
reverse positions 2 through 6
66+
swap position 3 with position 1
67+
rotate based on position of letter h
68+
reverse positions 2 through 5
69+
move position 1 to position 3
70+
rotate right 1 step
71+
rotate right 7 steps
72+
move position 6 to position 3
73+
rotate based on position of letter h
74+
swap letter d with letter h
75+
rotate left 0 steps
76+
move position 1 to position 2
77+
swap letter a with letter g
78+
swap letter a with letter g
79+
swap position 4 with position 2
80+
rotate right 1 step
81+
rotate based on position of letter b
82+
swap position 7 with position 1
83+
rotate based on position of letter e
84+
move position 1 to position 4
85+
move position 6 to position 3
86+
rotate left 3 steps
87+
swap letter f with letter g
88+
swap position 3 with position 1
89+
swap position 4 with position 3
90+
swap letter f with letter c
91+
rotate left 3 steps
92+
rotate left 0 steps
93+
rotate right 3 steps
94+
swap letter d with letter e
95+
swap position 2 with position 7
96+
move position 3 to position 6
97+
swap position 7 with position 1
98+
swap position 3 with position 6
99+
rotate left 5 steps
100+
swap position 2 with position 6

21.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import re
2+
3+
import sys
4+
inputdata = sys.stdin.readlines()
5+
6+
data = [x.rstrip('\n') for x in inputdata]
7+
8+
def getRegexResult(prog, s):
9+
m = prog.match(s)
10+
return m.groups() if m else []
11+
12+
def handle_sp(s, x, y):
13+
x = int(x)
14+
y = int(y)
15+
16+
lresult = list(s)
17+
tmp = lresult[x]
18+
lresult[x] = lresult[y]
19+
lresult[y] = tmp
20+
21+
return ''.join(lresult)
22+
23+
def handle_sl(s, x, y):
24+
return s.replace(x, '1').replace(y, x).replace('1', y)
25+
26+
def handle_rl(s, x):
27+
x = int(x)
28+
29+
return s[x:] + s[:x]
30+
31+
def handle_rr(s, x):
32+
x = int(x)
33+
34+
return s[-x:] + s[:-x]
35+
36+
def handle_rp(s, x):
37+
i = s.index(x)
38+
39+
r = handle_rr(s, 1)
40+
r = handle_rr(r, i)
41+
if i >= 4:
42+
r = handle_rr(r, 1)
43+
44+
return r
45+
46+
def handle_rv(s, x, y):
47+
x = int(x)
48+
y = int(y)
49+
50+
return s[:x] + s[x:(y+1)][::-1] + s[(y+1):]
51+
52+
def handle_mv(s, x, y):
53+
x = int(x)
54+
y = int(y)
55+
56+
lresult = list(s)
57+
tmp = lresult[x]
58+
del lresult[x]
59+
lresult.insert(y, tmp)
60+
61+
return ''.join(lresult)
62+
63+
rules = [
64+
(re.compile(r'^swap position (\d*) with position (\d*)$'), handle_sp),
65+
(re.compile(r'^swap letter (\w) with letter (\w)$'), handle_sl),
66+
(re.compile(r'^rotate left (\d*) steps?$'), handle_rl),
67+
(re.compile(r'^rotate right (\d*) steps?$'), handle_rr),
68+
(re.compile(r'^rotate based on position of letter (\w)$'), handle_rp),
69+
(re.compile(r'^reverse positions (\d*) through (\d*)$'), handle_rv),
70+
(re.compile(r'^move position (\d*) to position (\d*)$'), handle_mv)
71+
]
72+
73+
def run(input):
74+
global data
75+
76+
r = input
77+
for d in data:
78+
params, handler = filter(lambda x: x[0], [(getRegexResult(prog, d), handler) for prog, handler in rules])[0]
79+
r = handler(r, *params)
80+
return r
81+
82+
# part 1
83+
print run('abcdefgh')
84+
85+
# ugly part 2
86+
from itertools import permutations
87+
for p in permutations('abcdefgh'):
88+
p = ''.join(p)
89+
90+
if run(p) == 'fbgdceah':
91+
print p
92+
break

21.simple.input

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
swap position 4 with position 0
2+
swap letter d with letter b
3+
reverse positions 0 through 4
4+
rotate left 1 step
5+
move position 1 to position 4
6+
move position 3 to position 0
7+
rotate based on position of letter b
8+
rotate based on position of letter d

0 commit comments

Comments
 (0)