{ "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.Split (splitOn)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "hexStep :: (Int, Int) -> String -> (Int, Int)\n", "hexStep (n, ne) s = case s of \n", " \"n\" -> (n + 1, ne)\n", " \"ne\" -> (n, ne + 1)\n", " \"nw\" -> (n + 1, ne - 1)\n", " \"s\" -> (n - 1, ne)\n", " \"se\" -> (n - 1, ne + 1)\n", " \"sw\" -> (n, ne - 1)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "hexPath = foldl hexStep (0, 0)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
Redundant bracket
Found:
(abs n) + (abs ne)
Why Not:
abs n + (abs ne)
Redundant bracket
Found:
(abs n) + (abs ne)
Why Not:
(abs n) + abs ne
Redundant bracket
Found:
(abs n) - smallest
Why Not:
abs n - smallest
Redundant bracket
Found:
(abs ne) - smallest
Why Not:
abs ne - smallest
" ], "text/plain": [ "Line 2: Redundant bracket\n", "Found:\n", "(abs n) + (abs ne)\n", "Why not:\n", "abs n + (abs ne)Line 2: Redundant bracket\n", "Found:\n", "(abs n) + (abs ne)\n", "Why not:\n", "(abs n) + abs neLine 5: Redundant bracket\n", "Found:\n", "(abs n) - smallest\n", "Why not:\n", "abs n - smallestLine 5: Redundant bracket\n", "Found:\n", "(abs ne) - smallest\n", "Why not:\n", "abs ne - smallest" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "distance (n, ne) = if n * ne > 0 \n", " then (abs n) + (abs ne)\n", " else smallest + remainder\n", " where smallest = min (abs n) (abs ne)\n", " remainder = max ((abs n) - smallest) ((abs ne) - smallest)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "distance $ hexPath $ splitOn \",\" \"ne,ne,ne\"" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "distance $ hexPath $ splitOn \",\" \"ne,ne,sw,sw\"" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "distance $ hexPath $ splitOn \",\" \"ne,ne,s,s\"" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "distance $ hexPath $ splitOn \",\" \"se,sw,se,sw,sw\"" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "part1 = distance . hexPath . splitOn \",\"" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "main :: IO ()\n", "main = do \n", " text <- readFile \"../../data/advent11.txt\"\n", "-- let instrs = map read $ splitOn \",\" text\n", " print $ part1 text\n", "-- print $ part2 text" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "670" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "main" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "hexPathB = scanl hexStep (0, 0)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "maximum $ map distance $ hexPathB $ splitOn \",\" \"ne,ne,ne\"" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0,0),(0,1),(0,2),(0,3)]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "hexPathB $ splitOn \",\" \"ne,ne,ne\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0,1,2,3]" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "map distance $ hexPathB $ splitOn \",\" \"ne,ne,ne\"" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([(0,0),(0,1),(0,2),(0,3)],[0,1,2,3],3)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pth = splitOn \",\" \"ne,ne,ne\"\n", "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([(0,0),(0,1),(0,2),(0,1),(0,0)],[0,1,2,1,0],2)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pth = splitOn \",\" \"ne,ne,sw,sw\"\n", "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([(0,0),(0,1),(0,2),(-1,2),(-2,2)],[0,1,2,2,2],2)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pth = splitOn \",\" \"ne,ne,s,s\"\n", "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([(0,0),(-1,1),(-1,0),(-2,1),(-2,0),(-2,-1)],[0,1,1,2,2,3],3)" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "pth = splitOn \",\" \"se,sw,se,sw,sw\"\n", "(hexPathB pth, map distance $ hexPathB pth, maximum $ map distance $ hexPathB pth)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "part2 = maximum . map distance . hexPathB . splitOn \",\"" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [], "source": [ "main :: IO ()\n", "main = do \n", " text <- readFile \"../../data/advent11.txt\"\n", "-- let instrs = map read $ splitOn \",\" text\n", " print $ part1 text\n", " print $ part2 text" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "670\n", "1426" ] }, "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 }