Rearranged file locations
[advent-of-code-17.git] / src / advent02 / advent02.ipynb
diff --git a/src/advent02/advent02.ipynb b/src/advent02/advent02.ipynb
new file mode 100644 (file)
index 0000000..490084b
--- /dev/null
@@ -0,0 +1,352 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "{-# LANGUAGE NegativeLiterals #-}\n",
+    "{-# LANGUAGE FlexibleContexts #-}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import Text.Parsec \n",
+    "import Text.ParserCombinators.Parsec.Number"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "onlySpaces = many (oneOf \" \\t\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "sFile = sLine `sepEndBy` newline \n",
+    "sLine = int `sepBy` onlySpaces"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "parseFile :: String -> Either ParseError [[Int]]\n",
+    "parseFile input = parse sFile \"(unknown)\" input\n",
+    "\n",
+    "parseLine :: String -> Either ParseError [Int]\n",
+    "parseLine input = parse sLine \"(unknown)\" input\n",
+    "\n",
+    "successfulParse :: Either ParseError [a] -> [a]\n",
+    "successfulParse (Left _) = []\n",
+    "successfulParse (Right a) = a"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "main :: IO ()\n",
+    "main = do \n",
+    "        text <- readFile \"../../data/advent02.txt\"\n",
+    "        let sheet = successfulParse $ parseFile text\n",
+    "        print sheet\n",
+    "--         print $ part1 sheet\n",
+    "--         print $ part2 sheet"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[[179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397],[2637,136,3222,591,2593,1982,4506,195,4396,3741,2373,157,4533,3864,4159,142],[1049,1163,1128,193,1008,142,169,168,165,310,1054,104,1100,761,406,173],[200,53,222,227,218,51,188,45,98,194,189,42,50,105,46,176],[299,2521,216,2080,2068,2681,2376,220,1339,244,605,1598,2161,822,387,268],[1043,1409,637,1560,970,69,832,87,78,1391,1558,75,1643,655,1398,1193],[90,649,858,2496,1555,2618,2302,119,2675,131,1816,2356,2480,603,65,128],[2461,5099,168,4468,5371,2076,223,1178,194,5639,890,5575,1258,5591,6125,226],[204,205,2797,2452,2568,2777,1542,1586,241,836,3202,2495,197,2960,240,2880],[560,96,336,627,546,241,191,94,368,528,298,78,76,123,240,563],[818,973,1422,244,1263,200,1220,208,1143,627,609,274,130,961,685,1318],[1680,1174,1803,169,450,134,3799,161,2101,3675,133,4117,3574,4328,3630,4186],[1870,3494,837,115,1864,3626,24,116,2548,1225,3545,676,128,1869,3161,109],[890,53,778,68,65,784,261,682,563,781,360,382,790,313,785,71],[125,454,110,103,615,141,562,199,340,80,500,473,221,573,108,536],[1311,64,77,1328,1344,1248,1522,51,978,1535,1142,390,81,409,68,352]]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "main"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "Right [179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "parseLine \"179 2358    5197    867 163 4418    3135    5049    187 166 4682    5080    5541    172 4294    1397\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "Right [[1,2],[8,9]]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "parseFile \"1 2\\n8 9\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[[1,2],[8,9]]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "successfulParse $ parseFile \"1 2\\n8 9\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "cSum :: [Int] -> Int\n",
+    "cSum row = (maximum row) - (minimum row)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "check :: [[Int]] -> Int\n",
+    "check = sum . (map cSum)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "2"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "check $ successfulParse $ parseFile \"1 2\\n8 9\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 14,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "part1 = check"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 15,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[(2,1),(3,1),(4,1),(4,2)]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "digits = [1,2,3,4]\n",
+    "[(a, b) | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 16,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[(1,1),(2,2),(3,3),(4,4)]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "zip digits digits"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 17,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[179,2358,5197,867,163,4418,3135,5049,187,166,4682,5080,5541,172,4294,1397]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "Right digits = parseLine \"179 2358    5197    867 163 4418    3135    5049    187 166 4682    5080    5541    172 4294    1397\"\n",
+    "digits"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 18,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "27"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 19,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 20,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "part2 = sum . map p2cSum"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "main :: IO ()\n",
+    "main = do \n",
+    "        text <- readFile \"../../data/advent02.txt\"\n",
+    "        let sheet = successfulParse $ parseFile text\n",
+    "--         print sheet\n",
+    "        print $ part1 sheet\n",
+    "        print $ part2 sheet"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 24,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "39126\n",
+       "258"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "main"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Haskell",
+   "language": "haskell",
+   "name": "haskell"
+  },
+  "language_info": {
+   "codemirror_mode": "ihaskell",
+   "file_extension": ".hs",
+   "name": "haskell",
+   "version": "8.0.2"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}