{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import networkx as nx\n",
    "\n",
    "with open('input', 'r') as f:\n",
    "    data=f.readlines()\n",
    "\n",
    "read_rules = True\n",
    "sequences =[]\n",
    "#was expecting fancier things\n",
    "rules = nx.DiGraph()\n",
    "\n",
    "for ln,line in enumerate([line.strip() for line in data]):\n",
    "    if read_rules:\n",
    "        if len(line) == 0:\n",
    "            read_rules = False\n",
    "            continue\n",
    "        left,right = line.split('|')\n",
    "        rules.add_edge(left,right)\n",
    "    else:\n",
    "        sequences.append(line.split(','))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def test_sequence(sequence):\n",
    "    for lidx,left in enumerate(sequence):\n",
    "        if rules.has_node(left):\n",
    "            for right in sequence[lidx:]:\n",
    "                if rules.has_node(right):\n",
    "                    if rules.has_edge(right,left):\n",
    "                        return False\n",
    "    return True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5087"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([int(sequence[len(sequence)//2]) for sequence in sequences if test_sequence(sequence)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Part 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "87"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "incorrect_sequences=[sequence for sequence in sequences if not test_sequence(sequence)]\n",
    "len(incorrect_sequences)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from functools import cmp_to_key\n",
    "\n",
    "def comparator(x, y):\n",
    "    # If both elements are in the graph\n",
    "    if x in rules.nodes and y in rules.nodes:\n",
    "        if rules.has_edge(x, y):  # x must come before y\n",
    "            return -1\n",
    "        elif rules.has_edge(y, x):  # y must come before x\n",
    "            return 1\n",
    "    # If one or both elements are missing, treat them as equal\n",
    "    return 0\n",
    "\n",
    "sorted_incorrect = [sorted(sequence, key=cmp_to_key(comparator)) for sequence in incorrect_sequences]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4971"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum([int(sequence[len(sequence)//2]) for sequence in sorted_incorrect if test_sequence(sequence)])"
   ]
  }
 ],
 "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
}
