{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Day 25 - Adventure!\n",
    "\n",
    "This is a text adventure. Just enjoy playing it :-D\n",
    "\n",
    "(Yes, I could automate exploration, item collection and somehow detecting the items you shouldn't want to take, but where's the fun in that).\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import annotations\n",
    "\n",
    "import sys\n",
    "from typing import Callable, List\n",
    "\n",
    "from intcode import CPU, Instruction, InstructionSet, base_opcodes\n",
    "\n",
    "\n",
    "def ascii_input() -> Callable[[], int]:\n",
    "    buffer: List[int] = []\n",
    "\n",
    "    def input_one() -> int:\n",
    "        nonlocal buffer\n",
    "        if not buffer:\n",
    "            buffer += input().encode(\"ASCII\")\n",
    "            buffer += (10,)\n",
    "            buffer.reverse()\n",
    "        return buffer.pop()\n",
    "\n",
    "    return input_one\n",
    "\n",
    "\n",
    "def print_ascii(value: int) -> None:\n",
    "    sys.stdout.write(chr(value))\n",
    "\n",
    "\n",
    "def run_ascii(memory: List[int]) -> None:\n",
    "    opcodes: InstructionSet = {\n",
    "        **base_opcodes,\n",
    "        3: Instruction(ascii_input(), output=True),\n",
    "        4: Instruction(print_ascii, 1),\n",
    "    }\n",
    "    CPU(opcodes).reset(memory).execute()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import aocd\n",
    "\n",
    "data = aocd.get_data(day=25, year=2019)\n",
    "memory = list(map(int, data.split(\",\")))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "== Hull Breach ==\n",
      "You got in through a hole in the floor here. To keep your ship from also freezing, the hole has been sealed.\n",
      "\n",
      "Doors here lead:\n",
      "- north\n",
      "- east\n",
      "- south\n",
      "\n",
      "Command?\n"
     ]
    }
   ],
   "source": [
    "run_ascii(memory)"
   ]
  }
 ],
 "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.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}