Skip to content

Commit

Permalink
2022 Days 20 & 21
Browse files Browse the repository at this point in the history
  • Loading branch information
mimmackk committed Jan 13, 2025
1 parent 6e71733 commit 44a3920
Show file tree
Hide file tree
Showing 52 changed files with 2,910 additions and 2 deletions.
79 changes: 79 additions & 0 deletions 2022/R/day20.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
title: "Day 20"
date: 2022-12-20
author:
name: https://adventofcode.com/2022/day/20
url: https://adventofcode.com/2022/day/20
---

## Setup

```{r setup}
# Libraries
library(tidyverse)
# Read input from file
input <- read_lines("../input/day20.txt", skip_empty_rows = TRUE) |>
as.numeric()
```

## Part 1

```{r}
# Shift the value at the given index by n steps
shift_value <- function(vec, idx_old, n) {
idx_new <- (idx_old + n - 2) %% (length(vec) - 1) + 1
value <- vec[idx_old]
vec |>
discard_at(idx_old) |>
append(value, after = idx_new)
}
# Mix the given list of integers in order by shifting values one-by-one
mix <- function(file, n = 1) {
ids <- 1:length(file)
for (rep in 1:n) {
for (i in 1:length(file)) {
ids <- shift_value(ids, which(ids == i), file[i])
}
}
file[ids]
}
# Sum the grove coordinates in a given vector
grove_coords <- function(vec) {
c(1000, 2000, 3000) |>
map_dbl(~ vec[(which(vec == 0) + .x) %% length(vec)]) |>
sum()
}
```

Run on puzzle input:

```{r}
input |>
mix() |>
grove_coords()
```

## Part 2

Apply decryption key and mix 10 times:

```{r}
(input * 811589153) |>
mix(n = 10) |>
grove_coords()
```

134 changes: 134 additions & 0 deletions 2022/R/day21.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: "Day 21"
date: 2022-12-21
author:
name: https://adventofcode.com/2022/day/21
url: https://adventofcode.com/2022/day/21
---

## Setup

```{r setup}
# Libraries
library(tidyverse)
# Read input from file
input <- read_lines("../input/day21.txt", skip_empty_rows = TRUE) |>
unglue::unglue_data(
patterns = c(
"{monkey}: {input_1} {operation} {input_2}",
"{monkey}: {number}"
),
convert = TRUE
)
```

## Part 1

Iteratively apply each monkey-to-monnkey operation until complete:

```{r}
numbers <- input |>
select(monkey, number) |>
deframe()
eqns <- input |>
filter(!is.na(operation)) |>
select(-number)
compute_monkeys <- function(numbers, eqns) {
repeat {
new_values <- eqns |>
mutate(
value_1 = numbers[input_1],
value_2 = numbers[input_2]
) |>
drop_na(value_1, value_2) |>
mutate(output = pmap_dbl(
list(operation, value_1, value_2),
~ get(..1)(..2, ..3)
)) |>
select(monkey, output) |>
deframe()
if (length(new_values) == 0) {
break
} else {
numbers[names(new_values)] <- new_values
eqns <- filter(eqns, !(monkey %in% names(new_values)))
}
}
numbers
}
```

Run on puzzle input:

```{r}
compute_monkeys(numbers, eqns) |>
keep_at("root") |>
format(scientific = FALSE)
```


## Part 2

```{r}
# Replace 'humn' number with NA and re-compute known equation values
modified_numbers <- compute_monkeys(
modify_at(numbers, "humn", ~ NA),
mutate(eqns, operation = if_else(monkey == "root", "==", operation))
)
monkey_funcs <- eqns |>
# Reformat equations
mutate(operation = if_else(monkey == "root", "==", operation)) |>
mutate(
value_1 = modified_numbers[input_1],
value_2 = modified_numbers[input_2],
const = unname(coalesce(value_1, value_2)),
xposn = if_else(is.na(value_1), 1, 2),
input = if_else(is.na(value_1), input_1, input_2),
) |>
filter(is.na(value_1) | is.na(value_2)) |>
# Convert each operation into an inverse function
mutate(
f = pmap(list(operation, const, xposn), \(op, const, xposn) {
if (op == "+") partial(`-`, ... = , const)
else if (op == "*") partial(`/`, ... = , const)
else if (op == "-" & xposn == 1) partial(`+`, ... = , const)
else if (op == "/" & xposn == 1) partial(`*`, ... = , const)
else if (op == "-" & xposn == 2) partial(`-`, const, ... = )
else if (op == "/" & xposn == 2) partial(`/`, const, ... = )
}),
) |>
select(output = monkey, f, input, const)
# Initiate starting monkey value at the root monkey
cur_monkey <- filter(monkey_funcs, output == "root")
cur_value <- cur_monkey$const
next_monkey <- cur_monkey$input
# Compute function inverse for each monkey until "humn" is reached
while (next_monkey != "humn") {
cur_monkey <- filter(monkey_funcs, output == next_monkey)
cur_value <- cur_monkey$f[[1]](cur_value)
next_monkey <- cur_monkey$input
}
# View final input needed to achieve equality
cur_value |>
format(scientific = FALSE)
```

15 changes: 15 additions & 0 deletions _freeze/2022/R/day20/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hash": "01a4fa6a715128b32ddaf05dbb8c69f9",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Day 20\"\ndate: 2022-12-20\nauthor:\n name: https://adventofcode.com/2022/day/20\n url: https://adventofcode.com/2022/day/20\n---\n\n\n\n\n## Setup\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Libraries\nlibrary(tidyverse)\n\n# Read input from file\ninput <- read_lines(\"../input/day20.txt\", skip_empty_rows = TRUE) |> \n as.numeric()\n```\n:::\n\n\n\n\n## Part 1\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Shift the value at the given index by n steps\nshift_value <- function(vec, idx_old, n) {\n idx_new <- (idx_old + n - 2) %% (length(vec) - 1) + 1\n value <- vec[idx_old]\n \n vec |> \n discard_at(idx_old) |> \n append(value, after = idx_new)\n}\n\n# Mix the given list of integers in order by shifting values one-by-one \nmix <- function(file, n = 1) {\n ids <- 1:length(file)\n \n for (rep in 1:n) {\n for (i in 1:length(file)) {\n ids <- shift_value(ids, which(ids == i), file[i])\n }\n }\n \n file[ids]\n}\n\n# Sum the grove coordinates in a given vector\ngrove_coords <- function(vec) {\n c(1000, 2000, 3000) |> \n map_dbl(~ vec[(which(vec == 0) + .x) %% length(vec)]) |> \n sum()\n}\n```\n:::\n\n\n\n\nRun on puzzle input: \n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\ninput |> \n mix() |> \n grove_coords()\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 2215\n```\n\n\n:::\n:::\n\n\n\n\n## Part 2\n\nApply decryption key and mix 10 times:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n(input * 811589153) |> \n mix(n = 10) |> \n grove_coords()\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 8927480683\n```\n\n\n:::\n:::\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
15 changes: 15 additions & 0 deletions _freeze/2022/R/day21/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"hash": "a643653d5f363db6b9db1d6107541342",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Day 21\"\ndate: 2022-12-21\nauthor:\n name: https://adventofcode.com/2022/day/21\n url: https://adventofcode.com/2022/day/21\n---\n\n\n\n\n## Setup\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Libraries\nlibrary(tidyverse)\n\n# Read input from file\ninput <- read_lines(\"../input/day21.txt\", skip_empty_rows = TRUE) |> \n unglue::unglue_data(\n patterns = c(\n \"{monkey}: {input_1} {operation} {input_2}\",\n \"{monkey}: {number}\"\n ),\n convert = TRUE\n )\n```\n:::\n\n\n\n\n## Part 1\n\nIteratively apply each monkey-to-monnkey operation until complete:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nnumbers <- input |> \n select(monkey, number) |> \n deframe()\n\neqns <- input |> \n filter(!is.na(operation)) |> \n select(-number)\n\ncompute_monkeys <- function(numbers, eqns) {\n repeat {\n new_values <- eqns |> \n mutate(\n value_1 = numbers[input_1],\n value_2 = numbers[input_2]\n ) |> \n drop_na(value_1, value_2) |> \n mutate(output = pmap_dbl(\n list(operation, value_1, value_2), \n ~ get(..1)(..2, ..3)\n )) |> \n select(monkey, output) |> \n deframe()\n \n if (length(new_values) == 0) {\n break\n } else {\n numbers[names(new_values)] <- new_values\n eqns <- filter(eqns, !(monkey %in% names(new_values)))\n }\n }\n \n numbers\n}\n```\n:::\n\n\n\n\nRun on puzzle input:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\ncompute_monkeys(numbers, eqns) |> \n keep_at(\"root\") |> \n format(scientific = FALSE)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n root \n\"87457751482938\" \n```\n\n\n:::\n:::\n\n\n\n\n\n## Part 2\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Replace 'humn' number with NA and re-compute known equation values\nmodified_numbers <- compute_monkeys(\n modify_at(numbers, \"humn\", ~ NA),\n mutate(eqns, operation = if_else(monkey == \"root\", \"==\", operation))\n)\n\nmonkey_funcs <- eqns |> \n \n # Reformat equations\n mutate(operation = if_else(monkey == \"root\", \"==\", operation)) |> \n mutate(\n value_1 = modified_numbers[input_1], \n value_2 = modified_numbers[input_2],\n const = unname(coalesce(value_1, value_2)),\n xposn = if_else(is.na(value_1), 1, 2),\n input = if_else(is.na(value_1), input_1, input_2),\n ) |> \n filter(is.na(value_1) | is.na(value_2)) |> \n \n # Convert each operation into an inverse function\n mutate(\n f = pmap(list(operation, const, xposn), \\(op, const, xposn) {\n if (op == \"+\") partial(`-`, ... = , const)\n else if (op == \"*\") partial(`/`, ... = , const)\n else if (op == \"-\" & xposn == 1) partial(`+`, ... = , const)\n else if (op == \"/\" & xposn == 1) partial(`*`, ... = , const)\n else if (op == \"-\" & xposn == 2) partial(`-`, const, ... = )\n else if (op == \"/\" & xposn == 2) partial(`/`, const, ... = )\n }),\n ) |> \n select(output = monkey, f, input, const)\n\n# Initiate starting monkey value at the root monkey\ncur_monkey <- filter(monkey_funcs, output == \"root\")\ncur_value <- cur_monkey$const\nnext_monkey <- cur_monkey$input\n\n# Compute function inverse for each monkey until \"humn\" is reached\nwhile (next_monkey != \"humn\") {\n cur_monkey <- filter(monkey_funcs, output == next_monkey)\n cur_value <- cur_monkey$f[[1]](cur_value)\n next_monkey <- cur_monkey$input\n}\n\n# View final input needed to achieve equality\ncur_value |> \n format(scientific = FALSE)\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] \"3221245824363\"\n```\n\n\n:::\n:::\n",
"supporting": [],
"filters": [
"rmarkdown/pagebreak.lua"
],
"includes": {},
"engineDependencies": {},
"preserve": {},
"postProcess": true
}
}
12 changes: 12 additions & 0 deletions docs/2022/R/day01.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ <h1 class="quarto-secondary-nav-title">Day 1</h1>
<a href="../../2022/R/day19.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 19</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day20.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 20</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day21.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 21</span></a>
</div>
</li>
</ul>
</li>
Expand Down
12 changes: 12 additions & 0 deletions docs/2022/R/day02.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ <h1 class="quarto-secondary-nav-title">Day 2</h1>
<a href="../../2022/R/day19.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 19</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day20.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 20</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day21.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 21</span></a>
</div>
</li>
</ul>
</li>
Expand Down
12 changes: 12 additions & 0 deletions docs/2022/R/day03.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ <h1 class="quarto-secondary-nav-title">Day 3</h1>
<a href="../../2022/R/day19.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 19</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day20.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 20</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day21.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 21</span></a>
</div>
</li>
</ul>
</li>
Expand Down
12 changes: 12 additions & 0 deletions docs/2022/R/day04.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ <h1 class="quarto-secondary-nav-title">Day 4</h1>
<a href="../../2022/R/day19.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 19</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day20.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 20</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day21.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 21</span></a>
</div>
</li>
</ul>
</li>
Expand Down
12 changes: 12 additions & 0 deletions docs/2022/R/day05.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ <h1 class="quarto-secondary-nav-title">Day 5</h1>
<a href="../../2022/R/day19.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 19</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day20.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 20</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day21.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 21</span></a>
</div>
</li>
</ul>
</li>
Expand Down
12 changes: 12 additions & 0 deletions docs/2022/R/day06.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ <h1 class="quarto-secondary-nav-title">Day 6</h1>
<a href="../../2022/R/day19.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 19</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day20.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 20</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day21.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 21</span></a>
</div>
</li>
</ul>
</li>
Expand Down
12 changes: 12 additions & 0 deletions docs/2022/R/day07.html
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,18 @@ <h1 class="quarto-secondary-nav-title">Day 7</h1>
<a href="../../2022/R/day19.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 19</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day20.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 20</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="../../2022/R/day21.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Day 21</span></a>
</div>
</li>
</ul>
</li>
Expand Down
Loading

0 comments on commit 44a3920

Please sign in to comment.