From 9ce2b21f9734aa6e51186d2cc0075083a5864155 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Mon, 18 Dec 2017 22:42:34 +0000 Subject: [PATCH] Day 17 done --- advent-of-code.cabal | 41 +- src/advent17/advent17.hs | 72 +++ src/advent17/advent17.ipynb | 895 ++++++++++++++++++++++++++++++++++ src/advent17/advent17leaky.hs | 47 ++ 4 files changed, 1053 insertions(+), 2 deletions(-) create mode 100644 src/advent17/advent17.hs create mode 100644 src/advent17/advent17.ipynb create mode 100644 src/advent17/advent17leaky.hs diff --git a/advent-of-code.cabal b/advent-of-code.cabal index de95bf6..05e1ef7 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -14,9 +14,13 @@ cabal-version: >=1.10 extra-source-files: README.md library - hs-source-dirs: src - build-depends: base >= 4.7 && < 5 + exposed-modules: Advent18Parser + hs-source-dirs: src, src/advent18 default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , text + , megaparsec + executable advent01 hs-source-dirs: src/advent01 @@ -166,3 +170,36 @@ executable advent16 , megaparsec , vector +executable advent17 + hs-source-dirs: src/advent17 + main-is: advent17.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , vector + +executable advent17leaky + hs-source-dirs: src/advent17 + main-is: advent17leaky.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , vector + +executable advent18a + hs-source-dirs: src/advent18 + main-is: advent18a.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , containers + , mtl + , text + , megaparsec + +executable advent18b + hs-source-dirs: src/advent18 + main-is: advent18b.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , containers + , mtl + , text + , megaparsec \ No newline at end of file diff --git a/src/advent17/advent17.hs b/src/advent17/advent17.hs new file mode 100644 index 0000000..edbfd85 --- /dev/null +++ b/src/advent17/advent17.hs @@ -0,0 +1,72 @@ +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE BangPatterns #-} + +-- import Prelude hiding ((++)) +import Data.Vector.Unboxed ((!)) +import qualified Data.Vector.Unboxed as V +import Data.List (foldl') + +type RingBuffer = V.Vector Int + +initialStepSize :: Int +initialStepSize = 366 + +initialRingBuffer :: RingBuffer +initialRingBuffer = V.singleton 0 + + +main :: IO () +main = do + print $ part1 2017 + -- print $ part2 50000000 + print $ ith 2017 366 + print $ oneth 50000000 366 + + + +part1 :: Int -> Int +part1 k = (!1) $ (iterate updateRingBuffer initialRingBuffer)!!k + +part2 :: Int -> Int +part2 k = finalBuffer!targetLoc + where finalBuffer = (iterate updateRingBuffer initialRingBuffer)!!k + zeroLoc = V.head $ V.elemIndices 0 finalBuffer + targetLoc = (zeroLoc + 1) `rem` (V.length finalBuffer) + +updateRingBuffer :: RingBuffer -> RingBuffer +updateRingBuffer buffer = buffer' + where nextPos = (initialStepSize + 1) `rem` V.length buffer + (start, end) = V.splitAt nextPos buffer + nextValue = V.length buffer + buffer' = V.cons nextValue $ (V.++) end start + + + +(%) = mod + +ith :: Int -> Int -> Int +ith i steps = + let (position, list) = foldl' (\(currPos, currList) n -> + let newPos = (currPos + steps % n + 1) % n + in (newPos, take newPos currList ++ [n] ++ drop newPos currList)) + (0, [0]) [1..i] + in list !! (position + 1) + +oneth :: Int -> Int -> Int +oneth i steps = + snd $ foldl' (\(currPos, currOneth) n -> + let !newPos = (currPos + steps % n + 1) % n + in (newPos, if newPos == 0 then n else currOneth)) + (0, 1) [1..i] + +-- main :: IO () +-- main = do +-- print $ ith 2017 312 +-- print $ oneth 50000000 312 \ No newline at end of file diff --git a/src/advent17/advent17.ipynb b/src/advent17/advent17.ipynb new file mode 100644 index 0000000..ac61e31 --- /dev/null +++ b/src/advent17/advent17.ipynb @@ -0,0 +1,895 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "{-# LANGUAGE NegativeLiterals #-}\n", + "{-# LANGUAGE FlexibleContexts #-}\n", + "{-# LANGUAGE OverloadedStrings #-}\n", + "{-# LANGUAGE TypeFamilies #-}" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import Prelude hiding ((++))\n", + "import Data.Vector.Unboxed ((!), (++), (//))\n", + "import qualified Data.Vector.Unboxed as V\n", + "import qualified Data.List as L" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "type RingBuffer = V.Vector Int" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "initialStepSize = 366\n", + "\n", + "initialRingBuffer :: RingBuffer\n", + "initialRingBuffer = V.singleton 0" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "updateRingBufferInc nextValue stepSize buffer = (buffer', stepSize + 1)\n", + " where nextPos = (stepSize + 1) `rem` V.length buffer\n", + " (start, end) = V.splitAt nextPos buffer\n", + " buffer' = V.cons nextValue $ end ++ start" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "updateRingBuffer nextValue stepSize buffer = (buffer', stepSize)\n", + " where nextPos = (stepSize + 1) `rem` V.length buffer\n", + " (start, end) = V.splitAt nextPos buffer\n", + " buffer' = V.cons nextValue $ end ++ start" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([1,0],0)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "updateRingBuffer 1 0 initialRingBuffer" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "<interactive>:1:1: error:
• Variable not in scope: updateRingBufferE :: Integer -> Integer -> RingBuffer -> t
• Perhaps you meant one of these: ‘updateRingBuffer’ (line 1), ‘updateRingBufferInc’ (line 1)
" + ], + "text/plain": [ + ":1:1: error:\n", + " • Variable not in scope: updateRingBufferE :: Integer -> Integer -> RingBuffer -> t\n", + " • Perhaps you meant one of these: ‘updateRingBuffer’ (line 1), ‘updateRingBufferInc’ (line 1)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "updateRingBufferE 1 0 initialRingBuffer" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "initialRingBuffer" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "ringBufferUnfolder :: (Int, Int, RingBuffer) -> Maybe (RingBuffer, (Int, Int, RingBuffer))\n", + "ringBufferUnfolder (nextValue, stepSize, buffer) = Just (buffer', (nextValue + 1, stepSize', buffer'))\n", + " where (buffer', stepSize') = updateRingBuffer nextValue stepSize buffer" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1025" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "(!1) $ last $ take 2017 $ L.unfoldr ringBufferUnfolder (1, 366, initialRingBuffer)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[0,1,2,3,4,5,6,7,8]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "t = V.fromList [0,1,2,3,4,5,6,7,8] :: RingBuffer\n", + "t" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([9,4,5,6,7,8,0,1,2,3],3)" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "updateRingBuffer 9 3 t" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "updateRingBufferE nextValue _stepSize buffer = (buffer', 3)\n", + " where nextPos = (3 + 1) `rem` V.length buffer\n", + " (start, end) = V.splitAt nextPos buffer\n", + " buffer' = V.cons nextValue $ end ++ start" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "ringBufferUnfolderE :: (Int, Int, RingBuffer) -> Maybe (RingBuffer, (Int, Int, RingBuffer))\n", + "ringBufferUnfolderE (nextValue, stepSize, buffer) = Just (buffer', (nextValue + 1, stepSize', buffer'))\n", + " where (buffer', stepSize') = updateRingBufferE nextValue stepSize buffer" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[2017,638,1513,851,1135,269,1514,359,479,1136,1515,852,639,11,1516,1137,202,853,1517,85,1138,640,1518,480,854,1139,1519,360,270,641,1520,1140,855,20,1521,481,1141,152,1522,856,642,1142,1523,361,203,857,1524,1143,482,643,1525,271,1144,858,1526,114,362,1145,1527,644,859,483,1528,1146,64,48,1529,860,1147,645,1530,272,484,1148,1531,861,363,646,1532,1149,204,862,1533,153,1150,485,1534,647,863,1151,1535,86,364,273,1536,1152,864,648,1537,486,1153,115,1538,865,205,1154,1539,649,365,866,1540,1155,487,36,1541,650,1156,867,1542,274,154,1157,1543,488,868,651,1544,1158,366,27,1545,869,1159,206,1546,652,489,1160,1547,870,275,367,1548,1161,653,871,1549,65,1162,490,1550,116,872,1163,1551,654,155,368,1552,1164,873,491,1553,655,1165,276,1554,874,207,1166,1555,87,656,875,1556,1167,492,369,1557,15,1168,876,1558,657,277,1169,1559,493,877,49,1560,1170,658,370,1561,878,1171,208,1562,494,659,1172,1563,879,156,278,1564,1173,371,880,1565,660,1174,495,1566,117,881,1175,1567,6,661,209,1568,1176,882,496,1569,372,1177,662,1570,883,279,1178,1571,88,497,884,1572,1179,663,157,1573,373,1180,885,1574,66,664,1181,1575,498,886,280,1576,1182,210,665,1577,887,1183,374,1578,499,118,1184,1579,888,666,37,1580,1185,281,889,1581,500,1186,667,1582,375,890,1187,1583,158,211,668,1584,1188,891,501,1585,1,1189,376,1586,892,669,1190,1587,282,502,893,1588,1191,89,670,1589,119,1192,894,1590,377,503,1193,1591,671,895,212,1592,1194,283,159,1593,896,1195,672,1594,504,378,1196,1595,897,50,673,1596,1197,67,898,1597,505,1198,284,1598,674,899,1199,1599,379,213,506,1600,1200,900,675,1601,28,1201,160,1602,901,380,1202,1603,676,507,902,1604,1203,285,120,1605,677,1204,903,1606,214,508,1205,1607,381,904,678,1608,1206,90,286,1609,905,1207,509,1610,679,21,1208,1611,906,382,161,1612,1209,680,907,1613,510,1210,215,1614,287,908,1211,1615,681,383,511,1616,1212,909,38,1617,682,1213,121,1618,910,68,1214,1619,512,683,911,1620,1215,384,288,1621,216,1216,912,1622,684,513,1217,1623,162,913,385,1624,1218,685,91,1625,914,1219,514,1626,289,686,1220,1627,915,51,386,1628,1221,515,916,1629,687,1222,217,1630,122,917,1223,1631,290,688,516,1632,1224,918,387,1633,163,1225,689,1634,919,0,1226,1635,517,218,920,1636,1227,690,388,1637,291,1228,921,1638,518,691,1229,1639,16,922,92,1640,1230,389,692,1641,923,1231,519,1642,164,292,1232,1643,924,693,219,1644,1233,520,925,1645,390,1234,694,1646,123,926,1235,1647,69,521,695,1648,1236,927,293,1649,391,1237,29,1650,928,696,1238,1651,522,220,929,1652,1239,165,697,1653,392,1240,930,1654,523,294,1241,1655,698,931,52,1656,1242,124,524,1657,932,1243,699,1658,393,221,1244,1659,933,295,700,1660,1245,525,934,1661,93,1246,394,1662,701,935,1247,1663,166,526,39,1664,1248,936,702,1665,296,1249,395,1666,937,527,1250,1667,703,222,938,1668,1251,70,125,1669,704,1252,939,1670,528,396,1253,1671,297,940,705,1672,1254,167,529,1673,941,1255,223,1674,706,397,1256,1675,942,12,530,1676,1257,707,943,1677,298,1258,94,1678,398,944,1259,1679,708,531,22,1680,1260,945,224,1681,709,1261,299,1682,946,532,1262,1683,399,710,947,1684,1263,168,126,1685,533,1264,948,1686,711,53,1265,1687,400,949,300,1688,1266,712,534,1689,950,1267,225,1690,71,713,1268,1691,951,401,535,1692,1269,169,952,1693,714,1270,301,1694,95,953,1271,1695,536,715,402,1696,1272,954,226,1697,127,1273,716,1698,955,537,1274,1699,302,403,956,1700,1275,717,9,1701,538,1276,957,1702,170,718,1277,1703,227,958,404,1704,1278,539,719,1705,959,1279,303,1706,40,30,1280,1707,960,720,540,1708,1281,405,961,1709,128,1282,721,1710,304,962,1283,1711,541,228,722,1712,1284,963,406,1713,171,1285,542,1714,964,723,1286,1715,96,305,965,1716,1287,407,724,1717,543,1288,966,1718,72,229,1289,1719,725,967,54,1720,1290,544,408,1721,968,1291,726,1722,306,172,1292,1723,969,545,727,1724,1293,129,970,1725,409,1294,230,1726,728,971,1295,1727,546,307,5,1728,1296,972,729,1729,410,1297,547,1730,973,97,1298,1731,730,173,974,1732,1299,308,548,1733,731,1300,975,1734,411,231,1301,1735,17,976,732,1736,1302,549,130,1737,977,1303,412,1738,733,309,1304,1739,978,550,73,1740,1305,734,979,1741,232,1306,413,1742,551,980,1307,1743,735,174,310,1744,1308,981,41,1745,736,1309,552,1746,982,414,1310,1747,98,737,983,1748,1311,233,553,1749,311,1312,984,1750,738,415,1313,1751,131,985,554,1752,1314,739,175,1753,986,1315,55,1754,416,740,1316,1755,987,555,312,1756,1317,234,988,1757,741,1318,23,1758,556,989,1319,1759,417,742,74,1760,1320,990,313,1761,557,1321,743,1762,991,176,1322,1763,418,235,992,1764,1323,744,558,1765,132,1324,993,1766,314,745,1325,1767,419,994,559,1768,1326,99,746,1769,995,1327,31,1770,236,560,1328,1771,996,747,420,1772,1329,315,997,1773,177,1330,748,1774,561,998,1331,1775,7,421,749,1776,1332,999,133,1777,562,1333,316,1778,1000,750,1334,1779,237,422,1001,1780,1335,563,751,1781,56,1336,1002,1782,178,317,1337,1783,752,1003,564,1784,1338,423,100,1785,1004,1339,753,1786,238,565,1340,1787,1005,75,754,1788,1341,424,1006,1789,318,1342,566,1790,755,1007,1343,1791,134,179,425,1792,1344,1008,756,1793,567,1345,239,1794,1009,319,1346,1795,757,42,1010,1796,1347,568,426,1797,758,1348,1011,1798,13,101,1349,1799,569,1012,759,1800,1350,320,427,1801,1013,1351,240,1802,760,570,1352,1803,1014,180,135,1804,1353,761,1015,1805,428,1354,571,1806,321,1016,1355,1807,762,2,241,1808,1356,1017,572,1809,763,1357,429,1810,1018,76,1358,1811,322,764,1019,1812,1359,573,181,1813,430,1360,1020,1814,765,57,1361,1815,574,1021,242,1816,1362,766,323,1817,1022,1363,431,1818,575,767,1364,1819,1023,136,102,1820,1365,182,1024,1821,768,1366,576,1822,432,1025,1367,1823,324,769,243,1824,1368,1026,577,1825,32,1369,770,1826,1027,433,1370,1827,43,578,1028,1828,1371,771,325,1829,137,1372,1029,1830,434,772,1373,1831,579,1030,244,1832,1374,183,773,1833,1031,1375,326,1834,580,435,1376,1835,1032,774,77,1836,1377,103,1033,1837,581,1378,775,1838,245,1034,1379,1839,436,327,776,1840,1380,1035,582,1841,24,1381,184,1842,1036,777,1382,1843,437,583,1037,1844,1383,138,778,1845,328,1384,1038,1846,246,584,1385,1847,779,1039,438,1848,1386,58,18,1849,1040,1387,780,1850,585,329,1388,1851,1041,439,781,1852,1389,185,1042,1853,586,1390,247,1854,782,1043,1391,1855,104,440,587,1856,1392,1044,783,1857,330,1393,139,1858,1045,78,1394,1859,784,588,1046,1860,1395,441,248,1861,785,1396,1047,1862,331,589,1397,1863,186,1048,786,1864,1398,442,10,1865,1049,1399,590,1866,787,44,1400,1867,1050,332,443,1868,1401,788,1051,1869,591,1402,249,1870,140,1052,1403,1871,789,187,592,1872,1404,1053,444,1873,790,1405,333,1874,1054,105,1406,1875,593,791,1055,1876,1407,250,445,1877,59,1408,1056,1878,792,594,1409,1879,334,1057,79,1880,1410,793,446,1881,1058,1411,595,1882,188,794,1412,1883,1059,251,335,1884,1413,596,1060,1885,795,1414,447,1886,141,1061,1415,1887,33,796,597,1888,1416,1062,106,1889,448,1417,797,1890,1063,336,1418,1891,598,252,1064,1892,1419,798,189,1893,449,1420,1065,1894,599,799,1421,1895,4,1066,337,1896,1422,142,800,1897,1067,1423,600,1898,450,253,1424,1899,1068,801,25,1900,1425,601,1069,1901,338,1426,802,1902,451,1070,1427,1903,190,602,803,1904,1428,1071,80,1905,254,1429,452,1906,1072,804,1430,1907,603,339,1073,1908,1431,107,805,1909,143,1432,1074,1910,604,453,1433,1911,806,1075,191,1912,1434,340,605,1913,1076,1435,807,1914,255,454,1436,1915,1077,60,808,1916,1437,606,1078,1917,45,1438,341,1918,809,1079,1439,1919,455,607,14,1920,1440,1080,810,1921,256,1441,192,1922,1081,608,1442,1923,811,456,1082,1924,1443,342,144,1925,812,1444,1083,1926,609,108,1445,1927,457,1084,813,1928,1446,257,610,1929,1085,1447,343,1930,814,81,1448,1931,1086,458,611,1932,1449,815,1087,1933,193,1450,34,1934,344,1088,1451,1935,816,612,459,1936,1452,1089,258,1937,817,1453,145,1938,1090,613,1454,1939,61,818,1091,1940,1455,460,345,1941,614,1456,1092,1942,819,194,1457,1943,259,1093,461,1944,1458,820,615,1945,1094,1459,109,1946,346,821,1460,1947,1095,19,616,1948,1461,462,1096,1949,822,1462,146,1950,260,1097,1463,1951,617,823,347,1952,1464,1098,463,1953,195,1465,824,1954,1099,618,1466,1955,82,46,1100,1956,1467,825,464,1957,619,1468,1101,1958,348,826,1469,1959,261,1102,110,1960,1470,620,827,1961,1103,1471,465,1962,196,349,1472,1963,1104,828,621,1964,1473,147,1105,1965,466,1474,829,1966,262,1106,1475,1967,622,3,830,1968,1476,1107,350,1969,467,1477,623,1970,1108,831,1478,1971,62,197,1109,1972,1479,263,832,1973,624,1480,1110,1974,468,351,1481,1975,833,1111,83,1976,1482,625,148,1977,1112,1483,834,1978,469,111,1484,1979,1113,626,835,1980,1485,352,1114,1981,264,1486,470,1982,836,1115,1487,1983,627,198,26,1984,1488,1116,837,1985,353,1489,628,1986,1117,471,1490,1987,838,35,1118,1988,1491,265,629,1989,839,1492,1119,1990,149,472,1493,1991,354,1120,840,1992,1494,630,199,1993,1121,1495,47,1994,841,473,1496,1995,1122,631,266,1996,1497,842,1123,1997,355,1498,112,1998,632,1124,1499,1999,843,474,84,2000,1500,1125,200,2001,844,1501,633,2002,1126,356,1502,2003,475,845,1127,2004,1503,267,634,2005,150,1504,1128,2006,846,63,1505,2007,476,1129,635,2008,1506,847,357,2009,1130,1507,8,2010,268,848,1508,2011,1131,636,477,2012,1509,201,1132,2013,849,1510,358,2014,637,1133,1511,2015,113,850,478,2016,1512,1134,151]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "last $ take 2017 $ L.unfoldr ringBufferUnfolderE (1, 3, initialRingBuffer)" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [], + "source": [ + "part1 n k = (!1) $ last $ take k $ L.unfoldr ringBufferUnfolder (1, n, initialRingBuffer)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1025" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 366 2017" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Redundant bracket
Found:
(zeroLoc + 1) `rem` (V.length finalBuffer)
Why Not:
(zeroLoc + 1) `rem` V.length finalBuffer
" + ], + "text/plain": [ + "Line 4: Redundant bracket\n", + "Found:\n", + "(zeroLoc + 1) `rem` (V.length finalBuffer)\n", + "Why not:\n", + "(zeroLoc + 1) `rem` V.length finalBuffer" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 n k = finalBuffer!targetLoc\n", + " where finalBuffer = last $ take k $ L.unfoldr ringBufferUnfolder (1, n, initialRingBuffer)\n", + " zeroLoc = V.head $ V.elemIndices 0 finalBuffer\n", + " targetLoc = (zeroLoc + 1) `rem` (V.length finalBuffer)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "-- part2 366 50000000" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "urb buffer = buffer'\n", + " where nextPos = (initialStepSize + 1) `rem` V.length buffer\n", + " (start, end) = V.splitAt nextPos buffer\n", + " nextValue = V.length buffer\n", + " buffer' = V.cons nextValue $ end ++ start" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Redundant bracket
Found:
(iterate urb initialRingBuffer) !! 5
Why Not:
iterate urb initialRingBuffer !! 5
" + ], + "text/plain": [ + "Line 1: Redundant bracket\n", + "Found:\n", + "(iterate urb initialRingBuffer) !! 5\n", + "Why not:\n", + "iterate urb initialRingBuffer !! 5" + ] + }, + "metadata": {}, + "output_type": "display_data" + }, + { + "data": { + "text/plain": [ + "[5,3,0,1,4,2]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "(iterate urb initialRingBuffer)!!5" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Redundant bracket
Found:
(iterate urb initialRingBuffer) !! k
Why Not:
iterate urb initialRingBuffer !! k
" + ], + "text/plain": [ + "Line 1: Redundant bracket\n", + "Found:\n", + "(iterate urb initialRingBuffer) !! k\n", + "Why not:\n", + "iterate urb initialRingBuffer !! k" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 k = (!1) $ (iterate urb initialRingBuffer)!!k" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1025" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part1 2017" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "part1 k = (!1) $ (iterate urb initialRingBuffer)!!k" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
Redundant bracket
Found:
(iterate urb initialRingBuffer) !! k
Why Not:
iterate urb initialRingBuffer !! k
Redundant bracket
Found:
(zeroLoc + 1) `rem` (V.length finalBuffer)
Why Not:
(zeroLoc + 1) `rem` V.length finalBuffer
" + ], + "text/plain": [ + "Line 2: Redundant bracket\n", + "Found:\n", + "(iterate urb initialRingBuffer) !! k\n", + "Why not:\n", + "iterate urb initialRingBuffer !! kLine 4: Redundant bracket\n", + "Found:\n", + "(zeroLoc + 1) `rem` (V.length finalBuffer)\n", + "Why not:\n", + "(zeroLoc + 1) `rem` V.length finalBuffer" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 k = finalBuffer!targetLoc\n", + " where finalBuffer = (iterate urb initialRingBuffer)!!k\n", + " zeroLoc = V.head $ V.elemIndices 0 finalBuffer\n", + " targetLoc = (zeroLoc + 1) `rem` (V.length finalBuffer)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "13990" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "part2 50000" + ] + }, + { + "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 +} diff --git a/src/advent17/advent17leaky.hs b/src/advent17/advent17leaky.hs new file mode 100644 index 0000000..6745c61 --- /dev/null +++ b/src/advent17/advent17leaky.hs @@ -0,0 +1,47 @@ +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE NegativeLiterals #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE BangPatterns #-} + +import Prelude hiding ((++)) +import Data.Vector.Unboxed ((!), (++)) +import qualified Data.Vector.Unboxed as V +import qualified Data.List as L + +type RingBuffer = V.Vector Int + +initialStepSize :: Int +initialStepSize = 366 + +initialRingBuffer :: RingBuffer +initialRingBuffer = V.singleton 0 + + +main :: IO () +main = do + print $ part1 initialStepSize 2017 + print $ part2 initialStepSize 50000000 + + +part1 n k = (!1) $! last $! take k $! L.unfoldr ringBufferUnfolder (1, n, initialRingBuffer) + + +part2 n k = finalBuffer!targetLoc + where finalBuffer = last $! take k $! L.unfoldr ringBufferUnfolder (1, n, initialRingBuffer) + zeroLoc = V.head $ V.elemIndices 0 finalBuffer + targetLoc = (zeroLoc + 1) `rem` (V.length finalBuffer) + + +updateRingBuffer nextValue stepSize buffer = (buffer', stepSize) + where !nextPos = (stepSize + 1) `rem` V.length buffer + (!start, !end) = V.splitAt nextPos buffer + !buffer' = V.cons nextValue $ end ++ start + +ringBufferUnfolder :: (Int, Int, RingBuffer) -> Maybe (RingBuffer, (Int, Int, RingBuffer)) +ringBufferUnfolder (nextValue, stepSize, buffer) = Just (buffer', (nextValue + 1, stepSize', buffer')) + where (!buffer', !stepSize') = updateRingBuffer nextValue stepSize buffer \ No newline at end of file -- 2.34.1