--- /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
+}
module Main(main) where
-import Data.List (tails)
+import Data.Char (digitToInt)
main :: IO ()
main = do
print $ part1 digits
print $ part2 digits
-part1 :: String -> Integer
-part1 = sum_valid_pairs . part1_extract
+part1 :: String -> Int
+part1 = solve 1
-part2 :: String -> Integer
-part2 = sum_valid_pairs . part2_extract
+part2 :: String -> Int
+part2 digits = solve (length digits `div` 2) digits
-part1_extract :: String -> [String]
-part1_extract digits = map (take 2) $ tails (digits ++ [head digits])
+-- Verbose version
+-- solve n digits = sum $ map (digitToInt . fst)
+-- $ filter (uncurry (==))
+-- $ zip digits
+-- $ drop n
+-- $ cycle 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
+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
+{-# 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