"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": {},