AdventOfCode/2022/day5.ipynb

106 lines
2.9 KiB
Plaintext
Raw Permalink Normal View History

2022-12-05 15:56:53 +10:30
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"import numpy as np\n",
"# pattern = re.compile(r'(\\d+)-(\\d+),(\\d+)-(\\d+)')\n",
"numbers_pattern = re.compile(r'((?:(?<!\\d)-)?\\d+)')\n",
"\n",
"with open('input/5', 'r') as file:\n",
" lines = file.readlines()\n",
"\n",
"# numbers = [[int(x) for x in numbers_pattern.findall(line)] for line in lines]\n",
"# print(numbers)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Part 1: GFTNRBZPF\n",
"Part 2: VRQWPDSGP\n"
]
}
],
"source": [
"stacks = {}\n",
"# [J] [F] [M] \n",
"# [Z] [F] [G] [Q] [F] \n",
"# [G] [P] [H] [Z] [S] [Q] \n",
"# [V] [W] [Z] [P] [D] [G] [P] \n",
"# [T] [D] [S] [Z] [N] [W] [B] [N] \n",
"# [D] [M] [R] [J] [J] [P] [V] [P] [J]\n",
"# [B] [R] [C] [T] [C] [V] [C] [B] [P]\n",
"# [N] [S] [V] [R] [T] [N] [G] [Z] [W]\n",
"# 1 2 3 4 5 6 7 8 9 \n",
"stacks[1] = [c for c in 'NBDTVGZJ']\n",
"stacks[2] = [c for c in 'SRMDWPF']\n",
"stacks[3] = [c for c in 'VCRSZ']\n",
"stacks[4] = [c for c in 'RTJZPHG']\n",
"stacks[5] = [c for c in 'TCJNDZQF']\n",
"stacks[6] = [c for c in 'NVPWGSFM']\n",
"stacks[7] = [c for c in 'GCVBPQ']\n",
"stacks[8] = [c for c in 'ZBPN']\n",
"stacks[9] = [c for c in 'WPJ']\n",
"base_stacks = {k:[x for x in v] for k,v in stacks.items()}\n",
"\n",
"# numbers = numbers[10:]\n",
"# print(numbers)\n",
"for l in lines[10:]:\n",
" n = [int(x) for x in numbers_pattern.findall(l)]\n",
" num, source, dest = n\n",
" for i in range(0, num):\n",
" stacks[dest].append(stacks[source].pop())\n",
"# print(stacks)\n",
"print('Part 1: ' + ''.join(s[-1] for s in stacks.values()))\n",
"\n",
"stacks = base_stacks\n",
"for l in lines[10:]:\n",
" n = [int(x) for x in numbers_pattern.findall(l)]\n",
" num, source, dest = n\n",
" stacks[dest] += stacks[source][-num:]\n",
" stacks[source] = stacks[source][:-num]\n",
"# print(stacks)\n",
"print('Part 2: ' + ''.join(s[-1] for s in stacks.values()))\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.11.0 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "5c7b89af1651d0b8571dde13640ecdccf7d5a6204171d6ab33e7c296e100e08a"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}