{ "cells": [ { "cell_type": "markdown", "id": "e4d43863", "metadata": {}, "source": [ "# setup" ] }, { "cell_type": "code", "execution_count": 12, "id": "f81b055e", "metadata": {}, "outputs": [], "source": [ "sample = \"\"\"7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1\n", "\n", "22 13 17 11 0\n", " 8 2 23 4 24\n", "21 9 14 16 7\n", " 6 10 3 18 5\n", " 1 12 20 15 19\n", "\n", " 3 15 0 2 22\n", " 9 18 13 17 5\n", "19 8 7 25 23\n", "20 11 10 24 4\n", "14 21 16 12 6\n", "\n", "14 21 17 24 4\n", "10 16 15 9 19\n", "18 8 23 26 20\n", "22 11 13 6 5\n", " 2 0 12 3 7\"\"\"" ] }, { "cell_type": "code", "execution_count": 13, "id": "61075998", "metadata": {}, "outputs": [], "source": [ "input = open(\"input\",\"rt\").read()" ] }, { "cell_type": "code", "execution_count": 14, "id": "e8a71b8a", "metadata": {}, "outputs": [], "source": [ "splinput = input.split(\"\\n\\n\")" ] }, { "cell_type": "code", "execution_count": 15, "id": "fd29a42b", "metadata": {}, "outputs": [], "source": [ "numbers = list(map(int, splinput[0].split(',')))" ] }, { "cell_type": "code", "execution_count": 16, "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": 17, "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": 18, "id": "e18c276b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "74320" ] }, "execution_count": 18, "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": 19, "id": "61f6c035", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17884" ] }, "execution_count": 19, "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+1:])\n", "\n", "run_2(grids, numbers)" ] }, { "cell_type": "code", "execution_count": null, "id": "128cac38", "metadata": {}, "outputs": [], "source": [] } ], "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 }