From 83ebc9f5327ccd3c85f7d53882b56c43197116db Mon Sep 17 00:00:00 2001 From: Robin D Date: Mon, 2 Dec 2024 16:57:34 -0800 Subject: [PATCH] try full rebuild --- 2023/day/2/{index.qmd => _index.qmd} | 2 ++ 2023/day/2/index_files/execute-results/html.json | 14 ++++++++++++++ .../2022/day/1/index/execute-results/html.json | 9 ++++++--- .../2023/day/1/index/execute-results/html.json | 9 ++++++--- .../2023/day/3/index/execute-results/html.json | 15 +++++++++++++++ index.qmd | 1 - 6 files changed, 43 insertions(+), 7 deletions(-) rename 2023/day/2/{index.qmd => _index.qmd} (99%) create mode 100644 2023/day/2/index_files/execute-results/html.json create mode 100644 _freeze/2023/day/3/index/execute-results/html.json diff --git a/2023/day/2/index.qmd b/2023/day/2/_index.qmd similarity index 99% rename from 2023/day/2/index.qmd rename to 2023/day/2/_index.qmd index 23ec446..0e99b2f 100644 --- a/2023/day/2/index.qmd +++ b/2023/day/2/_index.qmd @@ -5,6 +5,8 @@ categories: - dplyr - tidyr draft: false +execute: + eval: false --- ## Setup diff --git a/2023/day/2/index_files/execute-results/html.json b/2023/day/2/index_files/execute-results/html.json new file mode 100644 index 0000000..e6a02d8 --- /dev/null +++ b/2023/day/2/index_files/execute-results/html.json @@ -0,0 +1,14 @@ +{ + "hash": "801bb6ae33a6d5ec0506c3b869a053de", + "result": { + "markdown": "---\ntitle: \"2023: Day 2 - Cube Conundrum\"\ndate: 2023-12-2\ncategories:\n - dplyr\n - tidyr\ndraft: false\n---\n\n\n## Setup\n\n[The original challenge](https://adventofcode.com/2023/day/2)\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(aochelpers)\nlibrary(tidyr)\nlibrary(dplyr)\ninput <- aoc_input_data_frame(2, 2023)\nhead(input)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 6 × 2\n X1 X2 \n \n1 Game 1 \" 1 green, 4 blue; 1 blue, 2 green, 1 red; 1 red, 1 green, 2 blue; 1 g…\n2 Game 2 \" 2 blue, 2 red, 6 green; 1 red, 6 green, 7 blue; 10 green, 8 blue, 1 …\n3 Game 3 \" 6 green, 5 blue, 9 red; 4 blue, 1 green, 13 red; 9 green, 14 red, 1 …\n4 Game 4 \" 14 green, 3 blue, 16 red; 20 red; 4 green, 2 red, 1 blue; 10 blue, 1…\n5 Game 5 \" 5 green, 4 blue; 1 red, 3 blue, 2 green; 4 green, 2 red, 15 blue; 11…\n6 Game 6 \" 6 blue, 10 green; 2 red, 6 green, 2 blue; 4 red, 4 blue, 1 green; 2 …\n```\n:::\n\n```{.r .cell-code}\nnames(input) <- c(\"game\", \"set\")\n```\n:::\n\n\n# TLDR; Solutions\n\n## Part 1 ⭐\n\n::: callout-danger\n### ❓Which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?\n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nset.results <- input |> \n\t# get game numbers\n\tmutate(game = as.numeric(gsub(\"Game \",\"\", game))) |>\n\t# split out the sets\n\tseparate_wider_delim(set, delim = \";\", names_sep = \"\", too_few = \"align_start\") |>\n\t# one row per set\n\tpivot_longer(contains(\"set\"), names_to = \"set\", values_to = \"cube\") |>\n\t# split out the colors\n\tseparate_wider_delim(cube, delim = \",\", names_sep = \"\", too_few = \"align_start\") |>\n\t# one row per cube\n\tpivot_longer(cube1:cube3, names_to = \"cube\", values_to = \"color\") |>\n\t# cleanup\n\tmutate(color = trimws(color)) |>\n\tselect(-cube) |> \n\tna.omit() |>\n\t# split out number of colors now\n\tseparate_wider_delim(color, delim = \" \", names_sep = \"\", too_few = \"align_start\") |>\n\tmutate(color1 = as.numeric(color1)) |>\n\t# put colors into separate columns\n\tpivot_wider(names_from = color2, values_from = color1) |>\n\treplace_na(list(blue=0, red=0, green=0))\n\n\tset.results %>%\n\tmutate(validity = ifelse(red >12 | green > 13 | blue >14, \n\t\t\t\t\t\t\t\t\t\t\t\t\t \"invalid\", \"valid\")) |>\n\t# flag games with invalid combos\n\tgroup_by(game) |> summarize(n.invalid = sum(validity==\"invalid\")) |>\n\t# keep only valid games\n\tungroup() |>\n\tfilter(n.invalid ==0) |>\n\t# solution input is the sum of valid game numbers\n\tsummarise(sum(game))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 1 × 1\n `sum(game)`\n \n1 2486\n```\n:::\n:::\n\n\n## Part 2 ⭐⭐\n\n::: callout-danger\n### ❓What is the fewest number of cubes of each color that could have been in the bag to make the game possible?\n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nset.results |> \n\tgroup_by(game) |>\n\t# find max number of each color per game\n\tsummarize(min.green = max(green), \n\t\t\t\t min.red = max(red), \n\t\t\t\t min.blue = max(blue)) |>\n\t# calculate submission value\n\tmutate(power = min.green*min.red*min.blue) |>\n\tsummarize(sum(power))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 1 × 1\n `sum(power)`\n \n1 87984\n```\n:::\n:::\n\n\n# Walkthrough / Explainer\n\nI was launched successfully to Snow Island, which has a noticeable lack of snow. While we're walking I'm playing a game with a local Elf. \"Guess how many cubes are in the bag\". Kinda..\n\n## Part 1\n\n:::{.callout-exa icon=true} Example Data\n\n\n::: {.cell}\n\n```{.r .cell-code}\nexa <- data.frame(input = \n\t\tc(\"Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green\", \n\t\t\t\"Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue\",\n \t\t\"Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red\",\n \t\t\"Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red\",\n \t\t\"Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green\"))\n\nexa <- exa |> \n\tseparate_wider_delim(input, delim = \":\", names = c(\"game\", \"set\"))\n```\n:::\n\n:::\n\nMy solution I think is going to use a series of `separates` and `pivot`ing to get the games as rows, colors on the columns and number of colors as values. Then I can compare each row with the total number of cubes to see which games were possible. \n\nGet number of each color cube per game/set combo.\n\n::: {.cell}\n\n```{.r .cell-code}\n\t# get game numbers\nset.results <- input |> \n\tmutate(game = as.numeric(gsub(\"Game \",\"\", game))) |>\n\t# split out the sets\n\tseparate_wider_delim(set, delim = \";\", names_sep = \"\", too_few = \"align_start\") |>\n\t# one row per set\n\tpivot_longer(contains(\"set\"), names_to = \"set\", values_to = \"cube\") |>\n\t# split out the colors\n\tseparate_wider_delim(cube, delim = \",\", names_sep = \"\", too_few = \"align_start\") |>\n\t# one row per cube\n\tpivot_longer(cube1:cube3, names_to = \"cube\", values_to = \"color\") |>\n\t# cleanup\n\tmutate(color = trimws(color)) |>\n\tselect(-cube) |> \n\tna.omit() |>\n\t# split out number of colors now\n\tseparate_wider_delim(color, delim = \" \", names_sep = \"\", too_few = \"align_start\") |>\n\tmutate(color1 = as.numeric(color1)) |>\n\t# put colors into separate columns\n\tpivot_wider(names_from = color2, values_from = color1) |>\n\treplace_na(list(blue=0, red=0, green=0))\n\nhead(set.results)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 6 × 5\n game set green blue red\n \n1 1 set1 1 4 0\n2 1 set2 2 1 1\n3 1 set3 1 2 1\n4 1 set4 1 0 1\n5 1 set5 1 0 0\n6 1 set6 1 1 1\n```\n:::\n:::\n\n\nNow compare each set result against the proposed pool of 12 red cubes, 13 green cubes, and 14 blue cubes. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nset.results %>%\n\tmutate(validity = ifelse(red >12 | green > 13 | blue >14, \n\t\t\t\t\t\t\t\t\t\t\t\t\t \"invalid\", \"valid\")) |>\n\t# flag games with invalid combos\n\tgroup_by(game) |> summarize(n.invalid = sum(validity==\"invalid\")) |>\n\t# keep only valid games\n\tungroup() |>\n\tfilter(n.invalid ==0) |>\n\t# solution input is the sum of valid game numbers\n\tsummarise(sum(game))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 1 × 1\n `sum(game)`\n \n1 2486\n```\n:::\n:::\n\n\n⭐\n\n\n## Part 2\n\nSo I find out that much like California, they're in a drought. So no water, no snow. So while we go check on the water, the elf asks another question about said game. What is the fewest number of cubes of each color that could have been in the bag to make the game possible?\n\n\n::: {.cell}\n\n```{.r .cell-code}\nset.results |> \n\tgroup_by(game) |>\n\t# find max number of each color per game\n\tsummarize(min.green = max(green), \n\t\t\t\t min.red = max(red), \n\t\t\t\t min.blue = max(blue)) |>\n\t# calculate submission value\n\tmutate(power = min.green*min.red*min.blue) |>\n\tsummarize(sum(power))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 1 × 1\n `sum(power)`\n \n1 87984\n```\n:::\n:::\n\n\nRight on the first try! Today's challenge was relativly straight forward, and my approach to the first challenge made the second part very easy. \n\n⭐⭐\n\n##### Session info {.appendix}\n\n
\n\nToggle\n\n\n::: {.cell}\n::: {.cell-output .cell-output-stdout}\n```\n─ Session info ───────────────────────────────────────────────────────────────\n setting value\n version R version 4.3.1 (2023-06-16 ucrt)\n os Windows 11 x64 (build 22000)\n system x86_64, mingw32\n ui RTerm\n language (EN)\n collate English_United States.utf8\n ctype English_United States.utf8\n tz America/Los_Angeles\n date 2023-12-02\n pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)\n\n─ Packages ───────────────────────────────────────────────────────────────────\n package * version date (UTC) lib source\n aochelpers * 0.1.0.9000 2023-11-30 [1] Github (EllaKaye/aochelpers@c2afc01)\n dplyr * 1.1.2 2023-04-20 [1] CRAN (R 4.3.1)\n tidyr * 1.3.0 2023-01-24 [1] CRAN (R 4.3.1)\n\n [1] C:/Users/renta/AppData/Local/R/win-library/4.3\n [2] C:/Program Files/R/R-4.3.1/library\n\n──────────────────────────────────────────────────────────────────────────────\n```\n:::\n:::\n\n\n
\n", + "supporting": [], + "filters": [ + "rmarkdown/pagebreak.lua" + ], + "includes": {}, + "engineDependencies": {}, + "preserve": {}, + "postProcess": true + } +} \ No newline at end of file diff --git a/_freeze/2022/day/1/index/execute-results/html.json b/_freeze/2022/day/1/index/execute-results/html.json index 678c4a8..5a311d3 100644 --- a/_freeze/2022/day/1/index/execute-results/html.json +++ b/_freeze/2022/day/1/index/execute-results/html.json @@ -1,8 +1,11 @@ { - "hash": "6d3bab2c5a94b922d4bfc13cee02fb4b", + "hash": "e68ce47cb4f0736c967941fd0867e05e", "result": { - "markdown": "---\ntitle: \"2022: Day 1\"\ndate: 2022-12-1\ncategories: [base R, lists]\ndraft: false\n---\n\n\n## Setup\n\n[The original challenge](https://adventofcode.com/2022/day/1)\n\n## My solution \n\n\n::: {.cell}\n\n```{.r .cell-code}\n# packages\nlibrary(dplyr)\n\n# import data\nraw <- read.delim(\"input.txt\", blank.lines.skip = FALSE, header = FALSE)\n\nraw <- rename(raw, calories = V1)\n```\n:::\n\n\nAssociate each entry with an elf. Each blank line indicates a new elf.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nnext.elf <- c(1, which(is.na(raw))) # where do the next elfs start?\nn.elf <- length(next.elf) # how many new elfs?\nraw$elfID <- 0 # set index\n```\n:::\n\n\nindex elves\n\n::: {.cell}\n\n```{.r .cell-code}\nfor(e in 1:(n.elf-1)){\n raw$elfID[next.elf[e]:(next.elf[e+1]-1)] <- e\n}\n```\n:::\n\n\nadd the last elf\n\n::: {.cell}\n\n```{.r .cell-code}\nraw$elfID[next.elf[e+1]:NROW(raw)] <- n.elf\n```\n:::\n\n\nnow drop empty rows\n\n::: {.cell}\n\n```{.r .cell-code}\nelves <- na.omit(raw)\n```\n:::\n\n\n:::{.callout-warning icon=false}\n### ❓ Which elf has the most calories? \n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nelves %>% group_by(elfID) %>%\n summarize(tot.cals = sum(calories)) %>%\n arrange(desc(tot.cals)) %>% slice(1)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n# A tibble: 1 × 2\n elfID tot.cals\n \n1 189 68775\n```\n:::\n:::\n\n\n\n:::{.callout-warning icon=false}\n### ❓ How many calories are carried by the top three elves?\n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntop3 <- elves %>% group_by(elfID) %>%\n summarize(tot.cals = sum(calories)) %>%\n arrange(desc(tot.cals)) %>% slice(1:3)\nsum(top3$tot.cals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 202585\n```\n:::\n:::\n", - "supporting": [], + "engine": "knitr", + "markdown": "---\ntitle: \"2022: Day 1\"\ndate: 2022-12-1\ncategories: [base R, lists]\ndraft: false\n---\n\n\n## Setup\n\n[The original challenge](https://adventofcode.com/2022/day/1)\n\n## My solution \n\n\n::: {.cell}\n\n```{.r .cell-code}\n# packages\nlibrary(dplyr)\n\n# import data\nraw <- read.delim(\"input.txt\", blank.lines.skip = FALSE, header = FALSE)\n\nraw <- rename(raw, calories = V1)\n```\n:::\n\n\nAssociate each entry with an elf. Each blank line indicates a new elf.\n\n\n::: {.cell}\n\n```{.r .cell-code}\nnext.elf <- c(1, which(is.na(raw))) # where do the next elfs start?\nn.elf <- length(next.elf) # how many new elfs?\nraw$elfID <- 0 # set index\n```\n:::\n\n\nindex elves\n\n::: {.cell}\n\n```{.r .cell-code}\nfor(e in 1:(n.elf-1)){\n raw$elfID[next.elf[e]:(next.elf[e+1]-1)] <- e\n}\n```\n:::\n\n\nadd the last elf\n\n::: {.cell}\n\n```{.r .cell-code}\nraw$elfID[next.elf[e+1]:NROW(raw)] <- n.elf\n```\n:::\n\n\nnow drop empty rows\n\n::: {.cell}\n\n```{.r .cell-code}\nelves <- na.omit(raw)\n```\n:::\n\n\n:::{.callout-warning icon=false}\n### ❓ Which elf has the most calories? \n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nelves %>% group_by(elfID) %>%\n summarize(tot.cals = sum(calories)) %>%\n arrange(desc(tot.cals)) %>% slice(1)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n# A tibble: 1 × 2\n elfID tot.cals\n \n1 189 68775\n```\n\n\n:::\n:::\n\n\n\n:::{.callout-warning icon=false}\n### ❓ How many calories are carried by the top three elves?\n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\ntop3 <- elves %>% group_by(elfID) %>%\n summarize(tot.cals = sum(calories)) %>%\n arrange(desc(tot.cals)) %>% slice(1:3)\nsum(top3$tot.cals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 202585\n```\n\n\n:::\n:::\n", + "supporting": [ + "index_files" + ], "filters": [ "rmarkdown/pagebreak.lua" ], diff --git a/_freeze/2023/day/1/index/execute-results/html.json b/_freeze/2023/day/1/index/execute-results/html.json index bec60b0..6409a6e 100644 --- a/_freeze/2023/day/1/index/execute-results/html.json +++ b/_freeze/2023/day/1/index/execute-results/html.json @@ -1,8 +1,11 @@ { - "hash": "4634156b0613bc5aa6628dd9bc013cb1", + "hash": "4a535403917efaa648f0753475875768", "result": { - "markdown": "---\ntitle: \"2023: Day 1\"\ndate: 2023-12-1\ncategories:\n - regex\n - loop\n - stringr\n - stringi\n - peeked\ndraft: false\n---\n\n\n# Setup\n\n[The original challenge](https://adventofcode.com/2023/day/1)\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(aochelpers) # for data loading\nlibrary(stringr); library(stringi)\ninput <- aoc_input_vector(1, 2023)\n```\n:::\n\n\n# TLDR; Solutions\n\n## Part 1 ⭐\n\n::: {.callout-danger}\n### ❓ What is my calibration number? \n\nOn each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.\n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nnums <- gsub(\"[a-z]\", \"\", input)\nfirst.num <- str_extract(nums, \"^.\")\nlast.num <- str_extract(nums, \".$\")\nas.numeric(paste0(first.num, last.num)) |> sum()\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 55386\n```\n:::\n:::\n\n\n## Part 2 ⭐⭐\n\n::: {.callout-danger}\n### ❓ What is my _actual_ calibration number? \n\n> Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid \"digits\". \n\n:::\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvalid <- c(\"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\", as.character(1:9))\nvalid.num <- c(1:9,1:9) \n\nN <- length(input)\nnew.cal.vals <- rep(0, N)\n\nfor(i in 1:N){\n\tx <- stri_locate_all_regex(input[i], valid)\n\tx.first <- lapply(x, head, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\n\tx.last <- lapply(x, tail, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\n\tfirst <- which.min(x.first[,1])\n\tlast <- which.max(x.last[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 54824\n```\n:::\n:::\n\n\n# Walkthrough / Explainer\n\n## Part 1\n\n:::{.callout-exa icon=true}\nExample Data\n\n::: {.cell}\n\n```{.r .cell-code}\nexa <- c(\"1abc2\",\"pqr3stu8vwx\",\"a1b2c3d4e5f\",\"treb7uchet\")\n```\n:::\n\n\n:::\n\nRemove all characters from the string, then grab the first and last number.\n\n::: {.cell}\n\n```{.r .cell-code}\nnums <- gsub(\"[a-z]\", \"\", input)\nfirst.num <- str_extract(nums, \"^.\")\nlast.num <- str_extract(nums, \".$\")\ncbind(nums, first.num, last.num)[c(1,4,300,304),] # verify\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n nums first.num last.num\n[1,] \"9\" \"9\" \"9\" \n[2,] \"57\" \"5\" \"7\" \n[3,] \"934\" \"9\" \"4\" \n[4,] \"83858\" \"8\" \"8\" \n```\n:::\n:::\n\n\nPaste together to create the two-digit calibration number.\n\n::: {.cell}\n\n```{.r .cell-code}\ncalibration.value <- as.numeric(paste0(first.num, last.num))\ncbind(nums, first.num, last.num, calibration.value)[c(1,4,300,304),] \n```\n\n::: {.cell-output .cell-output-stdout}\n```\n nums first.num last.num calibration.value\n[1,] \"9\" \"9\" \"9\" \"99\" \n[2,] \"57\" \"5\" \"7\" \"57\" \n[3,] \"934\" \"9\" \"4\" \"94\" \n[4,] \"83858\" \"8\" \"8\" \"88\" \n```\n:::\n:::\n\n\nSubmission value: \n\n::: {.cell}\n\n```{.r .cell-code}\nsum(calibration.value) \n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 55386\n```\n:::\n:::\n\n\n⭐\n\n## Part 2\n\n:::{.callout-exa icon=true}\nExample Data\n\n::: {.cell}\n\n```{.r .cell-code}\nexa2 <- c(\"two1nine\",\n \"eightwothree\",\n \"abcone2threexyz\",\n \"xtwone3four\",\n \"4nineeightseven2\",\n \"zoneight234\",\n \"7pqrstsixteen\", \n \"7eight7\", # 2 new test cases\n \"3stuffthree\")\n```\n:::\n\n\n:::\n\nOkay, so let's define a list of valid 'digits', and their corresponding numeric values. \n\n::: {.cell}\n\n```{.r .cell-code}\nvalid <- c(\"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\", as.character(1:9))\nvalid.num <- c(1:9,1:9) \n```\n:::\n\n\nAnd find where in the strings these valid values are located. \n`stri_locate_first_regex` find the indexes (positions) where there is a match to some pattern. \nI learned about this function from [Gus Lipkin's solution](https://adventofcode.guslipkin.me/2023/01/2023-01)\n\n\n::: {.cell}\n\n```{.r .cell-code}\nx <- stri_locate_first_regex(exa2[1], valid)\nfirst <- which.min(x[,1])\nlast <- which.max(x[,2])\ncal.val <- as.numeric(paste0(valid.num[first], valid.num[last]))\n```\n:::\n\n\nSo that gives me the correct first value. Now I need to apply this to each row. It's late, I'm tired so I'm gonna loop it. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nn <- length(exa2)\nnew.cal.vals <- rep(0, n)\nfor(i in 1:n){\n\tx <- stri_locate_first_regex(exa2[i], valid)\n\tfirst <- which.min(x[,1])\n\tlast <- which.max(x[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\ncbind(exa2, new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n exa2 new.cal.vals\n [1,] \"two1nine\" \"29\" \n [2,] \"eightwothree\" \"83\" \n [3,] \"abcone2threexyz\" \"13\" \n [4,] \"xtwone3four\" \"24\" \n [5,] \"4nineeightseven2\" \"42\" \n [6,] \"zoneight234\" \"14\" \n [7,] \"7pqrstsixteen\" \"76\" \n [8,] \"7eight7\" \"78\" \n [9,] \"3stuffthree\" \"33\" \n```\n:::\n\n```{.r .cell-code}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 392\n```\n:::\n:::\n\n\nHooray! This matches the example solution. Now to do this for my actual data. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nrm(new.cal.vals, x, first, last, i) # just cos\nN <- length(input)\nnew.cal.vals <- rep(0, N)\nfor(i in 1:N){\n\tx <- stri_locate_first_regex(input[i], valid)\n\tfirst <- which.min(x[,1])\n\tlast <- which.max(x[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 54759\n```\n:::\n:::\n\n\n❌ Nope. So I realized I was only using `stri_locate_first_regex` key is **first**. And so I was missing duplicates. I added a couple test cases `7eight7` and `3stuffthree`, and sure enough the last 7 wasn't getting caught. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nn <- length(exa2)\nnew.cal.vals <- rep(0, n)\nfor(i in 1:n){\n\tx <- stri_locate_all_regex(exa2[i], valid)\n\ty <- unlist(x) |> matrix(ncol=2, byrow=TRUE) #the output of _all_ was different\n\tfirst <- which.min(y[,1])\n\tlast <- which.max(y[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\ncbind(exa2, new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n exa2 new.cal.vals\n [1,] \"two1nine\" \"29\" \n [2,] \"eightwothree\" \"83\" \n [3,] \"abcone2threexyz\" \"13\" \n [4,] \"xtwone3four\" \"24\" \n [5,] \"4nineeightseven2\" \"42\" \n [6,] \"zoneight234\" \"14\" \n [7,] \"7pqrstsixteen\" \"76\" \n [8,] \"7eight7\" \"77\" \n [9,] \"3stuffthree\" \"33\" \n```\n:::\n\n```{.r .cell-code}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 391\n```\n:::\n:::\n\n\nLooks promising. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nrm(new.cal.vals, x, first, last, i, y, N) # just cos\nN <- length(input)\nnew.cal.vals <- rep(0, N)\nfor(i in 1:N){\n\tx <- stri_locate_all_regex(input[i], valid)\n\ty <- unlist(x) |> matrix(ncol=2, byrow=TRUE) \n\tfirst <- which.min(y[,1])\n\tlast <- which.max(y[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] NA\n```\n:::\n:::\n\n\nUhm...if there were some rows without numbers, this should have failed earlier...\n\n\n::: {.cell}\n\n```{.r .cell-code}\nis.miss <- which(is.na(new.cal.vals))\ninput[is.miss][1:5]\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] \"9sixqnine9jk9six\" \"58qtpqqz58888cmhs\" \n[3] \"88trnvjtqsmseight8\" \"962sixoneonectfgpknl8nine\" \n[5] \"twotwosevenvkzzhrpgninecqvf9\"\n```\n:::\n:::\n\n\nYea... no those for sure have numbers. Well heck. What is my function doing? \n\n\n::: {.cell}\n\n```{.r .cell-code}\n(x <- stri_locate_all_regex(input[is.miss[1]], valid))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[[1]]\n start end\n[1,] NA NA\n\n[[2]]\n start end\n[1,] NA NA\n\n[[3]]\n start end\n[1,] NA NA\n\n[[4]]\n start end\n[1,] NA NA\n\n[[5]]\n start end\n[1,] NA NA\n\n[[6]]\n start end\n[1,] 2 4\n[2,] 14 16\n\n[[7]]\n start end\n[1,] NA NA\n\n[[8]]\n start end\n[1,] NA NA\n\n[[9]]\n start end\n[1,] 6 9\n\n[[10]]\n start end\n[1,] NA NA\n\n[[11]]\n start end\n[1,] NA NA\n\n[[12]]\n start end\n[1,] NA NA\n\n[[13]]\n start end\n[1,] NA NA\n\n[[14]]\n start end\n[1,] NA NA\n\n[[15]]\n start end\n[1,] NA NA\n\n[[16]]\n start end\n[1,] NA NA\n\n[[17]]\n start end\n[1,] NA NA\n\n[[18]]\n start end\n[1,] 1 1\n[2,] 10 10\n[3,] 13 13\n```\n:::\n\n```{.r .cell-code}\n(y <- unlist(x) |> matrix(ncol=2, byrow=TRUE))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n [,1] [,2]\n [1,] NA NA\n [2,] NA NA\n [3,] NA NA\n [4,] NA NA\n [5,] NA NA\n [6,] 2 14\n [7,] 4 16\n [8,] NA NA\n [9,] NA NA\n[10,] 6 9\n[11,] NA NA\n[12,] NA NA\n[13,] NA NA\n[14,] NA NA\n[15,] NA NA\n[16,] NA NA\n[17,] NA NA\n[18,] NA NA\n[19,] 1 10\n[20,] 13 1\n[21,] 10 13\n```\n:::\n:::\n\n\nYea.. duplicate values of the same number makes for additional rows in the matrix. But then why didn't it mess up with my examples? 🤔 \n\nOkay well let's use `head` and `tail` via `lapply` to pull the first and last rows out of each list element. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nx <- stri_locate_all_regex(input[is.miss[1]], valid)\nx.first <- lapply(x, head, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\nx.last <- lapply(x, tail, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\nfirst <- which.min(x.first[,1])\nlast <- which.max(x.last[,2])\nas.numeric(paste0(valid.num[first], valid.num[last]))\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 96\n```\n:::\n\n```{.r .cell-code}\ninput[is.miss[1]]\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] \"9sixqnine9jk9six\"\n```\n:::\n:::\n\n\nThird time is the charm? 🤞\n\n\n::: {.cell}\n\n```{.r .cell-code}\nrm(new.cal.vals, x, first, last, i, y, N) # just cos\nN <- length(input)\nnew.cal.vals <- rep(0, N)\nfor(i in 1:N){\n\tx <- stri_locate_all_regex(input[i], valid)\n\tx.first <- lapply(x, head, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\n\tx.last <- lapply(x, tail, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\n\tfirst <- which.min(x.first[,1])\n\tlast <- which.max(x.last[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n```\n[1] 54824\n```\n:::\n:::\n\n\n⭐⭐\n\n\n##### Session info {.appendix}\n\n
Toggle\n\n\n::: {.cell}\n::: {.cell-output .cell-output-stdout}\n```\n─ Session info ───────────────────────────────────────────────────────────────\n setting value\n version R version 4.3.1 (2023-06-16 ucrt)\n os Windows 11 x64 (build 22000)\n system x86_64, mingw32\n ui RTerm\n language (EN)\n collate English_United States.utf8\n ctype English_United States.utf8\n tz America/Los_Angeles\n date 2023-12-02\n pandoc 3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)\n\n─ Packages ───────────────────────────────────────────────────────────────────\n package * version date (UTC) lib source\n aochelpers * 0.1.0.9000 2023-11-30 [1] Github (EllaKaye/aochelpers@c2afc01)\n stringi * 1.7.12 2023-01-11 [1] CRAN (R 4.3.0)\n stringr * 1.5.0 2022-12-02 [1] CRAN (R 4.3.1)\n\n [1] C:/Users/renta/AppData/Local/R/win-library/4.3\n [2] C:/Program Files/R/R-4.3.1/library\n\n──────────────────────────────────────────────────────────────────────────────\n```\n:::\n:::\n\n\n
\n\n\n\n\n\n", - "supporting": [], + "engine": "knitr", + "markdown": "---\ntitle: \"2023: Day 1\"\ndate: 2023-12-1\ncategories:\n - regex\n - loop\n - stringr\n - stringi\n - peeked\ndraft: false\n---\n\n\n# Setup\n\n[The original challenge](https://adventofcode.com/2023/day/1)\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(aochelpers) # for data loading\nlibrary(stringr); library(stringi)\ninput <- aoc_input_vector(1, 2023)\n```\n:::\n\n\n# TLDR; Solutions\n\n## Part 1 ⭐\n\n::: {.callout-danger}\n### ❓ What is my calibration number? \n\nOn each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.\n:::\n\n\n::: {.cell}\n\n```{.r .cell-code}\nnums <- gsub(\"[a-z]\", \"\", input)\nfirst.num <- str_extract(nums, \"^.\")\nlast.num <- str_extract(nums, \".$\")\nas.numeric(paste0(first.num, last.num)) |> sum()\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 55386\n```\n\n\n:::\n:::\n\n\n## Part 2 ⭐⭐\n\n::: {.callout-danger}\n### ❓ What is my _actual_ calibration number? \n\n> Your calculation isn't quite right. It looks like some of the digits are actually spelled out with letters: one, two, three, four, five, six, seven, eight, and nine also count as valid \"digits\". \n\n:::\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nvalid <- c(\"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\", as.character(1:9))\nvalid.num <- c(1:9,1:9) \n\nN <- length(input)\nnew.cal.vals <- rep(0, N)\n\nfor(i in 1:N){\n\tx <- stri_locate_all_regex(input[i], valid)\n\tx.first <- lapply(x, head, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\n\tx.last <- lapply(x, tail, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\n\tfirst <- which.min(x.first[,1])\n\tlast <- which.max(x.last[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 54824\n```\n\n\n:::\n:::\n\n\n# Walkthrough / Explainer\n\n## Part 1\n\n:::{.callout-exa icon=true}\nExample Data\n\n::: {.cell}\n\n```{.r .cell-code}\nexa <- c(\"1abc2\",\"pqr3stu8vwx\",\"a1b2c3d4e5f\",\"treb7uchet\")\n```\n:::\n\n\n:::\n\nRemove all characters from the string, then grab the first and last number.\n\n::: {.cell}\n\n```{.r .cell-code}\nnums <- gsub(\"[a-z]\", \"\", input)\nfirst.num <- str_extract(nums, \"^.\")\nlast.num <- str_extract(nums, \".$\")\ncbind(nums, first.num, last.num)[c(1,4,300,304),] # verify\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n nums first.num last.num\n[1,] \"9\" \"9\" \"9\" \n[2,] \"57\" \"5\" \"7\" \n[3,] \"934\" \"9\" \"4\" \n[4,] \"83858\" \"8\" \"8\" \n```\n\n\n:::\n:::\n\n\nPaste together to create the two-digit calibration number.\n\n::: {.cell}\n\n```{.r .cell-code}\ncalibration.value <- as.numeric(paste0(first.num, last.num))\ncbind(nums, first.num, last.num, calibration.value)[c(1,4,300,304),] \n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n nums first.num last.num calibration.value\n[1,] \"9\" \"9\" \"9\" \"99\" \n[2,] \"57\" \"5\" \"7\" \"57\" \n[3,] \"934\" \"9\" \"4\" \"94\" \n[4,] \"83858\" \"8\" \"8\" \"88\" \n```\n\n\n:::\n:::\n\n\nSubmission value: \n\n::: {.cell}\n\n```{.r .cell-code}\nsum(calibration.value) \n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 55386\n```\n\n\n:::\n:::\n\n\n⭐\n\n## Part 2\n\n:::{.callout-exa icon=true}\nExample Data\n\n::: {.cell}\n\n```{.r .cell-code}\nexa2 <- c(\"two1nine\",\n \"eightwothree\",\n \"abcone2threexyz\",\n \"xtwone3four\",\n \"4nineeightseven2\",\n \"zoneight234\",\n \"7pqrstsixteen\", \n \"7eight7\", # 2 new test cases\n \"3stuffthree\")\n```\n:::\n\n\n:::\n\nOkay, so let's define a list of valid 'digits', and their corresponding numeric values. \n\n::: {.cell}\n\n```{.r .cell-code}\nvalid <- c(\"one\",\"two\",\"three\",\"four\",\"five\",\"six\",\"seven\",\"eight\",\"nine\", as.character(1:9))\nvalid.num <- c(1:9,1:9) \n```\n:::\n\n\nAnd find where in the strings these valid values are located. \n`stri_locate_first_regex` find the indexes (positions) where there is a match to some pattern. \nI learned about this function from [Gus Lipkin's solution](https://adventofcode.guslipkin.me/2023/01/2023-01)\n\n\n::: {.cell}\n\n```{.r .cell-code}\nx <- stri_locate_first_regex(exa2[1], valid)\nfirst <- which.min(x[,1])\nlast <- which.max(x[,2])\ncal.val <- as.numeric(paste0(valid.num[first], valid.num[last]))\n```\n:::\n\n\nSo that gives me the correct first value. Now I need to apply this to each row. It's late, I'm tired so I'm gonna loop it. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nn <- length(exa2)\nnew.cal.vals <- rep(0, n)\nfor(i in 1:n){\n\tx <- stri_locate_first_regex(exa2[i], valid)\n\tfirst <- which.min(x[,1])\n\tlast <- which.max(x[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\ncbind(exa2, new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n exa2 new.cal.vals\n [1,] \"two1nine\" \"29\" \n [2,] \"eightwothree\" \"83\" \n [3,] \"abcone2threexyz\" \"13\" \n [4,] \"xtwone3four\" \"24\" \n [5,] \"4nineeightseven2\" \"42\" \n [6,] \"zoneight234\" \"14\" \n [7,] \"7pqrstsixteen\" \"76\" \n [8,] \"7eight7\" \"78\" \n [9,] \"3stuffthree\" \"33\" \n```\n\n\n:::\n\n```{.r .cell-code}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 392\n```\n\n\n:::\n:::\n\n\nHooray! This matches the example solution. Now to do this for my actual data. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nrm(new.cal.vals, x, first, last, i) # just cos\nN <- length(input)\nnew.cal.vals <- rep(0, N)\nfor(i in 1:N){\n\tx <- stri_locate_first_regex(input[i], valid)\n\tfirst <- which.min(x[,1])\n\tlast <- which.max(x[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 54759\n```\n\n\n:::\n:::\n\n\n❌ Nope. So I realized I was only using `stri_locate_first_regex` key is **first**. And so I was missing duplicates. I added a couple test cases `7eight7` and `3stuffthree`, and sure enough the last 7 wasn't getting caught. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nn <- length(exa2)\nnew.cal.vals <- rep(0, n)\nfor(i in 1:n){\n\tx <- stri_locate_all_regex(exa2[i], valid)\n\ty <- unlist(x) |> matrix(ncol=2, byrow=TRUE) #the output of _all_ was different\n\tfirst <- which.min(y[,1])\n\tlast <- which.max(y[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\ncbind(exa2, new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n exa2 new.cal.vals\n [1,] \"two1nine\" \"29\" \n [2,] \"eightwothree\" \"83\" \n [3,] \"abcone2threexyz\" \"13\" \n [4,] \"xtwone3four\" \"24\" \n [5,] \"4nineeightseven2\" \"42\" \n [6,] \"zoneight234\" \"14\" \n [7,] \"7pqrstsixteen\" \"76\" \n [8,] \"7eight7\" \"77\" \n [9,] \"3stuffthree\" \"33\" \n```\n\n\n:::\n\n```{.r .cell-code}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 391\n```\n\n\n:::\n:::\n\n\nLooks promising. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nrm(new.cal.vals, x, first, last, i, y, N) # just cos\nN <- length(input)\nnew.cal.vals <- rep(0, N)\nfor(i in 1:N){\n\tx <- stri_locate_all_regex(input[i], valid)\n\ty <- unlist(x) |> matrix(ncol=2, byrow=TRUE) \n\tfirst <- which.min(y[,1])\n\tlast <- which.max(y[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] NA\n```\n\n\n:::\n:::\n\n\nUhm...if there were some rows without numbers, this should have failed earlier...\n\n\n::: {.cell}\n\n```{.r .cell-code}\nis.miss <- which(is.na(new.cal.vals))\ninput[is.miss][1:5]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"9sixqnine9jk9six\" \"58qtpqqz58888cmhs\" \n[3] \"88trnvjtqsmseight8\" \"962sixoneonectfgpknl8nine\" \n[5] \"twotwosevenvkzzhrpgninecqvf9\"\n```\n\n\n:::\n:::\n\n\nYea... no those for sure have numbers. Well heck. What is my function doing? \n\n\n::: {.cell}\n\n```{.r .cell-code}\n(x <- stri_locate_all_regex(input[is.miss[1]], valid))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[[1]]\n start end\n[1,] NA NA\n\n[[2]]\n start end\n[1,] NA NA\n\n[[3]]\n start end\n[1,] NA NA\n\n[[4]]\n start end\n[1,] NA NA\n\n[[5]]\n start end\n[1,] NA NA\n\n[[6]]\n start end\n[1,] 2 4\n[2,] 14 16\n\n[[7]]\n start end\n[1,] NA NA\n\n[[8]]\n start end\n[1,] NA NA\n\n[[9]]\n start end\n[1,] 6 9\n\n[[10]]\n start end\n[1,] NA NA\n\n[[11]]\n start end\n[1,] NA NA\n\n[[12]]\n start end\n[1,] NA NA\n\n[[13]]\n start end\n[1,] NA NA\n\n[[14]]\n start end\n[1,] NA NA\n\n[[15]]\n start end\n[1,] NA NA\n\n[[16]]\n start end\n[1,] NA NA\n\n[[17]]\n start end\n[1,] NA NA\n\n[[18]]\n start end\n[1,] 1 1\n[2,] 10 10\n[3,] 13 13\n```\n\n\n:::\n\n```{.r .cell-code}\n(y <- unlist(x) |> matrix(ncol=2, byrow=TRUE))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2]\n [1,] NA NA\n [2,] NA NA\n [3,] NA NA\n [4,] NA NA\n [5,] NA NA\n [6,] 2 14\n [7,] 4 16\n [8,] NA NA\n [9,] NA NA\n[10,] 6 9\n[11,] NA NA\n[12,] NA NA\n[13,] NA NA\n[14,] NA NA\n[15,] NA NA\n[16,] NA NA\n[17,] NA NA\n[18,] NA NA\n[19,] 1 10\n[20,] 13 1\n[21,] 10 13\n```\n\n\n:::\n:::\n\n\nYea.. duplicate values of the same number makes for additional rows in the matrix. But then why didn't it mess up with my examples? 🤔 \n\nOkay well let's use `head` and `tail` via `lapply` to pull the first and last rows out of each list element. \n\n\n::: {.cell}\n\n```{.r .cell-code}\nx <- stri_locate_all_regex(input[is.miss[1]], valid)\nx.first <- lapply(x, head, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\nx.last <- lapply(x, tail, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\nfirst <- which.min(x.first[,1])\nlast <- which.max(x.last[,2])\nas.numeric(paste0(valid.num[first], valid.num[last]))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 96\n```\n\n\n:::\n\n```{.r .cell-code}\ninput[is.miss[1]]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"9sixqnine9jk9six\"\n```\n\n\n:::\n:::\n\n\nThird time is the charm? 🤞\n\n\n::: {.cell}\n\n```{.r .cell-code}\nrm(new.cal.vals, x, first, last, i, y, N) # just cos\nN <- length(input)\nnew.cal.vals <- rep(0, N)\nfor(i in 1:N){\n\tx <- stri_locate_all_regex(input[i], valid)\n\tx.first <- lapply(x, head, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\n\tx.last <- lapply(x, tail, 1) |> unlist() |> matrix(ncol=2, byrow=TRUE)\n\tfirst <- which.min(x.first[,1])\n\tlast <- which.max(x.last[,2])\n\tnew.cal.vals[i] <- as.numeric(paste0(valid.num[first], valid.num[last]))\n}\nsum(new.cal.vals)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 54824\n```\n\n\n:::\n:::\n\n\n⭐⭐\n\n\n##### Session info {.appendix}\n\n
Toggle\n\n\n::: {.cell}\n::: {.cell-output .cell-output-stdout}\n\n```\n─ Session info ───────────────────────────────────────────────────────────────\n setting value\n version R version 4.4.1 (2024-06-14)\n os macOS Sonoma 14.6.1\n system aarch64, darwin20\n ui X11\n language (EN)\n collate en_US.UTF-8\n ctype en_US.UTF-8\n tz America/Los_Angeles\n date 2024-12-02\n pandoc 3.1.11 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)\n\n─ Packages ───────────────────────────────────────────────────────────────────\n package * version date (UTC) lib source\n aochelpers * 0.1.0.9000 2024-12-02 [1] Github (EllaKaye/aochelpers@d4ccd91)\n stringi * 1.8.4 2024-05-06 [1] CRAN (R 4.4.0)\n stringr * 1.5.1 2023-11-14 [1] CRAN (R 4.4.0)\n\n [1] /Users/rdonatello/Library/R/arm64/4.4/library\n [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library\n\n──────────────────────────────────────────────────────────────────────────────\n```\n\n\n:::\n:::\n\n\n
\n\n\n\n\n\n", + "supporting": [ + "index_files" + ], "filters": [ "rmarkdown/pagebreak.lua" ], diff --git a/_freeze/2023/day/3/index/execute-results/html.json b/_freeze/2023/day/3/index/execute-results/html.json new file mode 100644 index 0000000..0a9ac6f --- /dev/null +++ b/_freeze/2023/day/3/index/execute-results/html.json @@ -0,0 +1,15 @@ +{ + "hash": "105ebf912b44e62530549c697a49413d", + "result": { + "engine": "knitr", + "markdown": "---\ntitle: \"2023: Day 3 - Gear Ratios\"\ndate: 2023-12-3\ncategories:\n - TAG_1\n - TAG_2\ndraft: true\n---\n\n\n## Setup\n\n[The original challenge](https://adventofcode.com/2023/day/3)\n\n\n::: {.cell}\n\n```{.r .cell-code}\nlibrary(aochelpers)\nlibrary(dplyr)\n# other options: aoc_input_data_frame(), aoc_input_matrix()\ninput <- aoc_input_matrix(3, 2023)\nhead(input)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14]\n[1,] \".\" \".\" \".\" \".\" \".\" \".\" \"6\" \"4\" \"4\" \".\" \".\" \".\" \".\" \".\" \n[2,] \".\" \".\" \".\" \".\" \".\" \"*\" \".\" \".\" \".\" \".\" \".\" \".\" \"3\" \"2\" \n[3,] \".\" \".\" \".\" \"9\" \"3\" \"9\" \".\" \"@\" \"2\" \"2\" \"5\" \".\" \".\" \".\" \n[4,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"4\" \"7\" \n[5,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[6,] \".\" \"6\" \"8\" \"8\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"4\" \"6\" \"9\" \n [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]\n[1,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"6\" \"1\" \"2\" \".\" \".\" \n[2,] \"1\" \".\" \".\" \"1\" \"7\" \"6\" \".\" \".\" \".\" \".\" \"+\" \".\" \n[3,] \".\" \".\" \".\" \".\" \".\" \"*\" \".\" \".\" \".\" \".\" \".\" \".\" \n[4,] \"0\" \".\" \"1\" \"2\" \"8\" \".\" \".\" \".\" \"2\" \"8\" \"8\" \".\" \n[5,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"*\" \n[6,] \"&\" \".\" \".\" \".\" \"=\" \".\" \".\" \".\" \".\" \"2\" \"8\" \"4\" \n [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36] [,37] [,38]\n[1,] \".\" \".\" \".\" \".\" \".\" \"2\" \"5\" \"4\" \".\" \".\" \"6\" \"3\" \n[2,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"&\" \".\" \".\" \".\" \"=\" \n[3,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[4,] \".\" \".\" \"+\" \".\" \".\" \".\" \".\" \".\" \".\" \"4\" \"4\" \"2\" \n[5,] \".\" \"1\" \"7\" \"6\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[6,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n [,39] [,40] [,41] [,42] [,43] [,44] [,45] [,46] [,47] [,48] [,49] [,50]\n[1,] \"8\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[2,] \".\" \".\" \".\" \"9\" \"0\" \"6\" \".\" \".\" \".\" \".\" \".\" \".\" \n[3,] \".\" \".\" \".\" \".\" \"$\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[4,] \"-\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"1\" \"9\" \"1\" \".\" \n[5,] \".\" \".\" \".\" \"6\" \"2\" \"%\" \".\" \".\" \".\" \"*\" \".\" \".\" \n[6,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"4\" \"9\" \".\" \n [,51] [,52] [,53] [,54] [,55] [,56] [,57] [,58] [,59] [,60] [,61] [,62]\n[1,] \".\" \".\" \".\" \"8\" \"0\" \"2\" \".\" \".\" \".\" \".\" \".\" \".\" \n[2,] \".\" \".\" \"*\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"=\" \"5\" \n[3,] \".\" \"4\" \"1\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[4,] \".\" \".\" \".\" \".\" \"%\" \".\" \".\" \".\" \".\" \"&\" \".\" \".\" \n[5,] \".\" \".\" \".\" \".\" \"7\" \"1\" \"5\" \".\" \"8\" \"3\" \"1\" \".\" \n[6,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n [,63] [,64] [,65] [,66] [,67] [,68] [,69] [,70] [,71] [,72] [,73] [,74]\n[1,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[2,] \"1\" \"8\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[3,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[4,] \".\" \".\" \".\" \".\" \".\" \".\" \"3\" \"6\" \"0\" \".\" \".\" \".\" \n[5,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"*\" \".\" \".\" \".\" \n[6,] \".\" \".\" \".\" \"3\" \"1\" \"8\" \".\" \".\" \"1\" \"8\" \"2\" \".\" \n [,75] [,76] [,77] [,78] [,79] [,80] [,81] [,82] [,83] [,84] [,85] [,86]\n[1,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[2,] \".\" \".\" \".\" \".\" \".\" \".\" \"9\" \"9\" \"4\" \".\" \".\" \"9\" \n[3,] \".\" \".\" \"/\" \".\" \".\" \".\" \".\" \".\" \"+\" \".\" \".\" \".\" \n[4,] \".\" \"2\" \"1\" \"8\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[5,] \".\" \".\" \".\" \".\" \".\" \".\" \"9\" \"3\" \"7\" \".\" \".\" \".\" \n[6,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n [,87] [,88] [,89] [,90] [,91] [,92] [,93] [,94] [,95] [,96] [,97] [,98]\n[1,] \".\" \".\" \".\" \"1\" \"1\" \"8\" \".\" \".\" \".\" \".\" \".\" \".\" \n[2,] \"3\" \"8\" \".\" \"*\" \".\" \".\" \".\" \".\" \".\" \"5\" \"7\" \"9\" \n[3,] \".\" \".\" \".\" \"1\" \"0\" \"2\" \".\" \".\" \".\" \".\" \"*\" \".\" \n[4,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"9\" \"1\" \"6\" \".\" \".\" \n[5,] \".\" \"7\" \"8\" \"2\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[6,] \".\" \"-\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"2\" \"0\" \n [,99] [,100] [,101] [,102] [,103] [,104] [,105] [,106] [,107] [,108]\n[1,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[2,] \".\" \".\" \".\" \".\" \"3\" \"5\" \".\" \".\" \".\" \".\" \n[3,] \".\" \".\" \".\" \".\" \"*\" \".\" \".\" \".\" \".\" \".\" \n[4,] \".\" \".\" \".\" \"5\" \"5\" \"2\" \".\" \"1\" \"7\" \"1\" \n[5,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"*\" \".\" \".\" \n[6,] \"0\" \".\" \".\" \".\" \".\" \".\" \".\" \"4\" \"8\" \"0\" \n [,109] [,110] [,111] [,112] [,113] [,114] [,115] [,116] [,117] [,118]\n[1,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[2,] \"1\" \"5\" \"5\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[3,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[4,] \".\" \".\" \"7\" \"4\" \"7\" \".\" \"5\" \"1\" \".\" \".\" \n[5,] \".\" \"*\" \".\" \".\" \".\" \".\" \"/\" \".\" \".\" \".\" \n[6,] \".\" \"1\" \"8\" \"2\" \".\" \".\" \".\" \".\" \"2\" \"5\" \n [,119] [,120] [,121] [,122] [,123] [,124] [,125] [,126] [,127] [,128]\n[1,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[2,] \".\" \".\" \".\" \".\" \"3\" \"2\" \"0\" \".\" \".\" \".\" \n[3,] \"6\" \"0\" \"3\" \".\" \".\" \".\" \".\" \"*\" \".\" \"4\" \n[4,] \".\" \".\" \".\" \".\" \"1\" \"1\" \"1\" \".\" \".\" \".\" \n[5,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[6,] \"7\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n [,129] [,130] [,131] [,132] [,133] [,134] [,135] [,136] [,137] [,138]\n[1,] \".\" \"3\" \"1\" \"7\" \".\" \"6\" \"9\" \"1\" \".\" \".\" \n[2,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \"$\" \".\" \n[3,] \"1\" \"3\" \"=\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[4,] \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n[5,] \".\" \"6\" \"4\" \"5\" \".\" \".\" \".\" \".\" \".\" \".\" \n[6,] \".\" \".\" \"%\" \".\" \".\" \".\" \".\" \".\" \".\" \".\" \n [,139] [,140]\n[1,] \".\" \".\" \n[2,] \".\" \".\" \n[3,] \".\" \".\" \n[4,] \".\" \".\" \n[5,] \".\" \".\" \n[6,] \".\" \".\" \n```\n\n\n:::\n:::\n\n\n# TLDR; Solutions\n\n## Part 1 ⭐\n\n::: {.callout-danger}\n### ❓ W\n\n:::\n\n\n## Part 2 ⭐⭐\n\n::: {.callout-danger}\n### ❓ \n\n:::\n\n\n# Walkthrough / Explainer\n\nI get to a gondola that'll take me to the water source. But ofc the gondola is broken. Why would I expect any different. Gotta find the missing part. \n\n## Part 1\n\n:::{.callout-exa icon=true}\nExample Data\n\n::: {.cell}\n\n```{.r .cell-code}\nexa <- c(\"467..114..\",\n\t\t\t\t \"...*......\",\n\t\t\t\t \"..35..633.\",\n\t\t\t\t \"......#...\",\n\t\t\t\t \"617*......\",\n\t\t\t\t \".....+.58.\",\n\t\t\t\t \"..592.....\",\n\t\t\t\t \"......755.\",\n\t\t\t\t \"...$.*....\",\n\t\t\t\t \".664.598..\")\n\n# convert to match aoc matrix input\n(exa.in <- matrix(unlist(strsplit(exa, split = \"\")), \n\t\t\t\t\t\t\tnrow = length(exa), byrow = TRUE))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n [1,] \"4\" \"6\" \"7\" \".\" \".\" \"1\" \"1\" \"4\" \".\" \".\" \n [2,] \".\" \".\" \".\" \"*\" \".\" \".\" \".\" \".\" \".\" \".\" \n [3,] \".\" \".\" \"3\" \"5\" \".\" \".\" \"6\" \"3\" \"3\" \".\" \n [4,] \".\" \".\" \".\" \".\" \".\" \".\" \"#\" \".\" \".\" \".\" \n [5,] \"6\" \"1\" \"7\" \"*\" \".\" \".\" \".\" \".\" \".\" \".\" \n [6,] \".\" \".\" \".\" \".\" \".\" \"+\" \".\" \"5\" \"8\" \".\" \n [7,] \".\" \".\" \"5\" \"9\" \"2\" \".\" \".\" \".\" \".\" \".\" \n [8,] \".\" \".\" \".\" \".\" \".\" \".\" \"7\" \"5\" \"5\" \".\" \n [9,] \".\" \".\" \".\" \"$\" \".\" \"*\" \".\" \".\" \".\" \".\" \n[10,] \".\" \"6\" \"6\" \"4\" \".\" \"5\" \"9\" \"8\" \".\" \".\" \n```\n\n\n:::\n:::\n\n\ndot's don't mean anything, so make them missing.\n\n::: {.cell}\n\n```{.r .cell-code}\nexa.in[exa.in==\".\"] <- NA\nexa.in\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n [1,] \"4\" \"6\" \"7\" NA NA \"1\" \"1\" \"4\" NA NA \n [2,] NA NA NA \"*\" NA NA NA NA NA NA \n [3,] NA NA \"3\" \"5\" NA NA \"6\" \"3\" \"3\" NA \n [4,] NA NA NA NA NA NA \"#\" NA NA NA \n [5,] \"6\" \"1\" \"7\" \"*\" NA NA NA NA NA NA \n [6,] NA NA NA NA NA \"+\" NA \"5\" \"8\" NA \n [7,] NA NA \"5\" \"9\" \"2\" NA NA NA NA NA \n [8,] NA NA NA NA NA NA \"7\" \"5\" \"5\" NA \n [9,] NA NA NA \"$\" NA \"*\" NA NA NA NA \n[10,] NA \"6\" \"6\" \"4\" NA \"5\" \"9\" \"8\" NA NA \n```\n\n\n:::\n:::\n\n\nWe only care if the number is neighbors with a symbol, so convert all symbols to something common. \n\n::: {.cell}\n\n```{.r .cell-code}\n(ex3 <- gsub(\"\\\\D\", \"*\", exa.in))\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]\n [1,] \"4\" \"6\" \"7\" NA NA \"1\" \"1\" \"4\" NA NA \n [2,] NA NA NA \"*\" NA NA NA NA NA NA \n [3,] NA NA \"3\" \"5\" NA NA \"6\" \"3\" \"3\" NA \n [4,] NA NA NA NA NA NA \"*\" NA NA NA \n [5,] \"6\" \"1\" \"7\" \"*\" NA NA NA NA NA NA \n [6,] NA NA NA NA NA \"*\" NA \"5\" \"8\" NA \n [7,] NA NA \"5\" \"9\" \"2\" NA NA NA NA NA \n [8,] NA NA NA NA NA NA \"7\" \"5\" \"5\" NA \n [9,] NA NA NA \"*\" NA \"*\" NA NA NA NA \n[10,] NA \"6\" \"6\" \"4\" NA \"5\" \"9\" \"8\" NA NA \n```\n\n\n:::\n:::\n\n\n\n\n\n\n## Part 2\n\n:::{.callout-exa icon=true}\nExample Data\n\n::: {.cell}\n\n```{.r .cell-code}\nexa <- 0\n```\n:::\n\n\n\n\n##### Session info {.appendix}\n\n
Toggle\n\n\n\n::: {.cell}\n::: {.cell-output .cell-output-stdout}\n\n```\n─ Session info ───────────────────────────────────────────────────────────────\n setting value\n version R version 4.4.1 (2024-06-14)\n os macOS Sonoma 14.6.1\n system aarch64, darwin20\n ui X11\n language (EN)\n collate en_US.UTF-8\n ctype en_US.UTF-8\n tz America/Los_Angeles\n date 2024-12-02\n pandoc 3.1.11 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/aarch64/ (via rmarkdown)\n\n─ Packages ───────────────────────────────────────────────────────────────────\n package * version date (UTC) lib source\n aochelpers * 0.1.0.9000 2024-12-02 [1] Github (EllaKaye/aochelpers@d4ccd91)\n dplyr * 1.1.4 2023-11-17 [1] CRAN (R 4.4.0)\n\n [1] /Users/rdonatello/Library/R/arm64/4.4/library\n [2] /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library\n\n──────────────────────────────────────────────────────────────────────────────\n```\n\n\n:::\n:::\n\n\n\n
\n\n\n\n\n\n", + "supporting": [], + "filters": [ + "rmarkdown/pagebreak.lua" + ], + "includes": {}, + "engineDependencies": {}, + "preserve": {}, + "postProcess": true + } +} \ No newline at end of file diff --git a/index.qmd b/index.qmd index b6a5086..963e211 100644 --- a/index.qmd +++ b/index.qmd @@ -1,7 +1,6 @@ --- title: "Advent of Code" subtitle: "My solutions and notes" -description: "My solutions and notes stuff" code-tools: false ---