{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "{-# LANGUAGE NegativeLiterals #-}\n", "{-# LANGUAGE FlexibleContexts #-}\n", "{-# LANGUAGE OverloadedStrings #-}\n", "{-# LANGUAGE TypeFamilies #-}" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import Data.List\n", "import Data.Char" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[\" | \",\" | +--+ \",\" A | C \",\" F---|----E|--+ \",\" | | | D \",\" +B-+ +--+ \",\" \"]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sampleText <- readFile \"sample-maze.txt\"\n", "sample = lines sampleText\n", "print sample" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "type Maze = [String]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "data Direction = Up | Down | Left | Right deriving (Show, Eq)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "data Progress = Progress { row :: Int\n", " , column :: Int\n", " , direction :: Direction\n", " , letters :: String\n", " , stepCount :: Int\n", " } deriving (Show, Eq)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "startProgress :: Maze -> Progress\n", "startProgress maze = Progress {row = 0, column = startCol, direction = Down, letters = \"\", stepCount = 0}\n", " where topRow = maze!!0\n", " startCol = head $ elemIndices '|' topRow" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "delta :: Direction -> (Int, Int)\n", "delta Up = (-1, 0)\n", "delta Down = ( 1, 0)\n", "delta Left = ( 0, -1)\n", "delta Right = ( 0, 1)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "isContinuation '|' = True\n", "isContinuation '-' = True\n", "isContinuation _ = False\n", "\n", "isJunction '+' = True\n", "isJunction _ = False " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "location :: Maze -> Int -> Int -> Char\n", "location maze r c = (maze!!r)!!c" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "newDirection :: Maze -> Progress -> Direction\n", "newDirection maze progress = \n", " if d == Up || d == Down \n", " then if isSpace leftChar then Right else Left\n", " else if isSpace upChar then Down else Up\n", " where d = direction progress\n", " r = row progress\n", " c = column progress\n", " upChar = location maze (r - 1) c\n", "-- downChar = location maze (r + 1) c\n", " leftChar = location maze r (c - 1)\n", "-- rightChar = location maze r (c + 1)\n", " " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "step :: Maze -> Progress -> Progress\n", "step maze progress = progress {row = r', column = c', direction = d', letters = l', stepCount = sc'}\n", " where r = row progress\n", " c = column progress\n", " thisChar = location maze r c\n", " l' = if isAlpha thisChar then (letters progress) ++ [thisChar] else letters progress\n", " d' = if isJunction thisChar then newDirection maze progress else direction progress \n", " (dr, dc) = delta d'\n", " r' = r + dr\n", " c' = c + dc\n", " sc' = stepCount progress + 1" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "\n", "isFinished :: Maze -> Progress -> Bool\n", "isFinished maze progress = isSpace $ location maze (row progress) (column progress)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "navigate' maze progress = \n", " if isFinished maze progress \n", " then progress\n", " else navigate' maze (step maze progress)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "navigate :: Maze -> Progress\n", "navigate maze = navigate' maze progress\n", " where progress = startProgress maze" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Progress {row = 3, column = 0, direction = Left, letters = \"ABCDEF\", stepCount = 38}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "navigate sample" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'+'" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "sample!!5!!8" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "isJunction '|'" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Progress {row = 5, column = 8, direction = Right, letters = \"\", stepCount = 0}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pt = (startProgress sample) {row = 5, column = 8, direction = Right}\n", "pt" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Up" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "newDirection sample pt" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Progress {row = 4, column = 8, direction = Up, letters = \"\", stepCount = 1}" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "step sample pt" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "part1 = letters " ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "part2 = stepCount" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "main :: IO ()\n", "main = do \n", " text <- readFile \"../../data/advent19.txt\"\n", " let maze = lines text\n", " let progress = navigate maze\n", " print $ part1 progress\n", " print $ part2 progress" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"XYFDJNRCQA\"\n", "17450" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "main" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "navigate " ] } ], "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 }