|
| 1 | +import os |
| 2 | +import re |
| 3 | + |
| 4 | +def parse_stacks(lines): |
| 5 | + stacks = {} |
| 6 | + for line in reversed(lines): |
| 7 | + regex = re.compile('(?:\[(.)\] ?| ?)', re.DOTALL) |
| 8 | + crates = regex.findall(line) |
| 9 | + for i, crate in enumerate(crates): |
| 10 | + if stacks.get(i + 1) == None: |
| 11 | + stacks[i + 1] = [] |
| 12 | + if crate != '': |
| 13 | + stacks[i + 1].append(crate) |
| 14 | + return stacks |
| 15 | + |
| 16 | +def part1(lines): |
| 17 | + crate_definition_lines = [] |
| 18 | + instructions = [] |
| 19 | + for line in lines: |
| 20 | + if line == "\n": |
| 21 | + continue |
| 22 | + |
| 23 | + if line.startswith('move'): |
| 24 | + regex = re.compile('move (\d+) from (\d+) to (\d+)') |
| 25 | + matches = regex.match(line).groups() |
| 26 | + instruction = { |
| 27 | + 'count': int(matches[0]), |
| 28 | + 'from': int(matches[1]), |
| 29 | + 'to': int(matches[2]) |
| 30 | + } |
| 31 | + instructions.append(instruction) |
| 32 | + else: |
| 33 | + crate_definition_lines.append(line) |
| 34 | + |
| 35 | + # We don't care about the labels |
| 36 | + crate_definition_lines.pop() |
| 37 | + stacks = parse_stacks(crate_definition_lines) |
| 38 | + |
| 39 | + for instruction in instructions: |
| 40 | + for count in range(instruction['count']): |
| 41 | + item = stacks[instruction['from']].pop() |
| 42 | + stacks[instruction['to']].append(item) |
| 43 | + |
| 44 | + top_crates = [] |
| 45 | + |
| 46 | + for i in stacks: |
| 47 | + stack = stacks[i] |
| 48 | + top_crates.append(stack[len(stack) - 1]) |
| 49 | + |
| 50 | + return ''.join(top_crates) |
| 51 | + |
| 52 | + |
| 53 | +def part2(lines): |
| 54 | + crate_definition_lines = [] |
| 55 | + instructions = [] |
| 56 | + for line in lines: |
| 57 | + if line == "\n": |
| 58 | + continue |
| 59 | + |
| 60 | + if line.startswith('move'): |
| 61 | + regex = re.compile('move (\d+) from (\d+) to (\d+)') |
| 62 | + matches = regex.match(line).groups() |
| 63 | + instruction = { |
| 64 | + 'count': int(matches[0]), |
| 65 | + 'from': int(matches[1]), |
| 66 | + 'to': int(matches[2]) |
| 67 | + } |
| 68 | + instructions.append(instruction) |
| 69 | + else: |
| 70 | + crate_definition_lines.append(line) |
| 71 | + |
| 72 | + # We don't care about the labels |
| 73 | + crate_definition_lines.pop() |
| 74 | + stacks = parse_stacks(crate_definition_lines) |
| 75 | + |
| 76 | + for instruction in instructions: |
| 77 | + temp = [] |
| 78 | + for count in range(instruction['count']): |
| 79 | + temp.append(stacks[instruction['from']].pop()) |
| 80 | + |
| 81 | + temp.reverse() |
| 82 | + stacks[int(instruction['to'])] += temp |
| 83 | + |
| 84 | + top_crates = [] |
| 85 | + |
| 86 | + for i in stacks: |
| 87 | + stack = stacks[i] |
| 88 | + top_crates.append(stack[len(stack) - 1]) |
| 89 | + |
| 90 | + return ''.join(top_crates) |
| 91 | + |
| 92 | + |
| 93 | +dirname = os.path.dirname(__file__) |
| 94 | +filename = os.path.join(dirname, '../input.txt') |
| 95 | +with open(filename) as f: |
| 96 | + lines = f.readlines() |
| 97 | + |
| 98 | +print(part1(lines)) |
| 99 | +print(part2(lines)) |
0 commit comments