AdventOfCode/2020/14_haskell.ipynb

143 lines
3.9 KiB
Text

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"puzzle <- readFile \"14.txt\"\n",
"plines = lines puzzle"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Day 14"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14839536808842"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import qualified Data.Map as Map\n",
"import Data.List (isPrefixOf)\n",
"import Data.List.Split\n",
"\n",
"intToBin :: Int -> Int -> String\n",
"intToBin _ 0 = \"\"\n",
"intToBin num len = intToBin (num `div` 2) (len - 1) ++ show (num `mod` 2)\n",
"\n",
"binToInt :: String -> Int\n",
"binToInt = foldl (\\acc e -> acc * 2 + read (e:\"\")) 0\n",
"\n",
"applyMask :: String -> Int -> Int\n",
"applyMask mask value = binToInt [if m == 'X' then x else m | (m, x) <- zip mask $ intToBin value 36]\n",
"\n",
"handleMask :: [String] -> Map.Map Int Int -> Int\n",
"handleMask (x:xs) = solve xs $ last $ splitOn \" = \" x\n",
"\n",
"handleMem :: [String] -> String -> Map.Map Int Int -> Int\n",
"handleMem (x:xs) mask mem = solve xs mask $ Map.insert a (applyMask mask b) mem\n",
" where [a,b] = map read $ splitOn \"] = \" $ last $ splitOn \"[\" x :: [Int]\n",
"\n",
"solve :: [String] -> String -> Map.Map Int Int -> Int\n",
"solve [] mask mem = sum $ Map.elems mem\n",
"solve line@(x:_) mask mem\n",
" | \"mask\" `isPrefixOf` x = handleMask line mem\n",
" | otherwise = handleMem line mask mem\n",
"\n",
"solve plines (replicate 36 'X') Map.empty"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Puzzle 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4215284199669"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"import Data.Bits (shiftL, testBit)\n",
"\n",
"iterReplace :: [Int] -> String -> Int -> String\n",
"iterReplace floating a i = [Map.findWithDefault x j tr | (j, x) <- zip [0..] a]\n",
" where tr = Map.fromList [(f, if testBit i j then '1' else '0') | (j, f) <- zip [0..] floating]\n",
"\n",
"iter :: String -> [Int]\n",
"iter a = map (binToInt . iterReplace floating a) [0 .. 1 `shiftL` length floating - 1]\n",
" where floating = filter (('X'==).(a!!)) [0..35]\n",
"\n",
"applyMask :: String -> Int -> String\n",
"applyMask mask value = [if m == '0' then x else m | (m, x) <- zip mask $ intToBin value 36]\n",
"\n",
"handleMask :: [String] -> Map.Map Int Int -> Int\n",
"handleMask (x:xs) = solve xs $ last $ splitOn \" = \" x\n",
"\n",
"handleMem :: [String] -> String -> Map.Map Int Int -> Int\n",
"handleMem (x:xs) mask mem = solve xs mask $ foldl (\\acc e -> Map.insert e b acc) mem $ iter $ applyMask mask a\n",
" where [a,b] = map read $ splitOn \"] = \" $ last $ splitOn \"[\" x :: [Int]\n",
"\n",
"solve :: [String] -> String -> Map.Map Int Int -> Int\n",
"solve [] mask mem = sum $ Map.elems mem\n",
"solve line@(x:_) mask mem\n",
" | \"mask\" `isPrefixOf` x = handleMask line mem\n",
" | otherwise = handleMem line mask mem\n",
"\n",
"solve plines (replicate 36 'X') Map.empty"
]
}
],
"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
}