Day 17 done
[advent-of-code-17.git] / src / advent17 / advent17.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {},
7 "outputs": [],
8 "source": [
9 "{-# LANGUAGE NegativeLiterals #-}\n",
10 "{-# LANGUAGE FlexibleContexts #-}\n",
11 "{-# LANGUAGE OverloadedStrings #-}\n",
12 "{-# LANGUAGE TypeFamilies #-}"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 2,
18 "metadata": {},
19 "outputs": [],
20 "source": [
21 "import Prelude hiding ((++))\n",
22 "import Data.Vector.Unboxed ((!), (++), (//))\n",
23 "import qualified Data.Vector.Unboxed as V\n",
24 "import qualified Data.List as L"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": 3,
30 "metadata": {},
31 "outputs": [],
32 "source": [
33 "type RingBuffer = V.Vector Int"
34 ]
35 },
36 {
37 "cell_type": "code",
38 "execution_count": 4,
39 "metadata": {},
40 "outputs": [],
41 "source": [
42 "initialStepSize = 366\n",
43 "\n",
44 "initialRingBuffer :: RingBuffer\n",
45 "initialRingBuffer = V.singleton 0"
46 ]
47 },
48 {
49 "cell_type": "code",
50 "execution_count": 5,
51 "metadata": {},
52 "outputs": [],
53 "source": [
54 "updateRingBufferInc nextValue stepSize buffer = (buffer', stepSize + 1)\n",
55 " where nextPos = (stepSize + 1) `rem` V.length buffer\n",
56 " (start, end) = V.splitAt nextPos buffer\n",
57 " buffer' = V.cons nextValue $ end ++ start"
58 ]
59 },
60 {
61 "cell_type": "code",
62 "execution_count": 6,
63 "metadata": {},
64 "outputs": [],
65 "source": [
66 "updateRingBuffer nextValue stepSize buffer = (buffer', stepSize)\n",
67 " where nextPos = (stepSize + 1) `rem` V.length buffer\n",
68 " (start, end) = V.splitAt nextPos buffer\n",
69 " buffer' = V.cons nextValue $ end ++ start"
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 7,
75 "metadata": {},
76 "outputs": [
77 {
78 "data": {
79 "text/plain": [
80 "([1,0],0)"
81 ]
82 },
83 "metadata": {},
84 "output_type": "display_data"
85 }
86 ],
87 "source": [
88 "updateRingBuffer 1 0 initialRingBuffer"
89 ]
90 },
91 {
92 "cell_type": "code",
93 "execution_count": 8,
94 "metadata": {},
95 "outputs": [
96 {
97 "data": {
98 "text/html": [
99 "<style>/* Styles used for the Hoogle display in the pager */\n",
100 ".hoogle-doc {\n",
101 "display: block;\n",
102 "padding-bottom: 1.3em;\n",
103 "padding-left: 0.4em;\n",
104 "}\n",
105 ".hoogle-code {\n",
106 "display: block;\n",
107 "font-family: monospace;\n",
108 "white-space: pre;\n",
109 "}\n",
110 ".hoogle-text {\n",
111 "display: block;\n",
112 "}\n",
113 ".hoogle-name {\n",
114 "color: green;\n",
115 "font-weight: bold;\n",
116 "}\n",
117 ".hoogle-head {\n",
118 "font-weight: bold;\n",
119 "}\n",
120 ".hoogle-sub {\n",
121 "display: block;\n",
122 "margin-left: 0.4em;\n",
123 "}\n",
124 ".hoogle-package {\n",
125 "font-weight: bold;\n",
126 "font-style: italic;\n",
127 "}\n",
128 ".hoogle-module {\n",
129 "font-weight: bold;\n",
130 "}\n",
131 ".hoogle-class {\n",
132 "font-weight: bold;\n",
133 "}\n",
134 ".get-type {\n",
135 "color: green;\n",
136 "font-weight: bold;\n",
137 "font-family: monospace;\n",
138 "display: block;\n",
139 "white-space: pre-wrap;\n",
140 "}\n",
141 ".show-type {\n",
142 "color: green;\n",
143 "font-weight: bold;\n",
144 "font-family: monospace;\n",
145 "margin-left: 1em;\n",
146 "}\n",
147 ".mono {\n",
148 "font-family: monospace;\n",
149 "display: block;\n",
150 "}\n",
151 ".err-msg {\n",
152 "color: red;\n",
153 "font-style: italic;\n",
154 "font-family: monospace;\n",
155 "white-space: pre;\n",
156 "display: block;\n",
157 "}\n",
158 "#unshowable {\n",
159 "color: red;\n",
160 "font-weight: bold;\n",
161 "}\n",
162 ".err-msg.in.collapse {\n",
163 "padding-top: 0.7em;\n",
164 "}\n",
165 ".highlight-code {\n",
166 "white-space: pre;\n",
167 "font-family: monospace;\n",
168 "}\n",
169 ".suggestion-warning { \n",
170 "font-weight: bold;\n",
171 "color: rgb(200, 130, 0);\n",
172 "}\n",
173 ".suggestion-error { \n",
174 "font-weight: bold;\n",
175 "color: red;\n",
176 "}\n",
177 ".suggestion-name {\n",
178 "font-weight: bold;\n",
179 "}\n",
180 "</style><span class='err-msg'>&lt;interactive&gt;:1:1: error:<br/> • Variable not in scope: updateRingBufferE :: Integer -&gt; Integer -&gt; RingBuffer -&gt; t<br/> • Perhaps you meant one of these: ‘updateRingBuffer’ (line 1), ‘updateRingBufferInc’ (line 1)</span>"
181 ],
182 "text/plain": [
183 "<interactive>:1:1: error:\n",
184 " • Variable not in scope: updateRingBufferE :: Integer -> Integer -> RingBuffer -> t\n",
185 " • Perhaps you meant one of these: ‘updateRingBuffer’ (line 1), ‘updateRingBufferInc’ (line 1)"
186 ]
187 },
188 "metadata": {},
189 "output_type": "display_data"
190 }
191 ],
192 "source": [
193 "updateRingBufferE 1 0 initialRingBuffer"
194 ]
195 },
196 {
197 "cell_type": "code",
198 "execution_count": 9,
199 "metadata": {},
200 "outputs": [
201 {
202 "data": {
203 "text/plain": [
204 "[0]"
205 ]
206 },
207 "metadata": {},
208 "output_type": "display_data"
209 }
210 ],
211 "source": [
212 "initialRingBuffer"
213 ]
214 },
215 {
216 "cell_type": "code",
217 "execution_count": 10,
218 "metadata": {},
219 "outputs": [],
220 "source": [
221 "ringBufferUnfolder :: (Int, Int, RingBuffer) -> Maybe (RingBuffer, (Int, Int, RingBuffer))\n",
222 "ringBufferUnfolder (nextValue, stepSize, buffer) = Just (buffer', (nextValue + 1, stepSize', buffer'))\n",
223 " where (buffer', stepSize') = updateRingBuffer nextValue stepSize buffer"
224 ]
225 },
226 {
227 "cell_type": "code",
228 "execution_count": 11,
229 "metadata": {},
230 "outputs": [
231 {
232 "data": {
233 "text/plain": [
234 "1025"
235 ]
236 },
237 "metadata": {},
238 "output_type": "display_data"
239 }
240 ],
241 "source": [
242 "(!1) $ last $ take 2017 $ L.unfoldr ringBufferUnfolder (1, 366, initialRingBuffer)"
243 ]
244 },
245 {
246 "cell_type": "code",
247 "execution_count": 12,
248 "metadata": {},
249 "outputs": [
250 {
251 "data": {
252 "text/plain": [
253 "[0,1,2,3,4,5,6,7,8]"
254 ]
255 },
256 "metadata": {},
257 "output_type": "display_data"
258 }
259 ],
260 "source": [
261 "t = V.fromList [0,1,2,3,4,5,6,7,8] :: RingBuffer\n",
262 "t"
263 ]
264 },
265 {
266 "cell_type": "code",
267 "execution_count": 13,
268 "metadata": {},
269 "outputs": [
270 {
271 "data": {
272 "text/plain": [
273 "([9,4,5,6,7,8,0,1,2,3],3)"
274 ]
275 },
276 "metadata": {},
277 "output_type": "display_data"
278 }
279 ],
280 "source": [
281 "updateRingBuffer 9 3 t"
282 ]
283 },
284 {
285 "cell_type": "code",
286 "execution_count": 14,
287 "metadata": {},
288 "outputs": [],
289 "source": [
290 "updateRingBufferE nextValue _stepSize buffer = (buffer', 3)\n",
291 " where nextPos = (3 + 1) `rem` V.length buffer\n",
292 " (start, end) = V.splitAt nextPos buffer\n",
293 " buffer' = V.cons nextValue $ end ++ start"
294 ]
295 },
296 {
297 "cell_type": "code",
298 "execution_count": 15,
299 "metadata": {},
300 "outputs": [],
301 "source": [
302 "ringBufferUnfolderE :: (Int, Int, RingBuffer) -> Maybe (RingBuffer, (Int, Int, RingBuffer))\n",
303 "ringBufferUnfolderE (nextValue, stepSize, buffer) = Just (buffer', (nextValue + 1, stepSize', buffer'))\n",
304 " where (buffer', stepSize') = updateRingBufferE nextValue stepSize buffer"
305 ]
306 },
307 {
308 "cell_type": "code",
309 "execution_count": 16,
310 "metadata": {
311 "scrolled": true
312 },
313 "outputs": [
314 {
315 "data": {
316 "text/plain": [
317 "[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]"
318 ]
319 },
320 "metadata": {},
321 "output_type": "display_data"
322 }
323 ],
324 "source": [
325 "last $ take 2017 $ L.unfoldr ringBufferUnfolderE (1, 3, initialRingBuffer)"
326 ]
327 },
328 {
329 "cell_type": "code",
330 "execution_count": 17,
331 "metadata": {},
332 "outputs": [],
333 "source": [
334 "part1 n k = (!1) $ last $ take k $ L.unfoldr ringBufferUnfolder (1, n, initialRingBuffer)"
335 ]
336 },
337 {
338 "cell_type": "code",
339 "execution_count": 18,
340 "metadata": {},
341 "outputs": [
342 {
343 "data": {
344 "text/plain": [
345 "1025"
346 ]
347 },
348 "metadata": {},
349 "output_type": "display_data"
350 }
351 ],
352 "source": [
353 "part1 366 2017"
354 ]
355 },
356 {
357 "cell_type": "code",
358 "execution_count": 19,
359 "metadata": {},
360 "outputs": [
361 {
362 "data": {
363 "text/html": [
364 "<style>/* Styles used for the Hoogle display in the pager */\n",
365 ".hoogle-doc {\n",
366 "display: block;\n",
367 "padding-bottom: 1.3em;\n",
368 "padding-left: 0.4em;\n",
369 "}\n",
370 ".hoogle-code {\n",
371 "display: block;\n",
372 "font-family: monospace;\n",
373 "white-space: pre;\n",
374 "}\n",
375 ".hoogle-text {\n",
376 "display: block;\n",
377 "}\n",
378 ".hoogle-name {\n",
379 "color: green;\n",
380 "font-weight: bold;\n",
381 "}\n",
382 ".hoogle-head {\n",
383 "font-weight: bold;\n",
384 "}\n",
385 ".hoogle-sub {\n",
386 "display: block;\n",
387 "margin-left: 0.4em;\n",
388 "}\n",
389 ".hoogle-package {\n",
390 "font-weight: bold;\n",
391 "font-style: italic;\n",
392 "}\n",
393 ".hoogle-module {\n",
394 "font-weight: bold;\n",
395 "}\n",
396 ".hoogle-class {\n",
397 "font-weight: bold;\n",
398 "}\n",
399 ".get-type {\n",
400 "color: green;\n",
401 "font-weight: bold;\n",
402 "font-family: monospace;\n",
403 "display: block;\n",
404 "white-space: pre-wrap;\n",
405 "}\n",
406 ".show-type {\n",
407 "color: green;\n",
408 "font-weight: bold;\n",
409 "font-family: monospace;\n",
410 "margin-left: 1em;\n",
411 "}\n",
412 ".mono {\n",
413 "font-family: monospace;\n",
414 "display: block;\n",
415 "}\n",
416 ".err-msg {\n",
417 "color: red;\n",
418 "font-style: italic;\n",
419 "font-family: monospace;\n",
420 "white-space: pre;\n",
421 "display: block;\n",
422 "}\n",
423 "#unshowable {\n",
424 "color: red;\n",
425 "font-weight: bold;\n",
426 "}\n",
427 ".err-msg.in.collapse {\n",
428 "padding-top: 0.7em;\n",
429 "}\n",
430 ".highlight-code {\n",
431 "white-space: pre;\n",
432 "font-family: monospace;\n",
433 "}\n",
434 ".suggestion-warning { \n",
435 "font-weight: bold;\n",
436 "color: rgb(200, 130, 0);\n",
437 "}\n",
438 ".suggestion-error { \n",
439 "font-weight: bold;\n",
440 "color: red;\n",
441 "}\n",
442 ".suggestion-name {\n",
443 "font-weight: bold;\n",
444 "}\n",
445 "</style><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(zeroLoc + 1) `rem` (V.length finalBuffer)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">(zeroLoc + 1) `rem` V.length finalBuffer</div></div>"
446 ],
447 "text/plain": [
448 "Line 4: Redundant bracket\n",
449 "Found:\n",
450 "(zeroLoc + 1) `rem` (V.length finalBuffer)\n",
451 "Why not:\n",
452 "(zeroLoc + 1) `rem` V.length finalBuffer"
453 ]
454 },
455 "metadata": {},
456 "output_type": "display_data"
457 }
458 ],
459 "source": [
460 "part2 n k = finalBuffer!targetLoc\n",
461 " where finalBuffer = last $ take k $ L.unfoldr ringBufferUnfolder (1, n, initialRingBuffer)\n",
462 " zeroLoc = V.head $ V.elemIndices 0 finalBuffer\n",
463 " targetLoc = (zeroLoc + 1) `rem` (V.length finalBuffer)"
464 ]
465 },
466 {
467 "cell_type": "code",
468 "execution_count": 20,
469 "metadata": {},
470 "outputs": [],
471 "source": [
472 "-- part2 366 50000000"
473 ]
474 },
475 {
476 "cell_type": "code",
477 "execution_count": 21,
478 "metadata": {},
479 "outputs": [],
480 "source": [
481 "urb buffer = buffer'\n",
482 " where nextPos = (initialStepSize + 1) `rem` V.length buffer\n",
483 " (start, end) = V.splitAt nextPos buffer\n",
484 " nextValue = V.length buffer\n",
485 " buffer' = V.cons nextValue $ end ++ start"
486 ]
487 },
488 {
489 "cell_type": "code",
490 "execution_count": 27,
491 "metadata": {},
492 "outputs": [
493 {
494 "data": {
495 "text/html": [
496 "<style>/* Styles used for the Hoogle display in the pager */\n",
497 ".hoogle-doc {\n",
498 "display: block;\n",
499 "padding-bottom: 1.3em;\n",
500 "padding-left: 0.4em;\n",
501 "}\n",
502 ".hoogle-code {\n",
503 "display: block;\n",
504 "font-family: monospace;\n",
505 "white-space: pre;\n",
506 "}\n",
507 ".hoogle-text {\n",
508 "display: block;\n",
509 "}\n",
510 ".hoogle-name {\n",
511 "color: green;\n",
512 "font-weight: bold;\n",
513 "}\n",
514 ".hoogle-head {\n",
515 "font-weight: bold;\n",
516 "}\n",
517 ".hoogle-sub {\n",
518 "display: block;\n",
519 "margin-left: 0.4em;\n",
520 "}\n",
521 ".hoogle-package {\n",
522 "font-weight: bold;\n",
523 "font-style: italic;\n",
524 "}\n",
525 ".hoogle-module {\n",
526 "font-weight: bold;\n",
527 "}\n",
528 ".hoogle-class {\n",
529 "font-weight: bold;\n",
530 "}\n",
531 ".get-type {\n",
532 "color: green;\n",
533 "font-weight: bold;\n",
534 "font-family: monospace;\n",
535 "display: block;\n",
536 "white-space: pre-wrap;\n",
537 "}\n",
538 ".show-type {\n",
539 "color: green;\n",
540 "font-weight: bold;\n",
541 "font-family: monospace;\n",
542 "margin-left: 1em;\n",
543 "}\n",
544 ".mono {\n",
545 "font-family: monospace;\n",
546 "display: block;\n",
547 "}\n",
548 ".err-msg {\n",
549 "color: red;\n",
550 "font-style: italic;\n",
551 "font-family: monospace;\n",
552 "white-space: pre;\n",
553 "display: block;\n",
554 "}\n",
555 "#unshowable {\n",
556 "color: red;\n",
557 "font-weight: bold;\n",
558 "}\n",
559 ".err-msg.in.collapse {\n",
560 "padding-top: 0.7em;\n",
561 "}\n",
562 ".highlight-code {\n",
563 "white-space: pre;\n",
564 "font-family: monospace;\n",
565 "}\n",
566 ".suggestion-warning { \n",
567 "font-weight: bold;\n",
568 "color: rgb(200, 130, 0);\n",
569 "}\n",
570 ".suggestion-error { \n",
571 "font-weight: bold;\n",
572 "color: red;\n",
573 "}\n",
574 ".suggestion-name {\n",
575 "font-weight: bold;\n",
576 "}\n",
577 "</style><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(iterate urb initialRingBuffer) !! 5</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">iterate urb initialRingBuffer !! 5</div></div>"
578 ],
579 "text/plain": [
580 "Line 1: Redundant bracket\n",
581 "Found:\n",
582 "(iterate urb initialRingBuffer) !! 5\n",
583 "Why not:\n",
584 "iterate urb initialRingBuffer !! 5"
585 ]
586 },
587 "metadata": {},
588 "output_type": "display_data"
589 },
590 {
591 "data": {
592 "text/plain": [
593 "[5,3,0,1,4,2]"
594 ]
595 },
596 "metadata": {},
597 "output_type": "display_data"
598 }
599 ],
600 "source": [
601 "(iterate urb initialRingBuffer)!!5"
602 ]
603 },
604 {
605 "cell_type": "code",
606 "execution_count": 28,
607 "metadata": {},
608 "outputs": [
609 {
610 "data": {
611 "text/html": [
612 "<style>/* Styles used for the Hoogle display in the pager */\n",
613 ".hoogle-doc {\n",
614 "display: block;\n",
615 "padding-bottom: 1.3em;\n",
616 "padding-left: 0.4em;\n",
617 "}\n",
618 ".hoogle-code {\n",
619 "display: block;\n",
620 "font-family: monospace;\n",
621 "white-space: pre;\n",
622 "}\n",
623 ".hoogle-text {\n",
624 "display: block;\n",
625 "}\n",
626 ".hoogle-name {\n",
627 "color: green;\n",
628 "font-weight: bold;\n",
629 "}\n",
630 ".hoogle-head {\n",
631 "font-weight: bold;\n",
632 "}\n",
633 ".hoogle-sub {\n",
634 "display: block;\n",
635 "margin-left: 0.4em;\n",
636 "}\n",
637 ".hoogle-package {\n",
638 "font-weight: bold;\n",
639 "font-style: italic;\n",
640 "}\n",
641 ".hoogle-module {\n",
642 "font-weight: bold;\n",
643 "}\n",
644 ".hoogle-class {\n",
645 "font-weight: bold;\n",
646 "}\n",
647 ".get-type {\n",
648 "color: green;\n",
649 "font-weight: bold;\n",
650 "font-family: monospace;\n",
651 "display: block;\n",
652 "white-space: pre-wrap;\n",
653 "}\n",
654 ".show-type {\n",
655 "color: green;\n",
656 "font-weight: bold;\n",
657 "font-family: monospace;\n",
658 "margin-left: 1em;\n",
659 "}\n",
660 ".mono {\n",
661 "font-family: monospace;\n",
662 "display: block;\n",
663 "}\n",
664 ".err-msg {\n",
665 "color: red;\n",
666 "font-style: italic;\n",
667 "font-family: monospace;\n",
668 "white-space: pre;\n",
669 "display: block;\n",
670 "}\n",
671 "#unshowable {\n",
672 "color: red;\n",
673 "font-weight: bold;\n",
674 "}\n",
675 ".err-msg.in.collapse {\n",
676 "padding-top: 0.7em;\n",
677 "}\n",
678 ".highlight-code {\n",
679 "white-space: pre;\n",
680 "font-family: monospace;\n",
681 "}\n",
682 ".suggestion-warning { \n",
683 "font-weight: bold;\n",
684 "color: rgb(200, 130, 0);\n",
685 "}\n",
686 ".suggestion-error { \n",
687 "font-weight: bold;\n",
688 "color: red;\n",
689 "}\n",
690 ".suggestion-name {\n",
691 "font-weight: bold;\n",
692 "}\n",
693 "</style><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(iterate urb initialRingBuffer) !! k</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">iterate urb initialRingBuffer !! k</div></div>"
694 ],
695 "text/plain": [
696 "Line 1: Redundant bracket\n",
697 "Found:\n",
698 "(iterate urb initialRingBuffer) !! k\n",
699 "Why not:\n",
700 "iterate urb initialRingBuffer !! k"
701 ]
702 },
703 "metadata": {},
704 "output_type": "display_data"
705 }
706 ],
707 "source": [
708 "part1 k = (!1) $ (iterate urb initialRingBuffer)!!k"
709 ]
710 },
711 {
712 "cell_type": "code",
713 "execution_count": 29,
714 "metadata": {},
715 "outputs": [
716 {
717 "data": {
718 "text/plain": [
719 "1025"
720 ]
721 },
722 "metadata": {},
723 "output_type": "display_data"
724 }
725 ],
726 "source": [
727 "part1 2017"
728 ]
729 },
730 {
731 "cell_type": "code",
732 "execution_count": null,
733 "metadata": {},
734 "outputs": [],
735 "source": [
736 "part1 k = (!1) $ (iterate urb initialRingBuffer)!!k"
737 ]
738 },
739 {
740 "cell_type": "code",
741 "execution_count": 30,
742 "metadata": {},
743 "outputs": [
744 {
745 "data": {
746 "text/html": [
747 "<style>/* Styles used for the Hoogle display in the pager */\n",
748 ".hoogle-doc {\n",
749 "display: block;\n",
750 "padding-bottom: 1.3em;\n",
751 "padding-left: 0.4em;\n",
752 "}\n",
753 ".hoogle-code {\n",
754 "display: block;\n",
755 "font-family: monospace;\n",
756 "white-space: pre;\n",
757 "}\n",
758 ".hoogle-text {\n",
759 "display: block;\n",
760 "}\n",
761 ".hoogle-name {\n",
762 "color: green;\n",
763 "font-weight: bold;\n",
764 "}\n",
765 ".hoogle-head {\n",
766 "font-weight: bold;\n",
767 "}\n",
768 ".hoogle-sub {\n",
769 "display: block;\n",
770 "margin-left: 0.4em;\n",
771 "}\n",
772 ".hoogle-package {\n",
773 "font-weight: bold;\n",
774 "font-style: italic;\n",
775 "}\n",
776 ".hoogle-module {\n",
777 "font-weight: bold;\n",
778 "}\n",
779 ".hoogle-class {\n",
780 "font-weight: bold;\n",
781 "}\n",
782 ".get-type {\n",
783 "color: green;\n",
784 "font-weight: bold;\n",
785 "font-family: monospace;\n",
786 "display: block;\n",
787 "white-space: pre-wrap;\n",
788 "}\n",
789 ".show-type {\n",
790 "color: green;\n",
791 "font-weight: bold;\n",
792 "font-family: monospace;\n",
793 "margin-left: 1em;\n",
794 "}\n",
795 ".mono {\n",
796 "font-family: monospace;\n",
797 "display: block;\n",
798 "}\n",
799 ".err-msg {\n",
800 "color: red;\n",
801 "font-style: italic;\n",
802 "font-family: monospace;\n",
803 "white-space: pre;\n",
804 "display: block;\n",
805 "}\n",
806 "#unshowable {\n",
807 "color: red;\n",
808 "font-weight: bold;\n",
809 "}\n",
810 ".err-msg.in.collapse {\n",
811 "padding-top: 0.7em;\n",
812 "}\n",
813 ".highlight-code {\n",
814 "white-space: pre;\n",
815 "font-family: monospace;\n",
816 "}\n",
817 ".suggestion-warning { \n",
818 "font-weight: bold;\n",
819 "color: rgb(200, 130, 0);\n",
820 "}\n",
821 ".suggestion-error { \n",
822 "font-weight: bold;\n",
823 "color: red;\n",
824 "}\n",
825 ".suggestion-name {\n",
826 "font-weight: bold;\n",
827 "}\n",
828 "</style><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(iterate urb initialRingBuffer) !! k</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">iterate urb initialRingBuffer !! k</div></div><div class=\"suggestion-name\" style=\"clear:both;\">Redundant bracket</div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Found:</div><div class=\"highlight-code\" id=\"haskell\">(zeroLoc + 1) `rem` (V.length finalBuffer)</div></div><div class=\"suggestion-row\" style=\"float: left;\"><div class=\"suggestion-warning\">Why Not:</div><div class=\"highlight-code\" id=\"haskell\">(zeroLoc + 1) `rem` V.length finalBuffer</div></div>"
829 ],
830 "text/plain": [
831 "Line 2: Redundant bracket\n",
832 "Found:\n",
833 "(iterate urb initialRingBuffer) !! k\n",
834 "Why not:\n",
835 "iterate urb initialRingBuffer !! kLine 4: Redundant bracket\n",
836 "Found:\n",
837 "(zeroLoc + 1) `rem` (V.length finalBuffer)\n",
838 "Why not:\n",
839 "(zeroLoc + 1) `rem` V.length finalBuffer"
840 ]
841 },
842 "metadata": {},
843 "output_type": "display_data"
844 }
845 ],
846 "source": [
847 "part2 k = finalBuffer!targetLoc\n",
848 " where finalBuffer = (iterate urb initialRingBuffer)!!k\n",
849 " zeroLoc = V.head $ V.elemIndices 0 finalBuffer\n",
850 " targetLoc = (zeroLoc + 1) `rem` (V.length finalBuffer)"
851 ]
852 },
853 {
854 "cell_type": "code",
855 "execution_count": 32,
856 "metadata": {},
857 "outputs": [
858 {
859 "data": {
860 "text/plain": [
861 "13990"
862 ]
863 },
864 "metadata": {},
865 "output_type": "display_data"
866 }
867 ],
868 "source": [
869 "part2 50000"
870 ]
871 },
872 {
873 "cell_type": "code",
874 "execution_count": null,
875 "metadata": {},
876 "outputs": [],
877 "source": []
878 }
879 ],
880 "metadata": {
881 "kernelspec": {
882 "display_name": "Haskell",
883 "language": "haskell",
884 "name": "haskell"
885 },
886 "language_info": {
887 "codemirror_mode": "ihaskell",
888 "file_extension": ".hs",
889 "name": "haskell",
890 "version": "8.0.2"
891 }
892 },
893 "nbformat": 4,
894 "nbformat_minor": 2
895 }