Browse Source

Day 21

master
Chris Smith 7 years ago
parent
commit
78f5700ce8
2 changed files with 167 additions and 0 deletions
  1. 67
    0
      21.py
  2. 100
    0
      data/21.txt

+ 67
- 0
21.py View File

@@ -0,0 +1,67 @@
1
+#!/usr/bin/python3
2
+
3
+"""Solution for day 21 of Advent of Code 2016.
4
+
5
+Each operation has an entry in the ops dictionary, which mangles arguments and does pre-processing before handing off
6
+to one of the main methods to perform the actual transformation.
7
+
8
+For part two, the reverse_ops dictionary maps most operations to a reverse of their original. The only exception is
9
+'rotate based on position', which is just brute-forced by rotating the letters and seeing if the forward instruction
10
+puts them back to the expected positions.
11
+"""
12
+
13
+
14
+from functools import reduce
15
+
16
+ops = {
17
+    'swap position':     lambda l, a: swap_positions(l, int(a[0]), int(a[3])),
18
+    'swap letter':       lambda l, a: swap_letter(l, a[0], a[3]),
19
+    'rotate left':       lambda l, a: rotate(l, -int(a[0])),
20
+    'rotate right':      lambda l, a: rotate(l, int(a[0])),
21
+    'rotate based':      lambda l, a: rotate(l, 1 + l.index(a[4]) + (1 if l.index(a[4]) > 3 else 0)),
22
+    'reverse positions': lambda l, a: reverse(l, int(a[0]), int(a[2])),
23
+    'move position':     lambda l, a: move(l, int(a[0]), int(a[3])),
24
+}
25
+
26
+reverse_ops = {
27
+    'swap position':     lambda l, a: swap_positions(l, int(a[3]), int(a[0])),
28
+    'swap letter':       ops['swap letter'],
29
+    'rotate left':       ops['rotate right'],
30
+    'rotate right':      ops['rotate left'],
31
+    'rotate based':      lambda l, a: reverse_rotation(l, a),
32
+    'reverse positions': ops['reverse positions'],
33
+    'move position':     lambda l, a: move(l, int(a[3]), int(a[0])),
34
+}
35
+
36
+
37
+def swap_positions(letters, x, y):
38
+    letters[x], letters[y] = letters[y], letters[x]
39
+    return letters
40
+
41
+
42
+def swap_letter(letters, a, b):
43
+    return [a if l == b else b if l == a else l for l in letters]
44
+
45
+
46
+def rotate(letters, num):
47
+    num %= len(letters)
48
+    return letters[-num:] + letters[:-num]
49
+
50
+
51
+def reverse(letters, start, end):
52
+    return letters[:start] + letters[start:end+1][::-1] + letters[end+1:]
53
+
54
+
55
+def move(letters, start, end):
56
+    letters.insert(end, letters.pop(start))
57
+    return letters
58
+
59
+
60
+def reverse_rotation(l, a):
61
+    return [rotate(l, i) for i in range(len(l)) if ops['rotate based'](rotate(l, i), a) == l][0]
62
+
63
+
64
+with open('data/21.txt', 'r') as file:
65
+    instr = list(map(lambda x: [' '.join(x.split(' ')[0:2])] + x.strip().split(' ')[2:], file.readlines()))
66
+    print('Part one: %s' % ''.join(reduce(lambda l, o: ops[o[0]](l, o[1:]), instr, list('abcdefgh'))))
67
+    print('Part two: %s' % ''.join(reduce(lambda l, o: reverse_ops[o[0]](l, o[1:]), instr[::-1], list('fbgdceah'))))

+ 100
- 0
data/21.txt View File

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

Loading…
Cancel
Save