AdventOfCode/2020/17_haskell.ipynb

150 lines
4 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"puzzle <- readFile \"17.txt\"\n",
"plines = lines puzzle"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Day 17"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"289"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import qualified Data.Set as Set\n",
"import Data.List (nub)\n",
"\n",
"type Cube = (Int, Int, Int)\n",
"type State = Set.Set Cube\n",
"\n",
"adjacent = [(i, j, k) | i <- [-1..1], j <- [-1..1], k <- [-1..1]] :: [Cube]\n",
"\n",
"neighbours :: Cube -> [Cube]\n",
"neighbours (x, y, z) = [(x+i, y+j, z+k) | (i,j,k) <- adjacent]\n",
"\n",
"realNeighbours :: Cube -> [Cube]\n",
"realNeighbours cube = filter (/=cube) $ neighbours cube\n",
"\n",
"countNeighbours :: State -> Cube -> Int\n",
"countNeighbours state cube = length activeNeighbours\n",
" where activeNeighbours = filter (`Set.member` state) $ realNeighbours cube\n",
"\n",
"simulateCube :: State -> Cube -> Bool\n",
"simulateCube state cube\n",
" | active = count `elem` [2,3]\n",
" | otherwise = count == 3\n",
" where active = cube `Set.member` state\n",
" count = countNeighbours state cube\n",
"\n",
"simulate :: Int -> State -> Int\n",
"simulate 0 state = length state\n",
"simulate n state = simulate (n-1) $ Set.fromList newState\n",
" where toUpdate = nub . concatMap neighbours . Set.toList $ state\n",
" newState = filter (simulateCube state) toUpdate\n",
"\n",
"simulate 6 $ Set.fromList [(0, i, j) | (i, line) <- zip [0..] plines, (j, c) <- zip [0..] line, c == '#']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2084"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"type Cube = (Int, Int, Int, Int)\n",
"type State = Set.Set Cube\n",
"\n",
"adjacent = [(h, i, j, k) | h <- [-1..1], i <- [-1..1], j <- [-1..1], k <- [-1..1]] :: [Cube]\n",
"\n",
"union :: Ord a => [Set.Set a] -> Set.Set a\n",
"union = foldl Set.union Set.empty\n",
"\n",
"neighbours :: Cube -> Set.Set Cube\n",
"neighbours (w, x, y, z) = Set.fromList [(w+h, x+i, y+j, z+k) | (h,i,j,k) <- adjacent]\n",
"\n",
"realNeighbours :: Cube -> Set.Set Cube\n",
"realNeighbours cube = Set.filter (/=cube) $ neighbours cube\n",
"\n",
"countNeighbours :: State -> Cube -> Int\n",
"countNeighbours state cube = length activeNeighbours\n",
" where activeNeighbours = Set.filter (`Set.member` state) $ realNeighbours cube\n",
"\n",
"simulateCube :: State -> Cube -> Bool\n",
"simulateCube state cube\n",
" | active = count `elem` [2,3]\n",
" | otherwise = count == 3\n",
" where active = cube `Set.member` state\n",
" count = countNeighbours state cube\n",
"\n",
"simulate :: Int -> State -> Int\n",
"simulate 0 state = length state\n",
"simulate n state = simulate (n-1) . Set.filter (simulateCube state) . union . Set.toList . Set.map neighbours $ state\n",
"\n",
"simulate 6 $ Set.fromList [(0, 0, i, j) | (i, line) <- zip [0..] plines, (j, c) <- zip [0..] line, c == '#']"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
},
"language_info": {
"codemirror_mode": "Haskell",
"file_extension": ".hs",
"mimetype": "text/x-haskell",
"name": "haskell",
"pygments_lexer": "Haskell",
"version": "8.8.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}