aoc2021/d04/d04.ipynb

193 lines
4.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "e4d43863",
"metadata": {},
"source": [
"# setup"
]
},
{
"cell_type": "code",
"execution_count": 65,
"id": "97f08c2f",
"metadata": {},
"outputs": [],
"source": [
"input = open(\"input\",\"rt\").read()"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "e8a71b8a",
"metadata": {},
"outputs": [],
"source": [
"splinput = input.split(\"\\n\\n\")"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "fd29a42b",
"metadata": {},
"outputs": [],
"source": [
"numbers = list(map(int, splinput[0].split(',')))"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "1a0fb4f1",
"metadata": {},
"outputs": [],
"source": [
"grids = []\n",
"ingrids = [l.splitlines() for l in splinput[1:]]\n",
"for grid in ingrids:\n",
" rows = []\n",
" for row in grid:\n",
" rows.append(list(map(lambda x: (int(x), False),row.strip().replace(' ',' ').split(' '))))\n",
" grids.append(rows)"
]
},
{
"cell_type": "markdown",
"id": "2891f090",
"metadata": {},
"source": [
"# commons"
]
},
{
"cell_type": "code",
"execution_count": 69,
"id": "a460886c",
"metadata": {},
"outputs": [],
"source": [
"def mark(ingrids, number):\n",
" grids = []\n",
" for grid in ingrids:\n",
" rows = []\n",
" for row in grid:\n",
" rows.append(list(map(lambda x: (x[0], x[1] or x[0]==number), row)))\n",
" grids.append(rows)\n",
" return grids\n",
"\n",
"def atleast1_row_completed(grid):\n",
" for row in grid:\n",
" if all(map(lambda x: x[1], row)):\n",
" return True\n",
" return False\n",
"\n",
"def wining_grid(grid):\n",
" return atleast1_row_completed(grid) or atleast1_row_completed(list(map(list,zip(*grid))))\n",
"\n",
"from itertools import chain\n",
"\n",
"def sum_unmarked(grid):\n",
" return sum(map(lambda x: x[0], filter(lambda x: not x[1], list(chain(*grid)))))"
]
},
{
"cell_type": "markdown",
"id": "504cbf6d",
"metadata": {},
"source": [
"# part 1"
]
},
{
"cell_type": "code",
"execution_count": 70,
"id": "e18c276b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"74320"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def run_1(grids, numbers):\n",
" for number in numbers:\n",
" grids = mark(grids, number)\n",
" for grid in grids:\n",
" if wining_grid(grid):\n",
" return number *sum_unmarked(grid) \n",
"\n",
"run_1(grids, numbers)"
]
},
{
"cell_type": "markdown",
"id": "48f45375",
"metadata": {},
"source": [
"# part 2"
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "61f6c035",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"17884"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def run_2(grids, numbers):\n",
" for (i, number) in enumerate(numbers):\n",
" grids = mark(grids, number)\n",
" # stop if all grid are winning except one\n",
" loosing_grids = list(filter(lambda g: not wining_grid(g), grids))\n",
" if len(loosing_grids) == 1:\n",
" lg = loosing_grids[0]\n",
" return run_1([lg], numbers[i:])\n",
"\n",
"run_2(grids, numbers)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}