{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "{-# LANGUAGE NegativeLiterals #-}\n", "{-# LANGUAGE FlexibleContexts #-}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "import Data.List.Split (splitOn)\n", "import Data.List (mapAccumL)" ] }, { "cell_type": "code", "execution_count": 4, "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": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(4,[1,3,5,7])" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "mapAccumL (\\a b -> (a + 1, a + b)) 0 [1,2,3,4]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [], "source": [ "hexPath = foldl hexStep (0, 0)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "distance $ hexPath $ splitOn \",\" \"ne,ne,ne\"" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "distance $ hexPath $ splitOn \",\" \"ne,ne,sw,sw\"" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "distance $ hexPath $ splitOn \",\" \"ne,ne,s,s\"" ] }, { "cell_type": "code", "execution_count": 27, "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": 28, "metadata": {}, "outputs": [ { "data": { "text/html": [ "