mirror of
https://github.com/RussellChamp/advent-of-code-2022.git
synced 2026-10-27 21:52:30 +00:00
day05 solution
This commit is contained in:
1 parent
312c9107e3
commit
dfe8eaf2ab
3 files changed
+65
-1
No files matched your search
+1
-1
@@ -1,2 +1,2 @@
|
||||
data.txt
|
||||
*.txt
|
||||
*.pyc
|
||||
@@ -0,0 +1,52 @@
|
||||
# Day 05: Supply Stacks
|
||||
|
||||
import os
|
||||
import re
|
||||
|
||||
# The moves part of the input data have been put into a separate file
|
||||
def get_moves_path():
|
||||
return os.path.join(os.path.dirname(__file__), 'moves.txt')
|
||||
|
||||
# The stacks part of the input data have been transformed into rows instead of columns and put into a separate file
|
||||
# Each row represents a stack with the bottom crate starting in the first index. letter are separated by spaces
|
||||
def get_stacks_path():
|
||||
return os.path.join(os.path.dirname(__file__), 'stacks.txt')
|
||||
|
||||
def get_last(arr):
|
||||
return arr[-1]
|
||||
|
||||
def solve_part1():
|
||||
stacks = map(str.split, open(get_stacks_path()).readlines())
|
||||
# print(stacks)
|
||||
|
||||
with open(get_moves_path()) as moves:
|
||||
for move in moves:
|
||||
[count, source, dest] = map(int, re.match('move (\d+) from (\d+) to (\d+)', move).groups())
|
||||
for _ in range(count):
|
||||
stacks[dest - 1].append(stacks[source - 1].pop())
|
||||
# print('Moved %d from stack %d to stack %d' % (count, source, dest))
|
||||
|
||||
print('GOAL: Which crate will end up on top of each stack?')
|
||||
print('ANSWER: After the rearrangement procedure completes, what crate ends up on top of each stack?')
|
||||
tops = ''.join(map(get_last, stacks))
|
||||
print('Tops: %s' % (tops))
|
||||
|
||||
def solve_part2():
|
||||
stacks = map(str.split, open(get_stacks_path()).readlines())
|
||||
# print(stacks)
|
||||
|
||||
with open(get_moves_path()) as moves:
|
||||
for move in moves:
|
||||
[count, source, dest] = map(int, re.match('move (\d+) from (\d+) to (\d+)', move).groups())
|
||||
# print('moving %d items from %s to %s' % (count, stacks[source-1], stacks[dest-1]))
|
||||
moved_items = stacks[source-1][-count:]
|
||||
del stacks[source-1][-count:]
|
||||
stacks[dest-1] += moved_items
|
||||
|
||||
print('ANSWER: After the rearrangement procedure completes, what crate ends up on top of each stack?')
|
||||
tops = ''.join(map(get_last, stacks))
|
||||
print('Tops: %s' % (tops))
|
||||
|
||||
if __name__ == '__main__':
|
||||
solve_part1()
|
||||
solve_part2()
|
||||
@@ -0,0 +1,12 @@
|
||||
import unittest
|
||||
|
||||
class Day05TestMethods(unittest.TestCase):
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
# Does a thing
|
||||
def test_true(self):
|
||||
self.assertEqual(True, False)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user