WARNING: This is my first attempt at #adventofcode. My solution will the most inefficient and convoluted Tidyverse R code you have seen this year- hacky code is all I know.
library(datapasta)
library(tidyverse)
The Day 1 challenge is something about sonar measurements and the data can be found here. It is just a list of numbers (aka a vector) so the first challenge is to read the data into RStudio. The datapasta package by Miles McBain might be helpful. It allows you to select, copy, and paste rectangular data into R.
Start by selecting the data and using Ctrl-C to copy. Iāve only ever used the dataframe function from this package so lets see if that works. Put your cursor in a code chunk and type df_paste(). Run that line of code and it will paste the data on your clipboard into the chunk. Add a object name to assign the data to and run the chunk again.
df_paste()
Add a object name to assign the data to and run the chunk again. See what that looks like by unfolding the code chunk below.
input <- data.frame(
depth = c(109L,117L,118L,98L,102L,94L,
101L,109L,121L,126L,117L,116L,134L,119L,98L,97L,96L,
98L,101L,107L,123L,134L,137L,160L,171L,188L,189L,
188L,169L,163L,182L,184L,187L,184L,196L,199L,218L,
222L,223L,218L,214L,220L,215L,223L,226L,228L,236L,
237L,268L,277L,282L,284L,285L,288L,289L,313L,322L,
331L,335L,331L,316L,323L,314L,313L,314L,301L,302L,
320L,315L,323L,324L,346L,347L,362L,376L,377L,378L,
379L,381L,391L,392L,395L,391L,393L,399L,401L,403L,
428L,423L,422L,427L,430L,432L,431L,433L,449L,456L,
455L,456L,457L,456L,460L,461L,460L,459L,460L,467L,
468L,505L,506L,513L,505L,509L,510L,511L,521L,522L,
523L,525L,521L,514L,517L,510L,507L,534L,543L,554L,
552L,578L,587L,593L,598L,591L,592L,579L,580L,576L,
594L,593L,594L,598L,610L,611L,609L,607L,606L,609L,
616L,617L,605L,604L,616L,628L,641L,642L,643L,644L,
641L,652L,645L,637L,652L,651L,648L,649L,647L,650L,
651L,652L,660L,675L,672L,643L,624L,633L,635L,626L,
642L,645L,651L,658L,671L,674L,684L,687L,668L,675L,
650L,662L,680L,679L,701L,704L,716L,720L,727L,736L,
734L,738L,735L,742L,754L,781L,777L,778L,792L,795L,
797L,796L,816L,819L,829L,839L,846L,839L,864L,865L,
868L,869L,873L,878L,876L,878L,881L,882L,909L,922L,
929L,944L,945L,944L,945L,954L,958L,959L,963L,967L,
960L,957L,965L,986L,1006L,986L,1007L,1010L,1011L,
1000L,1001L,970L,974L,972L,974L,982L,990L,988L,1002L,
1003L,1027L,1037L,1034L,1045L,1036L,1038L,1036L,
1016L,1015L,1032L,1038L,1040L,1041L,1043L,1046L,1088L,
1105L,1112L,1113L,1114L,1117L,1132L,1130L,1132L,1121L,
1124L,1112L,1117L,1118L,1122L,1123L,1154L,1155L,
1153L,1150L,1151L,1167L,1171L,1172L,1173L,1174L,1181L,
1183L,1193L,1194L,1227L,1231L,1234L,1253L,1256L,
1255L,1242L,1255L,1276L,1280L,1297L,1298L,1308L,1309L,
1307L,1325L,1327L,1329L,1325L,1319L,1323L,1324L,
1327L,1361L,1364L,1368L,1360L,1365L,1348L,1357L,1358L,
1359L,1372L,1373L,1377L,1381L,1383L,1385L,1384L,1387L,
1382L,1381L,1410L,1405L,1412L,1413L,1416L,1417L,
1423L,1434L,1437L,1442L,1443L,1445L,1460L,1461L,1459L,
1479L,1452L,1475L,1492L,1503L,1508L,1507L,1479L,
1480L,1472L,1471L,1478L,1479L,1456L,1459L,1469L,1457L,
1460L,1465L,1469L,1477L,1478L,1482L,1485L,1486L,
1485L,1463L,1473L,1477L,1480L,1482L,1493L,1494L,1495L,
1507L,1502L,1501L,1496L,1494L,1495L,1493L,1490L,1497L,
1521L,1530L,1524L,1525L,1521L,1529L,1545L,1529L,
1526L,1538L,1525L,1505L,1506L,1511L,1512L,1513L,1512L,
1515L,1548L,1555L,1557L,1575L,1576L,1598L,1596L,
1609L,1605L,1608L,1621L,1631L,1632L,1645L,1647L,1660L,
1665L,1693L,1703L,1697L,1699L,1711L,1701L,1702L,
1703L,1715L,1716L,1717L,1693L,1692L,1697L,1669L,1670L,
1673L,1690L,1697L,1705L,1707L,1726L,1720L,1721L,1726L,
1732L,1731L,1739L,1749L,1737L,1713L,1718L,1734L,
1735L,1742L,1745L,1742L,1741L,1742L,1743L,1776L,1779L,
1797L,1801L,1802L,1804L,1806L,1807L,1803L,1804L,
1806L,1817L,1815L,1802L,1803L,1809L,1811L,1814L,1811L,
1815L,1817L,1818L,1817L,1808L,1819L,1824L,1829L,
1830L,1817L,1819L,1820L,1821L,1822L,1823L,1827L,1828L,
1829L,1832L,1834L,1839L,1845L,1848L,1857L,1859L,1879L,
1880L,1885L,1888L,1887L,1912L,1910L,1917L,1929L,
1930L,1931L,1932L,1930L,1914L,1917L,1904L,1910L,1913L,
1901L,1903L,1904L,1900L,1896L,1917L,1921L,1910L,
1914L,1915L,1917L,1915L,1916L,1939L,1940L,1941L,1942L,
1943L,1946L,1947L,1962L,1946L,1949L,1938L,1935L,
1927L,1928L,1931L,1932L,1933L,1931L,1957L,1951L,1956L,
1957L,1958L,1969L,1967L,1966L,1992L,1991L,1997L,1999L,
2010L,2016L,2039L,2043L,2047L,2048L,2046L,2064L,
2069L,2072L,2084L,2094L,2103L,2104L,2105L,2080L,2090L,
2092L,2079L,2094L,2100L,2097L,2105L,2104L,2092L,
2093L,2094L,2100L,2105L,2106L,2122L,2126L,2128L,2134L,
2141L,2153L,2154L,2157L,2170L,2171L,2158L,2156L,
2180L,2181L,2167L,2182L,2188L,2166L,2162L,2167L,2163L,
2181L,2191L,2192L,2195L,2237L,2236L,2249L,2255L,2258L,
2271L,2272L,2257L,2260L,2263L,2264L,2281L,2282L,
2285L,2289L,2293L,2279L,2284L,2287L,2292L,2291L,2303L,
2304L,2302L,2306L,2324L,2323L,2327L,2330L,2321L,
2332L,2316L,2320L,2323L,2336L,2332L,2303L,2304L,2303L,
2304L,2308L,2311L,2324L,2325L,2347L,2348L,2345L,
2340L,2348L,2347L,2348L,2346L,2352L,2345L,2346L,2347L,
2350L,2354L,2359L,2363L,2369L,2374L,2378L,2377L,2393L,
2400L,2403L,2405L,2404L,2411L,2417L,2415L,2408L,
2407L,2409L,2419L,2420L,2446L,2448L,2450L,2451L,2446L,
2447L,2448L,2450L,2451L,2449L,2448L,2450L,2453L,
2454L,2460L,2463L,2465L,2474L,2471L,2473L,2471L,2485L,
2487L,2483L,2501L,2494L,2519L,2517L,2516L,2517L,
2520L,2523L,2524L,2489L,2491L,2494L,2493L,2504L,2507L,
2512L,2513L,2511L,2512L,2488L,2495L,2484L,2476L,2477L,
2480L,2479L,2487L,2495L,2492L,2493L,2513L,2512L,
2506L,2513L,2514L,2515L,2499L,2506L,2510L,2505L,2506L,
2512L,2513L,2515L,2527L,2515L,2516L,2533L,2534L,
2535L,2536L,2541L,2548L,2586L,2588L,2601L,2604L,2614L,
2616L,2615L,2622L,2624L,2639L,2638L,2642L,2647L,
2637L,2654L,2613L,2614L,2618L,2609L,2608L,2609L,2613L,
2634L,2631L,2651L,2658L,2660L,2670L,2674L,2675L,2678L,
2682L,2686L,2680L,2683L,2689L,2691L,2690L,2691L,
2697L,2700L,2704L,2716L,2720L,2723L,2737L,2734L,2737L,
2736L,2737L,2740L,2741L,2743L,2737L,2738L,2737L,
2746L,2752L,2748L,2749L,2755L,2756L,2758L,2774L,2784L,
2785L,2800L,2802L,2811L,2813L,2825L,2819L,2822L,
2823L,2829L,2824L,2823L,2824L,2805L,2808L,2806L,2821L,
2820L,2826L,2834L,2833L,2837L,2850L,2853L,2857L,2869L,
2868L,2870L,2891L,2890L,2895L,2896L,2905L,2926L,
2928L,2926L,2930L,2929L,2938L,2970L,2976L,2968L,2967L,
2988L,3000L,3001L,3002L,3003L,3020L,3021L,3025L,
3035L,3041L,3040L,3014L,3013L,3019L,3024L,3025L,3035L,
3036L,3032L,3054L,3065L,3064L,3053L,3054L,3058L,
3052L,3061L,3063L,3064L,3068L,3096L,3095L,3091L,3114L,
3124L,3130L,3137L,3146L,3147L,3148L,3142L,3144L,3145L,
3150L,3151L,3149L,3158L,3146L,3144L,3135L,3153L,
3166L,3168L,3167L,3151L,3147L,3155L,3156L,3159L,3156L,
3159L,3160L,3175L,3174L,3188L,3182L,3175L,3185L,
3189L,3194L,3169L,3173L,3176L,3177L,3169L,3137L,3138L,
3148L,3139L,3141L,3142L,3162L,3163L,3168L,3164L,
3168L,3167L,3176L,3173L,3197L,3198L,3199L,3216L,3232L,
3245L,3272L,3295L,3294L,3291L,3301L,3303L,3304L,3306L,
3307L,3306L,3312L,3291L,3292L,3290L,3266L,3271L,
3261L,3267L,3268L,3288L,3300L,3289L,3297L,3264L,3271L,
3255L,3256L,3263L,3266L,3269L,3308L,3323L,3320L,
3326L,3332L,3334L,3335L,3341L,3369L,3375L,3365L,3359L,
3362L,3365L,3370L,3371L,3364L,3365L,3366L,3375L,
3376L,3392L,3393L,3428L,3458L,3474L,3475L,3484L,3490L,
3496L,3512L,3510L,3512L,3513L,3516L,3517L,3540L,3550L,
3549L,3540L,3552L,3559L,3549L,3550L,3568L,3569L,
3568L,3572L,3576L,3578L,3580L,3581L,3583L,3586L,3590L,
3574L,3576L,3579L,3563L,3558L,3557L,3554L,3556L,
3564L,3561L,3563L,3583L,3582L,3590L,3594L,3583L,3578L,
3596L,3602L,3615L,3621L,3625L,3631L,3633L,3655L,
3636L,3639L,3640L,3641L,3644L,3646L,3652L,3655L,3671L,
3681L,3685L,3692L,3673L,3691L,3696L,3692L,3695L,3704L,
3719L,3721L,3728L,3737L,3738L,3754L,3757L,3754L,
3759L,3758L,3765L,3767L,3770L,3774L,3769L,3756L,3755L,
3750L,3780L,3782L,3781L,3785L,3788L,3771L,3774L,
3775L,3764L,3742L,3743L,3755L,3759L,3757L,3767L,3771L,
3770L,3776L,3778L,3773L,3774L,3775L,3778L,3777L,
3764L,3773L,3767L,3768L,3769L,3792L,3810L,3808L,3791L,
3789L,3790L,3816L,3838L,3841L,3849L,3850L,3849L,3853L,
3854L,3878L,3880L,3892L,3911L,3886L,3885L,3889L,
3892L,3893L,3918L,3919L,3915L,3916L,3913L,3928L,3955L,
3956L,3961L,3959L,3963L,3962L,3964L,3973L,3969L,
3972L,3973L,3974L,3993L,3995L,3998L,4018L,4026L,4027L,
4026L,4027L,4024L,4039L,4052L,4055L,4056L,4060L,
4077L,4069L,4066L,4067L,4071L,4102L,4099L,4106L,4112L,
4114L,4125L,4130L,4127L,4157L,4147L,4148L,4127L,4130L,
4131L,4130L,4129L,4134L,4124L,4132L,4133L,4134L,
4153L,4154L,4157L,4154L,4187L,4168L,4165L,4170L,4172L,
4205L,4203L,4204L,4174L,4175L,4172L,4169L,4171L,
4198L,4203L,4215L,4209L,4212L,4213L,4229L,4230L,4232L,
4233L,4237L,4242L,4243L,4268L,4277L,4278L,4284L,
4293L,4298L,4302L,4281L,4282L,4280L,4271L,4264L,4265L,
4258L,4269L,4268L,4273L,4276L,4284L,4270L,4271L,4266L,
4275L,4305L,4309L,4314L,4316L,4324L,4323L,4335L,
4338L,4339L,4368L,4369L,4368L,4373L,4375L,4366L,4370L,
4376L,4375L,4374L,4377L,4378L,4379L,4380L,4392L,
4395L,4391L,4389L,4390L,4391L,4400L,4393L,4391L,4396L,
4364L,4351L,4385L,4387L,4391L,4395L,4387L,4407L,
4408L,4410L,4426L,4449L,4450L,4460L,4461L,4460L,4465L,
4469L,4472L,4479L,4489L,4492L,4508L,4512L,4527L,4537L,
4544L,4552L,4553L,4561L,4566L,4567L,4561L,4559L,
4550L,4543L,4549L,4554L,4557L,4558L,4570L,4585L,4595L,
4597L,4584L,4583L,4605L,4603L,4606L,4607L,4608L,
4612L,4611L,4610L,4620L,4621L,4613L,4615L,4605L,4617L,
4626L,4650L,4655L,4665L,4664L,4663L,4665L,4669L,
4668L,4670L,4662L,4666L,4665L,4660L,4661L,4673L,4676L,
4679L,4685L,4687L,4708L,4704L,4707L,4717L,4718L,4716L,
4724L,4715L,4718L,4721L,4714L,4738L,4739L,4740L,
4753L,4756L,4757L,4763L,4754L,4755L,4747L,4760L,4761L,
4760L,4781L,4774L,4776L,4784L,4786L,4785L,4783L,
4798L,4800L,4796L,4795L,4798L,4800L,4801L,4805L,4794L,
4800L,4819L,4823L,4824L,4822L,4816L,4820L,4826L,
4831L,4830L,4832L,4833L,4827L,4822L,4826L,4825L,4824L,
4823L,4824L,4844L,4849L,4853L,4858L,4861L,4859L,4856L,
4841L,4843L,4844L,4843L,4858L,4859L,4864L,4860L,
4857L,4869L,4901L,4902L,4907L,4908L,4909L,4911L,4914L,
4918L,4906L,4907L,4912L,4902L,4911L,4888L,4890L,
4888L,4885L,4887L,4873L,4874L,4880L,4891L,4890L,4891L,
4892L,4909L,4910L,4930L,4932L,4937L,4940L,4948L,
4950L,4945L,4948L,4949L,4950L,4946L,4945L,4936L,4954L,
4955L,4942L,4952L,4956L,4959L,4958L,4960L,4958L,4964L,
4972L,4974L,4973L,4974L,4988L,4989L,4956L,4960L,
4959L,4966L,4968L,4961L,4965L,4971L,4979L,4983L,4990L,
4991L,4990L,4993L,4997L,5016L,5010L,5008L,5009L,
4997L,4998L,4999L,5008L,5021L,5022L,5023L,5024L,5030L,
5014L,5017L,5022L,5018L,5017L,5021L,5023L,5002L,
5003L,5004L,5010L,4998L,4999L,5008L,5007L,5011L,5013L,
5039L,5041L,5043L,5046L,5057L,5055L,5017L,5015L,5010L,
5011L,5017L,5011L,5019L,5020L,5022L,5023L,5032L,
5033L,5042L,5062L,5083L,5092L,5093L,5081L,5082L,5085L,
5086L,5085L,5087L,5099L,5096L,5085L,5088L,5087L,
5090L,5091L,5104L,5092L,5093L,5081L,5080L,5082L,5086L,
5087L,5088L,5098L,5097L,5101L,5114L,5117L,5120L,
5128L,5129L,5126L,5127L,5128L,5129L,5135L,5138L,5142L,
5145L,5146L,5169L,5170L,5172L,5168L,5172L,5189L,5190L,
5201L,5222L,5219L,5220L,5237L,5238L,5245L,5286L,
5280L,5293L,5294L,5306L,5305L,5308L,5317L,5320L,5322L,
5347L,5363L,5361L,5359L,5365L,5373L,5375L,5379L,
5378L,5379L,5381L,5388L,5386L,5389L,5401L,5402L,5401L,
5403L,5409L,5404L,5409L,5414L,5415L,5434L,5436L,
5444L,5448L,5465L,5464L,5465L,5466L,5468L,5469L,5456L,
5460L,5443L,5445L,5443L,5439L,5438L,5445L,5452L,5484L,
5487L,5481L,5488L,5489L,5504L,5513L,5536L,5539L,
5547L,5571L,5584L,5597L,5601L,5572L,5575L,5582L,5583L,
5586L,5591L,5590L,5603L,5598L,5603L,5588L,5570L,
5571L,5573L,5575L,5594L,5595L,5599L,5602L,5610L,5641L,
5639L,5647L,5656L,5663L,5661L,5664L,5651L,5645L,
5650L,5658L,5659L,5660L,5655L,5654L,5655L,5673L,5672L,
5655L,5653L,5661L,5666L,5669L,5671L,5675L,5681L,5692L,
5685L,5684L,5685L,5684L,5724L,5725L,5732L,5731L,
5732L,5733L,5728L,5743L,5742L,5767L,5775L,5779L,5783L,
5774L,5778L,5783L,5795L,5797L,5801L,5795L,5794L,
5795L,5790L,5789L,5791L,5789L,5791L,5792L,5788L,5789L,
5798L,5799L,5792L,5802L,5801L,5808L,5805L,5806L,
5799L,5795L,5812L,5816L,5818L,5794L,5779L,5781L,5792L,
5799L,5814L,5805L,5809L,5811L,5813L,5816L,5820L,5835L,
5830L,5829L,5857L,5862L,5863L,5854L,5849L,5852L,
5855L,5853L,5852L,5853L,5857L,5858L,5867L,5863L,5865L,
5864L,5875L,5900L,5874L,5883L,5886L,5915L,5913L,
5914L,5882L,5888L,5889L,5890L,5891L,5880L,5883L,5901L,
5903L,5904L,5907L,5910L,5913L,5928L,5954L,5963L,
5964L,5967L,5975L,5976L,5973L,5979L,5980L,5979L,5990L,
5989L,6002L,5998L,6006L,6012L,6007L,6014L,6019L,6020L,
6019L,6008L,6022L,6032L,6028L,6010L,6009L,6022L,
6023L,6025L,6030L,6037L,6038L,6041L,6035L,6025L,6020L,
6036L,6026L,6027L,6032L,6041L,6034L,6036L,6044L,
6055L,6061L,6101L,6102L,6123L,6128L,6129L,6131L,6141L,
6142L,6143L,6144L,6143L,6140L,6143L,6153L,6154L,
6168L,6170L,6167L,6174L,6180L,6160L,6162L,6167L,6163L,
6160L,6167L,6166L,6167L,6164L,6153L,6155L,6185L,6193L,
6170L,6190L,6201L,6238L,6225L,6223L,6222L,6225L,
6227L,6232L,6229L,6230L,6234L,6239L,6240L,6254L,6253L,
6240L,6252L,6273L,6266L,6276L,6277L,6276L,6299L,
6300L,6299L,6289L,6291L,6289L,6287L,6288L,6287L,6289L,
6292L,6293L,6297L,6308L)
)
Apparently this is data from a submarine sonar sweep and the task is to work out how many of these 2000 depth values increase from the previous measurement.
glimpse(input)
## Rows: 2,000
## Columns: 1
## $ depth <int> 109, 117, 118, 98, 102, 94, 101, 109, 121, 126, 117, 116, 134, 1ā¦
The lag() function from dplyr could be useful here. It will find the previous value of a vector- maybe I can mutate a new column that pulls each of the previous values. Then mutate another column that tests whether the previous value is higher or lower.
output1 <- input %>%
mutate(prev_depth = lag(depth))
head(output1)
## depth prev_depth
## 1 109 NA
## 2 117 109
## 3 118 117
## 4 98 118
## 5 102 98
## 6 94 102
OK that seemed to work, now lets add another column that tests whether the depth > prev_depth
output2 <- output1 %>%
mutate(test = depth > prev_depth)
head(output2)
## depth prev_depth test
## 1 109 NA NA
## 2 117 109 TRUE
## 3 118 117 TRUE
## 4 98 118 FALSE
## 5 102 98 TRUE
## 6 94 102 FALSE
Awesome! OK now I just need to count how many values in the test column are TRUE.
output2 %>%
count(test)
## test n
## 1 FALSE 551
## 2 TRUE 1448
## 3 NA 1
Well- by my count 1448 depth values are greater than the previous value, is that right?
woot!
Instructions for part 2 here. OK this is about to get harder- apparently every measurement isnāt so useful so we need to create a sliding window, adding together the previous and next values with the current one. The lag() function pulls the previous measurement and the lead() function pulls the next one. Maybe I can mutate a new column that adds the value, its lag and its lead together??
output3 <- input %>%
mutate(slide = lag(depth) + depth + lead(depth))
Great! OK now like before work out whether each consecutive value is larger than the previousā¦
output4 <- output3 %>%
mutate(prev_slide = lag(slide)) %>%
mutate(test = slide > prev_slide)
⦠and then count how many are larger.
output4 %>%
count(test)
## test n
## 1 FALSE 526
## 2 TRUE 1471
## 3 NA 3
I say 1471⦠is that right?
YAY!
Day 1 done⦠how long will it take for this challenge to get out of my league?