{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Imports" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "from time import time\n", "\n", "from numpy.lib.stride_tricks import sliding_window_view" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Load input" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": true }, "outputs": [], "source": [ "#INPUT_FILE = 'test.txt'\n", "INPUT_FILE = 'input.txt'" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "[188,\n", " 192,\n", " 193,\n", " 194,\n", " 192,\n", " 213,\n", " 214,\n", " 216,\n", " 219,\n", " 221,\n", " 222,\n", " 228,\n", " 231,\n", " 234,\n", " 239,\n", " 240,\n", " 252,\n", " 262,\n", " 267,\n", " 279,\n", " 278,\n", " 277,\n", " 279,\n", " 293,\n", " 309,\n", " 310,\n", " 312,\n", " 307,\n", " 308,\n", " 316,\n", " 320,\n", " 323,\n", " 335,\n", " 336,\n", " 343,\n", " 368,\n", " 357,\n", " 370,\n", " 377,\n", " 386,\n", " 387,\n", " 386,\n", " 402,\n", " 379,\n", " 393,\n", " 394,\n", " 403,\n", " 404,\n", " 405,\n", " 400,\n", " 421,\n", " 422,\n", " 423,\n", " 408,\n", " 409,\n", " 411,\n", " 408,\n", " 415,\n", " 417,\n", " 430,\n", " 439,\n", " 432,\n", " 459,\n", " 462,\n", " 463,\n", " 464,\n", " 466,\n", " 472,\n", " 485,\n", " 473,\n", " 488,\n", " 491,\n", " 492,\n", " 493,\n", " 496,\n", " 497,\n", " 501,\n", " 503,\n", " 516,\n", " 514,\n", " 513,\n", " 514,\n", " 517,\n", " 521,\n", " 522,\n", " 526,\n", " 548,\n", " 553,\n", " 560,\n", " 563,\n", " 565,\n", " 600,\n", " 601,\n", " 595,\n", " 596,\n", " 597,\n", " 599,\n", " 613,\n", " 630,\n", " 631,\n", " 635,\n", " 642,\n", " 648,\n", " 651,\n", " 654,\n", " 645,\n", " 653,\n", " 657,\n", " 676,\n", " 688,\n", " 711,\n", " 717,\n", " 718,\n", " 728,\n", " 735,\n", " 730,\n", " 733,\n", " 734,\n", " 737,\n", " 736,\n", " 738,\n", " 741,\n", " 749,\n", " 751,\n", " 752,\n", " 762,\n", " 765,\n", " 776,\n", " 779,\n", " 758,\n", " 743,\n", " 754,\n", " 759,\n", " 772,\n", " 773,\n", " 783,\n", " 790,\n", " 791,\n", " 783,\n", " 781,\n", " 784,\n", " 785,\n", " 791,\n", " 780,\n", " 789,\n", " 805,\n", " 792,\n", " 793,\n", " 797,\n", " 825,\n", " 828,\n", " 820,\n", " 817,\n", " 820,\n", " 821,\n", " 824,\n", " 825,\n", " 801,\n", " 815,\n", " 816,\n", " 829,\n", " 844,\n", " 845,\n", " 850,\n", " 853,\n", " 845,\n", " 841,\n", " 842,\n", " 844,\n", " 830,\n", " 844,\n", " 839,\n", " 842,\n", " 857,\n", " 859,\n", " 861,\n", " 871,\n", " 880,\n", " 881,\n", " 903,\n", " 935,\n", " 960,\n", " 964,\n", " 967,\n", " 969,\n", " 972,\n", " 990,\n", " 996,\n", " 999,\n", " 1003,\n", " 1008,\n", " 1034,\n", " 1038,\n", " 1039,\n", " 1053,\n", " 1054,\n", " 1055,\n", " 1061,\n", " 1065,\n", " 1066,\n", " 1061,\n", " 1064,\n", " 1073,\n", " 1078,\n", " 1084,\n", " 1087,\n", " 1106,\n", " 1113,\n", " 1118,\n", " 1128,\n", " 1132,\n", " 1133,\n", " 1126,\n", " 1127,\n", " 1134,\n", " 1150,\n", " 1153,\n", " 1162,\n", " 1176,\n", " 1186,\n", " 1187,\n", " 1215,\n", " 1218,\n", " 1221,\n", " 1224,\n", " 1225,\n", " 1232,\n", " 1234,\n", " 1241,\n", " 1247,\n", " 1248,\n", " 1251,\n", " 1250,\n", " 1252,\n", " 1247,\n", " 1249,\n", " 1251,\n", " 1252,\n", " 1253,\n", " 1260,\n", " 1263,\n", " 1269,\n", " 1270,\n", " 1277,\n", " 1287,\n", " 1262,\n", " 1275,\n", " 1276,\n", " 1281,\n", " 1282,\n", " 1285,\n", " 1284,\n", " 1296,\n", " 1304,\n", " 1310,\n", " 1307,\n", " 1308,\n", " 1285,\n", " 1286,\n", " 1293,\n", " 1298,\n", " 1303,\n", " 1309,\n", " 1312,\n", " 1315,\n", " 1317,\n", " 1325,\n", " 1335,\n", " 1338,\n", " 1343,\n", " 1346,\n", " 1348,\n", " 1360,\n", " 1366,\n", " 1367,\n", " 1371,\n", " 1384,\n", " 1396,\n", " 1400,\n", " 1407,\n", " 1425,\n", " 1424,\n", " 1427,\n", " 1433,\n", " 1446,\n", " 1449,\n", " 1466,\n", " 1469,\n", " 1495,\n", " 1507,\n", " 1508,\n", " 1509,\n", " 1521,\n", " 1514,\n", " 1519,\n", " 1533,\n", " 1534,\n", " 1538,\n", " 1539,\n", " 1541,\n", " 1551,\n", " 1561,\n", " 1569,\n", " 1570,\n", " 1582,\n", " 1583,\n", " 1593,\n", " 1599,\n", " 1607,\n", " 1608,\n", " 1614,\n", " 1618,\n", " 1633,\n", " 1638,\n", " 1646,\n", " 1652,\n", " 1653,\n", " 1680,\n", " 1682,\n", " 1689,\n", " 1700,\n", " 1702,\n", " 1700,\n", " 1706,\n", " 1709,\n", " 1710,\n", " 1712,\n", " 1718,\n", " 1710,\n", " 1711,\n", " 1736,\n", " 1727,\n", " 1729,\n", " 1758,\n", " 1762,\n", " 1780,\n", " 1794,\n", " 1811,\n", " 1813,\n", " 1817,\n", " 1818,\n", " 1826,\n", " 1829,\n", " 1830,\n", " 1811,\n", " 1817,\n", " 1818,\n", " 1820,\n", " 1821,\n", " 1822,\n", " 1824,\n", " 1825,\n", " 1838,\n", " 1840,\n", " 1841,\n", " 1844,\n", " 1846,\n", " 1849,\n", " 1875,\n", " 1878,\n", " 1879,\n", " 1881,\n", " 1908,\n", " 1917,\n", " 1923,\n", " 1918,\n", " 1930,\n", " 1921,\n", " 1926,\n", " 1927,\n", " 1929,\n", " 1940,\n", " 1941,\n", " 1946,\n", " 1949,\n", " 1951,\n", " 1960,\n", " 1964,\n", " 1965,\n", " 1971,\n", " 1975,\n", " 1979,\n", " 1984,\n", " 1986,\n", " 1985,\n", " 2010,\n", " 2005,\n", " 2006,\n", " 2010,\n", " 2011,\n", " 2012,\n", " 2013,\n", " 2029,\n", " 2031,\n", " 2036,\n", " 2039,\n", " 2045,\n", " 2027,\n", " 2035,\n", " 2014,\n", " 2017,\n", " 2031,\n", " 2033,\n", " 2037,\n", " 2040,\n", " 2044,\n", " 2050,\n", " 2051,\n", " 2049,\n", " 2052,\n", " 2053,\n", " 2068,\n", " 2069,\n", " 2071,\n", " 2073,\n", " 2074,\n", " 2075,\n", " 2086,\n", " 2089,\n", " 2091,\n", " 2092,\n", " 2093,\n", " 2094,\n", " 2095,\n", " 2096,\n", " 2097,\n", " 2100,\n", " 2106,\n", " 2107,\n", " 2108,\n", " 2122,\n", " 2094,\n", " 2095,\n", " 2121,\n", " 2131,\n", " 2132,\n", " 2135,\n", " 2162,\n", " 2177,\n", " 2178,\n", " 2180,\n", " 2199,\n", " 2210,\n", " 2211,\n", " 2213,\n", " 2217,\n", " 2229,\n", " 2255,\n", " 2259,\n", " 2266,\n", " 2268,\n", " 2266,\n", " 2272,\n", " 2278,\n", " 2295,\n", " 2296,\n", " 2294,\n", " 2298,\n", " 2303,\n", " 2309,\n", " 2310,\n", " 2311,\n", " 2312,\n", " 2310,\n", " 2315,\n", " 2316,\n", " 2332,\n", " 2335,\n", " 2337,\n", " 2339,\n", " 2342,\n", " 2343,\n", " 2362,\n", " 2371,\n", " 2373,\n", " 2375,\n", " 2381,\n", " 2380,\n", " 2382,\n", " 2396,\n", " 2398,\n", " 2405,\n", " 2406,\n", " 2403,\n", " 2405,\n", " 2413,\n", " 2418,\n", " 2423,\n", " 2440,\n", " 2443,\n", " 2442,\n", " 2434,\n", " 2436,\n", " 2444,\n", " 2450,\n", " 2456,\n", " 2485,\n", " 2486,\n", " 2487,\n", " 2491,\n", " 2489,\n", " 2490,\n", " 2491,\n", " 2513,\n", " 2509,\n", " 2511,\n", " 2525,\n", " 2526,\n", " 2535,\n", " 2548,\n", " 2551,\n", " 2557,\n", " 2559,\n", " 2560,\n", " 2568,\n", " 2565,\n", " 2591,\n", " 2607,\n", " 2614,\n", " 2615,\n", " 2616,\n", " 2630,\n", " 2665,\n", " 2677,\n", " 2680,\n", " 2679,\n", " 2686,\n", " 2687,\n", " 2692,\n", " 2687,\n", " 2689,\n", " 2690,\n", " 2694,\n", " 2692,\n", " 2693,\n", " 2694,\n", " 2695,\n", " 2696,\n", " 2698,\n", " 2720,\n", " 2743,\n", " 2744,\n", " 2752,\n", " 2777,\n", " 2780,\n", " 2778,\n", " 2786,\n", " 2800,\n", " 2807,\n", " 2808,\n", " 2810,\n", " 2817,\n", " 2823,\n", " 2835,\n", " 2840,\n", " 2848,\n", " 2850,\n", " 2855,\n", " 2856,\n", " 2861,\n", " 2864,\n", " 2890,\n", " 2892,\n", " 2894,\n", " 2905,\n", " 2906,\n", " 2907,\n", " 2918,\n", " 2922,\n", " 2927,\n", " 2928,\n", " 2918,\n", " 2933,\n", " 2934,\n", " 2936,\n", " 2939,\n", " 2947,\n", " 2944,\n", " 2932,\n", " 2938,\n", " 2941,\n", " 2954,\n", " 2957,\n", " 2965,\n", " 2970,\n", " 2971,\n", " 2972,\n", " 2981,\n", " 2984,\n", " 2994,\n", " 3006,\n", " 3028,\n", " 3020,\n", " 3023,\n", " 3026,\n", " 3021,\n", " 3024,\n", " 3016,\n", " 3041,\n", " 3042,\n", " 3053,\n", " 3055,\n", " 3056,\n", " 3057,\n", " 3060,\n", " 3061,\n", " 3062,\n", " 3069,\n", " 3070,\n", " 3097,\n", " 3104,\n", " 3107,\n", " 3110,\n", " 3112,\n", " 3114,\n", " 3115,\n", " 3122,\n", " 3120,\n", " 3121,\n", " 3142,\n", " 3143,\n", " 3167,\n", " 3169,\n", " 3170,\n", " 3172,\n", " 3180,\n", " 3182,\n", " 3181,\n", " 3182,\n", " 3183,\n", " 3184,\n", " 3185,\n", " 3186,\n", " 3201,\n", " 3181,\n", " 3185,\n", " 3187,\n", " 3166,\n", " 3162,\n", " 3164,\n", " 3167,\n", " 3168,\n", " 3180,\n", " 3189,\n", " 3190,\n", " 3191,\n", " 3189,\n", " 3191,\n", " 3196,\n", " 3213,\n", " 3224,\n", " 3229,\n", " 3233,\n", " 3232,\n", " 3245,\n", " 3279,\n", " 3302,\n", " 3313,\n", " 3316,\n", " 3333,\n", " 3334,\n", " 3337,\n", " 3339,\n", " 3348,\n", " 3364,\n", " 3365,\n", " 3375,\n", " 3380,\n", " 3381,\n", " 3382,\n", " 3402,\n", " 3418,\n", " 3433,\n", " 3438,\n", " 3439,\n", " 3453,\n", " 3482,\n", " 3484,\n", " 3488,\n", " 3497,\n", " 3511,\n", " 3500,\n", " 3502,\n", " 3497,\n", " 3502,\n", " 3505,\n", " 3516,\n", " 3517,\n", " 3519,\n", " 3525,\n", " 3520,\n", " 3524,\n", " 3528,\n", " 3533,\n", " 3548,\n", " 3552,\n", " 3555,\n", " 3562,\n", " 3573,\n", " 3577,\n", " 3579,\n", " 3587,\n", " 3592,\n", " 3620,\n", " 3615,\n", " 3616,\n", " 3619,\n", " 3620,\n", " 3631,\n", " 3630,\n", " 3640,\n", " 3674,\n", " 3675,\n", " 3684,\n", " 3685,\n", " 3699,\n", " 3701,\n", " 3694,\n", " 3727,\n", " 3746,\n", " 3749,\n", " 3742,\n", " 3745,\n", " 3748,\n", " 3756,\n", " 3759,\n", " 3746,\n", " 3759,\n", " 3762,\n", " 3763,\n", " 3748,\n", " 3762,\n", " 3760,\n", " 3766,\n", " 3770,\n", " 3774,\n", " 3776,\n", " 3778,\n", " 3785,\n", " 3787,\n", " 3788,\n", " 3815,\n", " 3816,\n", " 3822,\n", " 3832,\n", " 3831,\n", " 3832,\n", " 3835,\n", " 3834,\n", " 3820,\n", " 3822,\n", " 3823,\n", " 3825,\n", " 3826,\n", " 3831,\n", " 3833,\n", " 3843,\n", " 3864,\n", " 3889,\n", " 3895,\n", " 3903,\n", " 3908,\n", " 3910,\n", " 3909,\n", " 3911,\n", " 3912,\n", " 3913,\n", " 3921,\n", " 3948,\n", " 3949,\n", " 3954,\n", " 3955,\n", " 3958,\n", " 3960,\n", " 3962,\n", " 3963,\n", " 3975,\n", " 3986,\n", " 3989,\n", " 3990,\n", " 3992,\n", " 4006,\n", " 4012,\n", " 4032,\n", " 4036,\n", " 4037,\n", " 4038,\n", " 4041,\n", " 4048,\n", " 4056,\n", " 4041,\n", " 4044,\n", " 4048,\n", " 4077,\n", " 4076,\n", " 4088,\n", " 4072,\n", " 4073,\n", " 4075,\n", " 4080,\n", " 4075,\n", " 4080,\n", " 4081,\n", " 4110,\n", " 4139,\n", " 4142,\n", " 4143,\n", " 4145,\n", " 4148,\n", " 4155,\n", " 4157,\n", " 4163,\n", " 4164,\n", " 4174,\n", " 4181,\n", " 4186,\n", " 4193,\n", " 4195,\n", " 4198,\n", " 4197,\n", " 4202,\n", " 4209,\n", " 4208,\n", " 4211,\n", " 4213,\n", " 4211,\n", " 4223,\n", " 4222,\n", " 4224,\n", " 4232,\n", " 4234,\n", " 4239,\n", " 4243,\n", " 4245,\n", " 4247,\n", " 4248,\n", " 4252,\n", " 4245,\n", " 4253,\n", " 4289,\n", " 4288,\n", " 4290,\n", " 4292,\n", " 4294,\n", " 4295,\n", " 4298,\n", " 4308,\n", " 4314,\n", " 4315,\n", " 4320,\n", " 4328,\n", " 4326,\n", " 4328,\n", " 4334,\n", " 4330,\n", " 4339,\n", " 4362,\n", " 4363,\n", " 4364,\n", " 4365,\n", " 4366,\n", " 4375,\n", " 4388,\n", " 4389,\n", " 4395,\n", " 4382,\n", " 4393,\n", " 4385,\n", " 4381,\n", " 4380,\n", " 4391,\n", " 4392,\n", " 4409,\n", " 4410,\n", " 4414,\n", " 4434,\n", " 4445,\n", " 4452,\n", " 4457,\n", " 4453,\n", " 4461,\n", " 4469,\n", " 4468,\n", " 4487,\n", " 4482,\n", " 4505,\n", " 4512,\n", " 4513,\n", " 4517,\n", " 4535,\n", " 4542,\n", " 4544,\n", " 4546,\n", " 4559,\n", " 4560,\n", " 4575,\n", " 4576,\n", " 4578,\n", " 4579,\n", " 4580,\n", " 4581,\n", " 4582,\n", " 4585,\n", " 4589,\n", " 4588,\n", " 4592,\n", " 4593,\n", " 4594,\n", " 4620,\n", " 4621,\n", " 4632,\n", " 4633,\n", " 4634,\n", " 4640,\n", " 4641,\n", " 4643,\n", " 4651,\n", " 4666,\n", " 4672,\n", " 4673,\n", " 4672,\n", " 4674,\n", " 4695,\n", " 4697,\n", " 4707,\n", " 4706,\n", " 4708,\n", " 4709,\n", " 4705,\n", " 4702,\n", " 4694,\n", " 4732,\n", " 4734,\n", " 4735,\n", " 4723,\n", " 4728,\n", " 4734,\n", " 4752,\n", " 4754,\n", " 4756,\n", " 4759,\n", " 4768,\n", " 4770,\n", " 4773,\n", " 4780,\n", " 4781,\n", " 4783,\n", " 4792,\n", " 4797,\n", " 4806,\n", " 4825,\n", " 4833,\n", " 4839,\n", " 4838,\n", " 4842,\n", " 4843,\n", " 4853,\n", " 4851,\n", " 4852,\n", " 4853,\n", " 4834,\n", " 4835,\n", " 4836,\n", " 4837,\n", " 4838,\n", " 4837,\n", " 4833,\n", " 4843,\n", " 4844,\n", " 4825,\n", " 4826,\n", " 4832,\n", " 4833,\n", " 4832,\n", " 4840,\n", " 4844,\n", " 4838,\n", " 4839,\n", " 4862,\n", " 4867,\n", " 4868,\n", " 4873,\n", " 4877,\n", " 4881,\n", " 4888,\n", " 4891,\n", " 4896,\n", " 4911,\n", " 4910,\n", " 4911,\n", " 4906,\n", " 4913,\n", " 4915,\n", " 4918,\n", " 4926,\n", " 4933,\n", " 4968,\n", " 4969,\n", " 4970,\n", " 4974,\n", " ...]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_list = [int(height) for height in open(INPUT_FILE, 'r')]\n", "input_list" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 188, 192, 193, ..., 10397, 10406, 10415], dtype=uint64)" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "input_list_np = np.loadtxt(INPUT_FILE, dtype='uint64')\n", "input_list_np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Part 1" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOLUTION: 1754, TIME: 0.3273ms\n" ] } ], "source": [ "start_t = time()\n", "solution = sum([x > y for x, y in zip(input_list[1:], input_list[:-1])])\n", "end_t = time()\n", "print(f'SOLUTION: {solution}, TIME: {round((end_t - start_t) * 1000, 4)}ms')" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOLUTION: 1754, TIME: 0.1428ms\n" ] } ], "source": [ "start_t = time()\n", "solution = np.greater(input_list_np[1:], input_list_np[:-1]).sum()\n", "end_t = time()\n", "print(f'SOLUTION: {solution}, TIME: {round((end_t - start_t) * 1000, 4)}ms')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Part 2" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "WINDOW_SIZE = 3" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOLUTION: 1789, TIME: 1.07ms\n" ] } ], "source": [ "start_t = time()\n", "windows_sum = [sum(input_list[i:i + WINDOW_SIZE]) for i in range(len(input_list) - WINDOW_SIZE + 1)]\n", "solution = sum([x > y for x, y in zip(windows_sum[1:], windows_sum[:-1])])\n", "end_t = time()\n", "print(f'SOLUTION: {solution}, TIME: {round((end_t - start_t) * 1000, 4)}ms')" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SOLUTION: 1789, TIME: 0.5288ms\n" ] } ], "source": [ "start_t = time()\n", "windows_sum = np.sum(sliding_window_view(input_list_np, WINDOW_SIZE), axis=1)\n", "solution = np.greater(windows_sum[1:], windows_sum[:-1]).sum()\n", "end_t = time()\n", "print(f'SOLUTION: {solution}, TIME: {round((end_t - start_t) * 1000, 4)}ms')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }