--- /dev/null
+import Distribution.Simple
+main = defaultMain
--- /dev/null
+name: adventofcode17
+version: 0.1.0.0
+-- synopsis:
+-- description:
+homepage: https://github.com/neilnjae/advent-of-code-17#readme
+license: BSD3
+license-file: LICENSE
+author: Neil Smith
+maintainer: noone@njae.me.uk
+copyright: 2017 Neil Smith
+category: None
+build-type: Simple
+cabal-version: >=1.10
+extra-source-files: README.md
+
+library
+ hs-source-dirs: src
+ build-depends: base >= 4.7 && < 5
+ default-language: Haskell2010
+
+executable advent01
+ hs-source-dirs: src/advent01
+ main-is: advent01.hs
+ default-language: Haskell2010
+ build-depends: base >= 4.7 && < 5
+ , parsec
+
+executable advent02
+ hs-source-dirs: src/advent02
+ main-is: advent02.hs
+ default-language: Haskell2010
+ build-depends: base >= 4.7 && < 5
+ , parsec
+ , parsec-numbers
+++ /dev/null
-# adventofcode1701
+++ /dev/null
-import Distribution.Simple
-main = defaultMain
+++ /dev/null
-name: adventofcode1701
-version: 0.1.0.0
--- synopsis:
--- description:
-homepage: https://github.com/neilnjae/advent-of-code-17#readme
-license: BSD3
-license-file: LICENSE
-author: Neil Smith
-maintainer: noone@njae.me.uk
-copyright: 2017 Neil Smith
-category: None
-build-type: Simple
-cabal-version: >=1.10
-extra-source-files: README.md
-
-library
- hs-source-dirs: src
- build-depends: base >= 4.7 && < 5
- default-language: Haskell2010
-
-executable advent01
- hs-source-dirs: app
- main-is: advent01.hs
- default-language: Haskell2010
- build-depends: base >= 4.7 && < 5
- , parsec
\ No newline at end of file
+++ /dev/null
-{
- "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
-}
+++ /dev/null
-{-# LANGUAGE NegativeLiterals #-}
-
-module Main(main) where
-
-import Data.Char (digitToInt)
-
-main :: IO ()
-main = do
- digits <- readFile "data/advent01.txt"
- print $ part1 digits
- print $ part2 digits
-
-part1 :: String -> Int
-part1 = solve 1
-
-part2 :: String -> Int
-part2 digits = solve (length digits `div` 2) digits
-
--- Verbose version
--- solve n digits = sum $ map (digitToInt . fst)
--- $ filter (uncurry (==))
--- $ zip digits
--- $ drop n
--- $ cycle digits
-
-solve :: Int -> String -> Int
-solve n digits = sum $ zipWith (\a b -> if a == b then digitToInt a else 0) digits
- $ drop n
- $ cycle digits
+++ /dev/null
-{
- "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 Data.List (tails)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [],
- "source": [
- "digits = \"1122\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {},
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": 50,
- "metadata": {},
- "outputs": [],
- "source": [
- "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": 91,
- "metadata": {},
- "outputs": [],
- "source": [
- "part2 :: String -> Integer \n",
- "part2 = sum_valid_pairs . part2_extract"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 92,
- "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": 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": 101,
- "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": 102,
- "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
-}
+++ /dev/null
-{-# LANGUAGE NegativeLiterals #-}
-
-module Main(main) where
-
-import Data.List (tails)
-
-main :: IO ()
-main = do
- digits <- readFile "data/advent01.txt"
- print $ part1 digits
- print $ part2 digits
-
-part1 :: String -> Integer
-part1 = sum_valid_pairs . part1_extract
-
-part2 :: String -> Integer
-part2 = sum_valid_pairs . part2_extract
-
-part1_extract :: String -> [String]
-part1_extract digits = map (take 2) $ tails (digits ++ [head digits])
-
-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
-
-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
+++ /dev/null
-module Main where
-
-main :: IO ()
-main = do
- putStrLn "hello world"
+++ /dev/null
-# adventofcode1701
+++ /dev/null
-import Distribution.Simple
-main = defaultMain
+++ /dev/null
-name: adventofcode1702
-version: 0.1.0.0
--- synopsis:
--- description:
-homepage: https://github.com/neilnjae/advent-of-code-17#readme
-license: BSD3
-license-file: LICENSE
-author: Neil Smith
-maintainer: noone@njae.me.uk
-copyright: 2017 Neil Smith
-category: None
-build-type: Simple
-cabal-version: >=1.10
-extra-source-files: README.md
-
-library
- hs-source-dirs: src
- build-depends: base >= 4.7 && < 5
- default-language: Haskell2010
-
-executable advent02
- hs-source-dirs: app
- main-is: advent02.hs
- default-language: Haskell2010
- build-depends: base >= 4.7 && < 5
- , parsec
- , parsec-numbers
+++ /dev/null
-module Main(main) where
-
-import Text.Parsec
-import Text.ParserCombinators.Parsec.Number
-
-
-main :: IO ()
-main = do
- text <- readFile "data/advent02.txt"
- let sheet = successfulParse $ parseFile text
- print $ part1 sheet
- print $ part2 sheet
-
-
-part1 :: [[Int]] -> Int
-part1 = sum . map p1cSum
-
-part2 :: [[Int]] -> Int
-part2 = sum . map p2cSum
-
-
-p1cSum :: [Int] -> Int
-p1cSum row = (maximum row) - (minimum row)
-
-p2cSum :: [Int] -> Int
-p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]
-
-
-
-sFile = sLine `sepEndBy` newline
-sLine = int `sepBy` onlySpaces
-
-onlySpaces = many (oneOf " \t")
-
-parseFile :: String -> Either ParseError [[Int]]
-parseFile input = parse sFile "(unknown)" input
-
-parseLine :: String -> Either ParseError [Int]
-parseLine input = parse sLine "(unknown)" input
-
-successfulParse :: Either ParseError [a] -> [a]
-successfulParse (Left _) = []
-successfulParse (Right a) = a
-
+++ /dev/null
-{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "{-# LANGUAGE NegativeLiterals #-}\n",
- "{-# LANGUAGE FlexibleContexts #-}"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [],
- "source": [
- "import Text.Parsec \n",
- "import Text.ParserCombinators.Parsec.Number"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [],
- "source": [
- "sFile = sLine `sepEndBy` newline \n",
- "sLine = int `sepBy` onlySpaces"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [],
- "source": [
- "onlySpaces = many (oneOf \" \\t\")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "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": 65,
- "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": 66,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "39126\n",
- "258"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- }
- ],
- "source": [
- "main"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "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": 28,
- "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": 32,
- "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": 37,
- "metadata": {},
- "outputs": [],
- "source": [
- "cSum :: [Int] -> Int\n",
- "cSum row = (maximum row) - (minimum row)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 42,
- "metadata": {},
- "outputs": [],
- "source": [
- "check :: [[Int]] -> Int\n",
- "check = sum . (map cSum)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 43,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "2"
- ]
- },
- "metadata": {},
- "output_type": "display_data"
- }
- ],
- "source": [
- "check $ successfulParse $ parseFile \"1 2\\n8 9\""
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 45,
- "metadata": {},
- "outputs": [],
- "source": [
- "part1 = check"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 54,
- "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": 51,
- "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": null,
- "metadata": {},
- "outputs": [],
- "source": []
- },
- {
- "cell_type": "code",
- "execution_count": 55,
- "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": 60,
- "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": 61,
- "metadata": {},
- "outputs": [],
- "source": [
- "p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 64,
- "metadata": {},
- "outputs": [],
- "source": [
- "part2 = sum . map p2cSum"
- ]
- },
- {
- "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
-}
+++ /dev/null
-module Main where
-
-main :: IO ()
-main = do
- putStrLn "hello world"
--- /dev/null
+{
+ "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
+}
--- /dev/null
+{-# LANGUAGE NegativeLiterals #-}
+
+module Main(main) where
+
+import Data.Char (digitToInt)
+
+main :: IO ()
+main = do
+ digits <- readFile "data/advent01.txt"
+ print $ part1 digits
+ print $ part2 digits
+
+part1 :: String -> Int
+part1 = solve 1
+
+part2 :: String -> Int
+part2 digits = solve (length digits `div` 2) digits
+
+-- Verbose version
+-- solve n digits = sum $ map (digitToInt . fst)
+-- $ filter (uncurry (==))
+-- $ zip digits
+-- $ drop n
+-- $ cycle digits
+
+solve :: Int -> String -> Int
+solve n digits = sum $ zipWith (\a b -> if a == b then digitToInt a else 0) digits
+ $ drop n
+ $ cycle digits
--- /dev/null
+{
+ "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 Data.List (tails)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "digits = \"1122\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": []
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "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": 5,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "part1_extract :: String -> [String] \n",
+ "part1_extract digits = map (take 2) $ tails (digits ++ [head digits])"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "part1 :: String -> Integer \n",
+ "part1 = sum_valid_pairs . part1_extract"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "3"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part1 \"1122\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "4"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part1 \"1111\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part1 \"1234\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "9"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part1 \"91212129\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "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": 12,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "part2 :: String -> Integer \n",
+ "part2 = sum_valid_pairs . part2_extract"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[\"12\",\"12\",\"21\",\"21\"]"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2_extract \"1122\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2 \"1122\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[\"11\",\"22\",\"11\",\"22\"]"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2_extract \"1212\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "6"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2 \"1212\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[\"12\",\"21\",\"21\",\"12\"]"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2_extract \"1221\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "0"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2 \"1221\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "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": 20,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "4"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2 \"123425\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "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": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "12"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2 \"123123\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "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": 24,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "4"
+ ]
+ },
+ "metadata": {},
+ "output_type": "display_data"
+ }
+ ],
+ "source": [
+ "part2 \"12131415\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "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": 26,
+ "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
+}
--- /dev/null
+{-# LANGUAGE NegativeLiterals #-}
+
+module Main(main) where
+
+import Data.List (tails)
+
+main :: IO ()
+main = do
+ digits <- readFile "data/advent01.txt"
+ print $ part1 digits
+ print $ part2 digits
+
+part1 :: String -> Integer
+part1 = sum_valid_pairs . part1_extract
+
+part2 :: String -> Integer
+part2 = sum_valid_pairs . part2_extract
+
+part1_extract :: String -> [String]
+part1_extract digits = map (take 2) $ tails (digits ++ [head digits])
+
+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
+
+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
--- /dev/null
+module Main(main) where
+
+import Text.Parsec
+import Text.ParserCombinators.Parsec.Number
+
+
+main :: IO ()
+main = do
+ text <- readFile "data/advent02.txt"
+ let sheet = successfulParse $ parseFile text
+ print $ part1 sheet
+ print $ part2 sheet
+
+
+part1 :: [[Int]] -> Int
+part1 = sum . map p1cSum
+
+part2 :: [[Int]] -> Int
+part2 = sum . map p2cSum
+
+
+p1cSum :: [Int] -> Int
+p1cSum row = (maximum row) - (minimum row)
+
+p2cSum :: [Int] -> Int
+p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]
+
+
+
+sFile = sLine `sepEndBy` newline
+sLine = int `sepBy` onlySpaces
+
+onlySpaces = many (oneOf " \t")
+
+parseFile :: String -> Either ParseError [[Int]]
+parseFile input = parse sFile "(unknown)" input
+
+parseLine :: String -> Either ParseError [Int]
+parseLine input = parse sLine "(unknown)" input
+
+successfulParse :: Either ParseError [a] -> [a]
+successfulParse (Left _) = []
+successfulParse (Right a) = a
+
--- /dev/null
+{
+ "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
+}
ghc-options:
! '*': -O2 -Wall -Wno-missing-signatures -threaded -rtsopts -with-rtsopts=-N
packages:
-- adventofcode1701/
-- adventofcode1702/
extra-deps:
- parsec-numbers-0.1.0
resolver: lts-9.14