X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=src%2Fadvent11%2Fadvent11.ipynb;fp=src%2Fadvent11%2Fadvent11.ipynb;h=6d40bf564a2aa6f5ce9f3f04381adbb20d764e88;hb=c8dd17f67a0e6574e60eba88ca85adde5ad16b69;hp=0000000000000000000000000000000000000000;hpb=85761c8d6f9d0edfbf382c15c47edc7a412f92ee;p=advent-of-code-17.git diff --git a/src/advent11/advent11.ipynb b/src/advent11/advent11.ipynb new file mode 100644 index 0000000..6d40bf5 --- /dev/null +++ b/src/advent11/advent11.ipynb @@ -0,0 +1,520 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "{-# LANGUAGE NegativeLiterals #-}\n", + "{-# LANGUAGE FlexibleContexts #-}" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "import Data.List.Split (splitOn)\n", + "import Data.List (mapAccumL)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "hexStep (Int, Int) -> String -> (Int, Int)\n", + "hexStep (n, ne) s = case s of \n", + " \"n\" -> (n + 1, ne)\n", + " \"ne\" -> (n, ne + 1)\n", + " \"nw\" -> (n + 1, ne - 1)\n", + " \"s\" -> (n - 1, ne)\n", + " \"se\" -> (n - 1, ne + 1)\n", + " \"sw\" -> (n, ne - 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(4,[1,3,5,7])" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "mapAccumL (\\a b -> (a + 1, a + b)) 0 [1,2,3,4]" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [], + "source": [ + "hexPath = foldl hexStep (0, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "distance $ hexPath $ splitOn \",\" \"ne,ne,ne\"" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "distance $ hexPath $ splitOn \",\" \"ne,ne,sw,sw\"" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "distance $ hexPath $ splitOn \",\" \"ne,ne,s,s\"" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "distance $ hexPath $ splitOn \",\" \"se,sw,se,sw,sw\"" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Redundant bracket
Found:
(abs n) + (abs ne)
Why Not:
abs n + (abs ne)
Redundant bracket
Found:
(abs n) + (abs ne)
Why Not:
(abs n) + abs ne
Redundant bracket
Found:
(abs n) - smallest
Why Not:
abs n - smallest
Redundant bracket
Found:
(abs ne) - smallest
Why Not:
abs ne - smallest
" + ], + "text/plain": [ + "Line 2: Redundant bracket\n", + "Found:\n", + "(abs n) + (abs ne)\n", + "Why not:\n", + "abs n + (abs ne)Line 2: Redundant bracket\n", + "Found:\n", + "(abs n) + (abs ne)\n", + "Why not:\n", + "(abs n) + abs neLine 5: Redundant bracket\n", + "Found:\n", + "(abs n) - smallest\n", + "Why not:\n", + "abs n - smallestLine 5: Redundant bracket\n", + "Found:\n", + "(abs ne) - smallest\n", + "Why not:\n", + "abs ne - smallest" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "distance (n, ne) = if n * ne > 0 \n", + " then (abs n) + (abs ne)\n", + " else smallest + remainder\n", + " where smallest = min (abs n) (abs ne)\n", + " remainder = max ((abs n) - smallest) ((abs ne) - smallest)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [], + "source": [ + "part1 = distance . hexPath . splitOn \",\"" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " text <- readFile \"../../data/advent11.txt\"\n", + "-- let instrs = map read $ splitOn \",\" text\n", + " print $ part1 text\n", + "-- print $ part2 text" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "670" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "main" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "hexPathB = scanl hexStep (0, 0)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "maximum $ map distance $ hexPathB $ splitOn \",\" \"ne,ne,ne\"" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[(0,0),(0,1),(0,2),(0,3)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "hexPathB $ splitOn \",\" \"ne,ne,ne\"" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0,1,2,3]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "map distance $ hexPathB $ splitOn \",\" \"ne,ne,ne\"" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([(0,0),(0,1),(0,2),(0,3)],[0,1,2,3],3)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "pth = splitOn \",\" \"ne,ne,ne\"\n", + "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([(0,0),(0,1),(0,2),(0,1),(0,0)],[0,1,2,1,0],2)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "pth = splitOn \",\" \"ne,ne,sw,sw\"\n", + "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([(0,0),(0,1),(0,2),(-1,2),(-2,2)],[0,1,2,2,2],2)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "pth = splitOn \",\" \"ne,ne,s,s\"\n", + "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([(0,0),(-1,1),(-1,0),(-2,1),(-2,0),(-2,-1)],[0,1,1,2,2,3],3)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "pth = splitOn \",\" \"se,sw,se,sw,sw\"\n", + "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "part2 = maximum . map distance . hexPathB . splitOn \",\"" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " text <- readFile \"../../data/advent11.txt\"\n", + "-- let instrs = map read $ splitOn \",\" text\n", + " print $ part1 text\n", + " print $ part2 text" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "670\n", + "1426" + ] + }, + "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 +}