Files
adventcode2024/4/4.ipynb
Tobias Kessels 77c5f531f0 Light Clean up
2024-12-12 12:50:03 +01:00

183 lines
3.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#read lines from input and store each list of space separeted integers into left and right lists respectively.\n",
"with open('input', 'r') as f:\n",
" data=f.readlines()\n",
"\n",
"data = [line.strip() for line in data]\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"421"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"horizontal = sum(row.count('XMAS') + row.count('SAMX') for row in data)\n",
"horizontal"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"463"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vertical = sum(\"\".join(col).count('XMAS') + \"\".join(col).count('SAMX') for col in zip(*data))\n",
"vertical"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1640"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"\n",
"def diagonals(matrix):\n",
" np_matrix = np.array(matrix)\n",
" n, m = np_matrix.shape\n",
" for offset in range(-n + 1, m): # Diagonal offsets\n",
" yield np_matrix.diagonal(offset)\n",
" yield np.fliplr(np_matrix).diagonal(offset)\n",
"\n",
"matrix = np.array([list(line) for line in data])\n",
"\n",
"diagonal = sum(\"\".join(diag).count('XMAS') + \"\".join(diag).count('SAMX') for diag in diagonals(matrix))\n",
"diagonal"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2524"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"horizontal + vertical + diagonal"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"pattern = np.array([\n",
" [\"M\",\".\",\"M\"],\n",
" [\".\",\"A\",\".\"],\n",
" [\"S\",\".\",\"S\"]\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"mask = pattern != '.'"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1873"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from numpy.lib.stride_tricks import sliding_window_view\n",
" \n",
"windows = sliding_window_view(matrix,pattern.shape)\n",
"count = 0\n",
"for i in range(4):\n",
" matches = (windows == np.rot90(pattern,k=i)) | ~mask \n",
" count += np.sum(np.all(np.all(matches,axis=-1),axis=-1))\n",
"\n",
"count"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}