X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=adventofcode1701%2Fapp%2Fadvent01-Copy1.ipynb;fp=adventofcode1701%2Fapp%2Fadvent01-Copy1.ipynb;h=9f5d993bfb0a0aeece37804bc1e5df3ae0fde05d;hb=1c73a76288c91ef6b410b2034bc324b60eb9a827;hp=0000000000000000000000000000000000000000;hpb=765da598a39a2c2b93102c182272b5546f4b6996;p=advent-of-code-17.git diff --git a/adventofcode1701/app/advent01-Copy1.ipynb b/adventofcode1701/app/advent01-Copy1.ipynb new file mode 100644 index 0000000..9f5d993 --- /dev/null +++ b/adventofcode1701/app/advent01-Copy1.ipynb @@ -0,0 +1,466 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "{-# LANGUAGE NegativeLiterals #-}\n", + "{-# LANGUAGE FlexibleContexts #-}" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "import Data.List (tails)\n", + "import Data.Char (digitToInt)" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "digits = \"1122\"" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "sum $ map (digitToInt . fst) $ filter (uncurry (==)) $ zip digits $ drop 1 $ cycle digits" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "solve n digits = sum $ map (digitToInt . fst) \n", + " $ filter (uncurry (==)) \n", + " $ zip digits \n", + " $ drop n \n", + " $ cycle digits" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [], + "source": [ + "solve n digits = sum $ zipWith (\\a b -> if a == b then digitToInt a else 0) digits \n", + " $ drop n \n", + " $ cycle digits" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "part1 = solve 1" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 \"1122\"" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 \"1111\"" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 \"1234\"" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 \"91212129\"" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "part2 digits = solve (length digits `div` 2) digits" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": {}, + "outputs": [], + "source": [ + "part2_extract :: String -> [String]\n", + "part2_extract digits = map (\\ds -> (take 1 ds) ++ (take 1 $ drop offset ds)) \n", + " $ take (length digits) \n", + " $ tails (digits ++ digits)\n", + " where offset = length digits `div` 2" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"12\",\"12\",\"21\",\"21\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"1122\"" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"1122\"" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"11\",\"22\",\"11\",\"22\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"1212\"" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"1212\"" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"12\",\"21\",\"21\",\"12\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"1221\"" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"1221\"" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"14\",\"22\",\"35\",\"41\",\"22\",\"53\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"123425\"" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"123425\"" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"11\",\"22\",\"33\",\"11\",\"22\",\"33\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"123123\"" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"123123\"" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"11\",\"24\",\"11\",\"35\",\"11\",\"42\",\"11\",\"53\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"12131415\"" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"12131415\"" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " digits <- readFile \"../../data/advent01.txt\"\n", + " print $ part1 digits\n", + " print $ part2 digits" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1158\n", + "1132" + ] + }, + "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 +}