X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=src%2Fadvent04%2Fadvent04.ipynb;fp=src%2Fadvent04%2Fadvent04.ipynb;h=63a54e506894e3bac4e28e788478a951c5875aa5;hb=44f12be1a7004e39cb6e4064a335bcecf7cc728a;hp=0000000000000000000000000000000000000000;hpb=56a472b8d4126a5f146a655662d867d5c32a783a;p=advent-of-code-17.git diff --git a/src/advent04/advent04.ipynb b/src/advent04/advent04.ipynb new file mode 100644 index 0000000..63a54e5 --- /dev/null +++ b/src/advent04/advent04.ipynb @@ -0,0 +1,362 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "{-# LANGUAGE NegativeLiterals #-}\n", + "{-# LANGUAGE FlexibleContexts #-}" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [], + "source": [ + "import Data.List.Split (splitOn)\n", + "import qualified Data.Map.Strict as M\n", + "import Data.Map.Strict ((!))\n", + "import Data.List (sort)" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "main :: IO ()\n", + "main = do \n", + " text <- readFile \"../../data/advent04.txt\"\n", + " let passphrases = map parseLine $ lines text\n", + " print $ take 10 passphrases\n", + " print $ part1 passphrases\n", + " print $ part2 passphrases\n", + "-- print $ take 10 passphrases\n", + "-- part1 triangles\n", + "-- part2 triangles" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "parseLine :: String -> [String]\n", + "parseLine = filter (not . null) . splitOn \" \"" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "frequency :: (Ord a) => [a] -> M.Map a Int \n", + "frequency xs = M.fromListWith (+) [(x, 1) | x <- xs]" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[\"sayndz\",\"zfxlkl\",\"attjtww\",\"cti\",\"sokkmty\",\"brx\",\"fhh\",\"suelqbp\"],[\"xmuf\",\"znkhaes\",\"pggrlp\",\"zia\",\"znkhaes\",\"znkhaes\"],[\"nti\",\"rxr\",\"bogebb\",\"zdwrin\"],[\"sryookh\",\"unrudn\",\"zrkz\",\"jxhrdo\",\"gctlyz\"],[\"bssqn\",\"wbmdc\",\"rigc\",\"zketu\",\"ketichh\",\"enkixg\",\"bmdwc\",\"stnsdf\",\"jnz\",\"mqovwg\",\"ixgken\"],[\"flawt\",\"cpott\",\"xth\",\"ucwgg\",\"xce\",\"jcubx\",\"wvl\",\"qsysa\",\"nlg\"],[\"qovcqn\",\"zxcz\",\"vojsno\",\"nqoqvc\",\"hnf\",\"gqewlkd\",\"uevax\",\"vuna\",\"fxjkbll\",\"vfge\"],[\"qrzf\",\"phwuf\",\"ligf\",\"xgen\",\"vkig\",\"elptd\",\"njdm\",\"gvqiu\",\"epfzsvk\",\"urbltg\",\"dqg\"],[\"sfpku\",\"viwihi\",\"fje\",\"umdkwvi\",\"ejzhzj\",\"qrbl\",\"sfpku\",\"sad\",\"nawnow\",\"ksnku\"],[\"nzhj\",\"mfudick\",\"ueaa\",\"jnhz\",\"kpy\",\"pzk\"]]\n", + "383\n", + "265" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "main" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "fromList [(\"eild\",1),(\"gcne\",1),(\"jxczomh\",1),(\"kebx\",1),(\"meja\",1),(\"nrskdr\",1)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "frequency $ parseLine \"kebx eild nrskdr meja jxczomh gcne\"" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "metadata": {}, + "outputs": [], + "source": [ + "containsDuplicates passphrase = (not . M.null) $ M.filter (> 1) $ frequency passphrase" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [], + "source": [ + "containsDuplicates = (not . M.null) . M.filter (> 1) . frequency" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "containsDuplicates $ parseLine \"kebx eild nrskdr meja jxczomh gcne\"" + ] + }, + { + "cell_type": "code", + "execution_count": 66, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "containsDuplicates $ parseLine \"kebx eild nrskdr meja jxczomh gcne meja\"" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "part1 passphrases = length $ filter (not . containsDuplicates) passphrases" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[[\"sayndz\",\"zfxlkl\",\"attjtww\",\"cti\",\"sokkmty\",\"brx\",\"fhh\",\"suelqbp\"],[\"xmuf\",\"znkhaes\",\"pggrlp\",\"zia\",\"znkhaes\",\"znkhaes\"],[\"nti\",\"rxr\",\"bogebb\",\"zdwrin\"],[\"sryookh\",\"unrudn\",\"zrkz\",\"jxhrdo\",\"gctlyz\"],[\"bssqn\",\"wbmdc\",\"rigc\",\"zketu\",\"ketichh\",\"enkixg\",\"bmdwc\",\"stnsdf\",\"jnz\",\"mqovwg\",\"ixgken\"],[\"flawt\",\"cpott\",\"xth\",\"ucwgg\",\"xce\",\"jcubx\",\"wvl\",\"qsysa\",\"nlg\"],[\"qovcqn\",\"zxcz\",\"vojsno\",\"nqoqvc\",\"hnf\",\"gqewlkd\",\"uevax\",\"vuna\",\"fxjkbll\",\"vfge\"],[\"qrzf\",\"phwuf\",\"ligf\",\"xgen\",\"vkig\",\"elptd\",\"njdm\",\"gvqiu\",\"epfzsvk\",\"urbltg\",\"dqg\"],[\"sfpku\",\"viwihi\",\"fje\",\"umdkwvi\",\"ejzhzj\",\"qrbl\",\"sfpku\",\"sad\",\"nawnow\",\"ksnku\"],[\"nzhj\",\"mfudick\",\"ueaa\",\"jnhz\",\"kpy\",\"pzk\"]]\n", + "383" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "main" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "fromList [('e',1),('h',1),('l',2),('o',1)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "frequency \"hello\"" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "fromList [('e',1),('h',1),('l',2),('o',1)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "frequency \"olleh\"" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "fromList []" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "M.difference (frequency \"hello\") (frequency \"olleh\")" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "fromList []" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "M.difference (frequency \"olleho\") (frequency \"hello\")" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "fromList [('e',1),('h',1),('l',2),('o',1)]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "M.intersection (frequency \"olleh\") (frequency \"oohello\")" + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"ehllo\"" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "sort \"hello\"" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"ehllo\"" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "sort \"olleh\"" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "containsAnagrams passphrase = containsDuplicates $ map sort passphrase" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [], + "source": [ + "part2 passphrases = length $ filter (not . containsAnagrams) passphrases" + ] + }, + { + "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 +}