From 765da598a39a2c2b93102c182272b5546f4b6996 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 1 Dec 2017 08:38:12 +0000 Subject: [PATCH] Day 1 done --- adventofcode1701/app/advent01.hs | 49 ++-- adventofcode1701/app/advent01.ipynb | 384 +++++++++++++++++++++++++--- 2 files changed, 374 insertions(+), 59 deletions(-) diff --git a/adventofcode1701/app/advent01.hs b/adventofcode1701/app/advent01.hs index dc63f9c..d56fdbd 100644 --- a/adventofcode1701/app/advent01.hs +++ b/adventofcode1701/app/advent01.hs @@ -2,41 +2,30 @@ module Main(main) where -import Text.Parsec hiding (State) --- import Text.ParserCombinators.Parsec.Number - - +import Data.List (tails) main :: IO () main = do - text <- readFile "data/advent01.txt" - let instructions = successfulParse $ parseIline text - part1 instructions - part2 instructions - -part1 :: [Int] -> IO () -part1 instructions = do - print $ sum instructions - -part2 :: [Int] -> IO () -part2 instructions = do - print $ length $ takeWhile (>= 0) $ scanl (+) 0 instructions - - - --- instructionFile = instructionLine `endBy` newline -instructionLine = many (up <|> down) + digits <- readFile "data/advent01.txt" + print $ part1 digits + print $ part2 digits +part1 :: String -> Integer +part1 = sum_valid_pairs . part1_extract -up = char '(' *> pure 1 -down = char ')' *> pure -1 +part2 :: String -> Integer +part2 = sum_valid_pairs . part2_extract --- parseIfile :: String -> Either ParseError [[Int]] --- parseIfile input = parse instructionFile "(unknown)" input +part1_extract :: String -> [String] +part1_extract digits = map (take 2) $ tails (digits ++ [head digits]) -parseIline :: String -> Either ParseError [Int] -parseIline input = parse instructionLine "(unknown)" input +part2_extract :: String -> [String] +part2_extract digits = map (\ds -> (take 1 ds) ++ (take 1 $ drop offset ds)) + $ take (length digits) + $ tails (digits ++ digits) + where offset = length digits `div` 2 -successfulParse :: Either ParseError [a] -> [a] -successfulParse (Left _) = [] -successfulParse (Right a) = a +sum_valid_pairs :: [String] -> Integer +sum_valid_pairs possibles = sum $ map (read . take 1) + $ filter (\(x:y:_) -> x == y) + $ filter (\p -> length p == 2) possibles \ No newline at end of file diff --git a/adventofcode1701/app/advent01.ipynb b/adventofcode1701/app/advent01.ipynb index 4fb001c..cd6ab10 100644 --- a/adventofcode1701/app/advent01.ipynb +++ b/adventofcode1701/app/advent01.ipynb @@ -16,81 +16,407 @@ "metadata": {}, "outputs": [], "source": [ - "import Text.Parsec hiding (State)" + "import Data.List (tails)" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "instructionLine = many (up <|> down)\n", - "\n", - "up = char '(' *> pure 1\n", - "down = char ')' *> pure -1" + "digits = \"1122\"" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 50, "metadata": {}, "outputs": [], "source": [ - "parseIline :: String -> Either ParseError [Int]\n", - "parseIline input = parse instructionLine \"(unknown)\" input\n", - "\n", - "successfulParse :: Either ParseError [a] -> [a]\n", - "successfulParse (Left _) = []\n", - "successfulParse (Right a) = a" + "sum_valid_pairs :: [String] -> Integer\n", + "sum_valid_pairs possibles = sum $ map (read . take 1) \n", + " $ filter (\\(x:y:_) -> x == y) \n", + " $ filter (\\p -> length p == 2) possibles" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "metadata": {}, + "outputs": [], + "source": [ + "part1_extract :: String -> [String] \n", + "part1_extract digits = map (take 2) $ tails (digits ++ [head digits])" + ] + }, + { + "cell_type": "code", + "execution_count": 86, + "metadata": {}, + "outputs": [], + "source": [ + "part1 :: String -> Integer \n", + "part1 = sum_valid_pairs . part1_extract" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 \"1122\"" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 \"1111\"" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 \"1234\"" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 \"91212129\"" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 91, "metadata": {}, "outputs": [], "source": [ - "part1 :: [Int] -> IO ()\n", - "part1 instructions = do\n", - " print $ sum instructions" + "part2 :: String -> Integer \n", + "part2 = sum_valid_pairs . part2_extract" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 92, "metadata": {}, "outputs": [], "source": [ - "part2 :: [Int] -> IO ()\n", - "part2 instructions = do\n", - " print $ length $ takeWhile (>= 0) $ scanl (+) 0 instructions" + "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": 93, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"12\",\"12\",\"21\",\"21\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"1122\"" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"1122\"" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"11\",\"22\",\"11\",\"22\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"1212\"" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"1212\"" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[\"12\",\"21\",\"21\",\"12\"]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2_extract \"1221\"" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"1221\"" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "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": 100, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"123425\"" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "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": 77, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"123123\"" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "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": 78, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 \"12131415\"" ] }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "main :: IO ()\n", "main = do \n", - " text <- readFile \"../../data/advent01.txt\"\n", - " let instructions = successfulParse $ parseIline text\n", - " part1 instructions\n", - " part2 instructions" + " digits <- readFile \"../../data/advent01.txt\"\n", + " print $ part1 digits\n", + " print $ part2 digits" ] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "138\n", - "1771" + "1158\n", + "1132" ] }, "metadata": {}, -- 2.34.1