9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}"
19 "import qualified Data.Vector as V\n",
20 "import Data.Vector ((//), (!))\n",
21 "import Data.List (unfoldr)\n",
22 "import qualified Data.Map.Strict as M"
31 "type Memory = V.Vector Int\n",
32 "type Redist = (Int, Int, Memory)\n",
33 "type History = M.Map Memory Int"
48 "output_type": "display_data"
52 "vec0 = V.fromList [0, 2, 7, 0]\n",
62 "redistStart :: Memory -> Redist\n",
63 "redistStart vec0 = (current, toDistribute, startVec)\n",
64 " where origin = V.maxIndex vec0\n",
65 " toDistribute = vec0!origin\n",
66 " current = (origin + 1) `mod` (length vec0)\n",
67 " startVec = vec0 // [(origin, 0)]"
82 "output_type": "display_data"
86 "redistStart $ V.fromList [0, 2, 7, 0]"
95 "redistR :: Redist -> Memory\n",
96 "redistR (_, 0, vec) = vec\n",
97 "redistR (i, n, vec) = redistR (i', n', vec')\n",
98 " where n' = n - 1\n",
99 " i' = (i + 1) `mod` (length vec)\n",
100 " vec' = vec // [(i, vec!i + 1)]"
105 "execution_count": 8,
109 "redist = redistR . redistStart"
114 "execution_count": 9,
124 "output_type": "display_data"
128 "redist $ V.fromList [0, 2, 7, 0]"
133 "execution_count": 10,
143 "output_type": "display_data"
147 "redist $ redist $ V.fromList [0, 2, 7, 0]"
152 "execution_count": 11,
156 "redistSeq = unfoldr redistU\n",
157 " where redistU vec = Just (redist vec, redist vec)"
162 "execution_count": 12,
168 "[(0,[2,4,1,2]),(1,[3,1,2,3]),(2,[0,2,3,4]),(3,[1,3,4,1]),(4,[2,4,1,2]),(5,[3,1,2,3]),(6,[0,2,3,4]),(7,[1,3,4,1])]"
172 "output_type": "display_data"
176 "take 8 $ zip [0..] $ redistSeq $ V.fromList [0, 2, 7, 0]"
181 "execution_count": 13,
185 "findRepeat :: History -> [(Int, Memory)] -> Int\n",
186 "findRepeat h ((n, x) : nxs) = if x `M.member` h \n",
188 " else findRepeat (M.insert x n h) nxs\n",
189 " where n0 = (M.!) h x"
194 "execution_count": 26,
198 "findRepeatB :: History -> [(Int, Memory)] -> Int\n",
199 "findRepeatB h ((n, x) : nxs) = if x `M.member` h \n",
201 " else findRepeatB (M.insert x n h) nxs\n",
202 " where n0 = (M.!) h x"
207 "execution_count": 27,
217 "output_type": "display_data"
221 "findRepeat M.empty $ zip [0..] $ redistSeq $ V.fromList [0, 2, 7, 0]"
226 "execution_count": 28,
230 "part1 :: [Int] -> Int\n",
231 "-- part1 memlist = findRepeat M.empty $ zip [0..] $ redistSeq $ V.fromList memlist\n",
232 "part1 = (findRepeat M.empty) . (zip [0..]) . redistSeq . V.fromList"
237 "execution_count": 29,
241 "part2 :: [Int] -> Int\n",
242 "-- part1 memlist = findRepeat M.empty $ zip [0..] $ redistSeq $ V.fromList memlist\n",
243 "part2 = (findRepeatB M.empty) . (zip [0..]) . redistSeq . V.fromList"
248 "execution_count": 30,
254 " text <- readFile \"../../data/advent06.txt\"\n",
255 "-- text0 <- readFile \"../../data/advent06.txt\"\n",
256 "-- let text = \"0 2 7 0\"\n",
257 " let memory = map read $ words text\n",
258 " print $ part1 memory\n",
259 " print $ part2 memory"
264 "execution_count": 31,
275 "output_type": "display_data"
284 "execution_count": null,
292 "display_name": "Haskell",
293 "language": "haskell",
297 "codemirror_mode": "ihaskell",
298 "file_extension": ".hs",