Skip to content

Commit

Permalink
2022 Day 16
Browse files Browse the repository at this point in the history
  • Loading branch information
mimmackk committed Dec 28, 2024
1 parent 97d9aaf commit fd6c906
Show file tree
Hide file tree
Showing 13 changed files with 884 additions and 27 deletions.
127 changes: 124 additions & 3 deletions 2022/R/day16.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,135 @@ input <- read_lines("../input/day16.txt", skip_empty_rows = TRUE) |>

## Part 1

Represent tunnels and valves as a graph:

```{r}
edges <- input |>
g <- input |>
unnest(target) |>
pmap(function(source, target, ...) c(source, target)) |>
unique() |>
unlist() |>
make_graph(directed = TRUE)
make_graph(directed = TRUE) |>
as_undirected()
```

Get the list of valves with nonzero flow:

```{r}
flows <- input |>
filter(rate > 0 | source == "AA") |>
select(source, rate) |>
deframe()
non_init_flows <- flows |>
discard_at("AA")
valves <- names(flows)
dists <- distances(g, valves, valves)
```

List all permutations of possible valves to visit with a total distance less than 30:

```{r}
get_path <- function(choices, last, cur_length, max_length) {
if (cur_length >= max_length)
return(head(last, -1))
if (length(choices) == 0)
return(last)
ls <- list()
for (valve in choices) {
ls <- append(
ls,
list(get_path(
choices[choices != valve],
c(last, valve),
cur_length + dists[tail(last, 1), valve] + 1,
max_length
))
)
}
ls |>
discard(is_null) |>
list_flatten() |>
unique()
}
combos <- get_path(names(non_init_flows), names(flows["AA"]), 0, 30)
```

Compute total pressure released for each permutation:

```{r}
get_pressures <- function(paths, max_time) {
map_dbl(paths, \(path) {
valve <- tail(path, -1)
valve_lag <- head(path, -1)
flow <- tail(flows[path], -1)
dist <- map2_int(valve_lag, valve, \(src, target) dists[src, target])
time_start <- cumsum(dist) + 1:length(dist) + 1
pressure <- (max_time - time_start + 1) * flow
sum(pressure[time_start <= max_time])
})
}
pressures <- get_pressures(combos, 30)
```

Find the permutation that gives the maximum pressure:

```{r}
max_idx <- which.max(pressures)
pressures[max_idx]
```

## Part 2

List all permutations of possible valves to visit with a total distance less than 26:

```{r}
el_combos <- get_path(names(non_init_flows), names(flows["AA"]), 0, 26)
el_pressures <- get_pressures(el_combos, 26)
```

For each set of permutations, get the best pressure.

```{r}
el_best <- tibble(valves = map(el_combos, sort), pressure = el_pressures) |>
slice_max(pressure, by = valves, with_ties = FALSE)
```

Get best combinations of permutations between yourself and the elephant:

```{r}
el_best |>
rename(el_valves = valves, el_pressure = pressure) |>
pmap_dbl(\(el_valves, el_pressure) {
el_valves <- el_valves[el_valves != "AA"]
el_pressure + el_best |>
filter(map_lgl(valves, ~ length(intersect(.x, el_valves)) == 0)) |>
pull(pressure) |>
max()
}) |>
max()
plot(edges)
```

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

## Setup

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

```{r}
```

71 changes: 61 additions & 10 deletions 2022/input/day16.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
Valve BB has flow rate=13; tunnels lead to valves CC, AA
Valve CC has flow rate=2; tunnels lead to valves DD, BB
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
Valve EE has flow rate=3; tunnels lead to valves FF, DD
Valve FF has flow rate=0; tunnels lead to valves EE, GG
Valve GG has flow rate=0; tunnels lead to valves FF, HH
Valve HH has flow rate=22; tunnel leads to valve GG
Valve II has flow rate=0; tunnels lead to valves AA, JJ
Valve JJ has flow rate=21; tunnel leads to valve II
Valve RT has flow rate=0; tunnels lead to valves EN, LZ
Valve VB has flow rate=0; tunnels lead to valves SZ, BF
Valve AD has flow rate=0; tunnels lead to valves EB, JF
Valve RE has flow rate=4; tunnels lead to valves QB, IF, XT, WF, KW
Valve RL has flow rate=0; tunnels lead to valves DQ, LZ
Valve OK has flow rate=0; tunnels lead to valves QH, BF
Valve RV has flow rate=0; tunnels lead to valves IU, JF
Valve TE has flow rate=0; tunnels lead to valves HE, XF
Valve WW has flow rate=0; tunnels lead to valves QH, YZ
Valve HB has flow rate=15; tunnel leads to valve OM
Valve IY has flow rate=14; tunnels lead to valves UH, KW, BN, LW, UY
Valve QF has flow rate=0; tunnels lead to valves JF, PL
Valve YZ has flow rate=0; tunnels lead to valves JG, WW
Valve QB has flow rate=0; tunnels lead to valves SP, RE
Valve SO has flow rate=0; tunnels lead to valves QH, SZ
Valve EB has flow rate=7; tunnels lead to valves IF, NH, AD, VI, DQ
Valve VL has flow rate=0; tunnels lead to valves JF, YV
Valve BF has flow rate=18; tunnels lead to valves OK, VB, OH, SX
Valve UC has flow rate=0; tunnels lead to valves SC, YV
Valve OQ has flow rate=0; tunnels lead to valves XT, AA
Valve YV has flow rate=6; tunnels lead to valves YX, TT, VL, UC, NH
Valve KJ has flow rate=0; tunnels lead to valves OH, JG
Valve QH has flow rate=20; tunnels lead to valves SO, OK, WW
Valve KW has flow rate=0; tunnels lead to valves RE, IY
Valve PL has flow rate=0; tunnels lead to valves JG, QF
Valve DQ has flow rate=0; tunnels lead to valves EB, RL
Valve AA has flow rate=0; tunnels lead to valves YI, EN, UK, OQ, VI
Valve XT has flow rate=0; tunnels lead to valves OQ, RE
Valve SZ has flow rate=24; tunnels lead to valves VB, SO
Valve IU has flow rate=25; tunnels lead to valves RV, HE, HQ
Valve OM has flow rate=0; tunnels lead to valves NY, HB
Valve YX has flow rate=0; tunnels lead to valves YV, SI
Valve SX has flow rate=0; tunnels lead to valves ZB, BF
Valve KD has flow rate=0; tunnels lead to valves XF, LW
Valve SP has flow rate=0; tunnels lead to valves XF, QB
Valve UY has flow rate=0; tunnels lead to valves UK, IY
Valve XF has flow rate=22; tunnels lead to valves SP, TE, KD, NY
Valve SC has flow rate=0; tunnels lead to valves LZ, UC
Valve UK has flow rate=0; tunnels lead to valves UY, AA
Valve LW has flow rate=0; tunnels lead to valves KD, IY
Valve FL has flow rate=0; tunnels lead to valves BN, LZ
Valve VI has flow rate=0; tunnels lead to valves AA, EB
Valve HW has flow rate=0; tunnels lead to valves JF, CY
Valve YI has flow rate=0; tunnels lead to valves AA, TT
Valve HE has flow rate=0; tunnels lead to valves IU, TE
Valve JG has flow rate=10; tunnels lead to valves PL, YZ, SI, KJ
Valve BN has flow rate=0; tunnels lead to valves IY, FL
Valve IF has flow rate=0; tunnels lead to valves EB, RE
Valve JF has flow rate=19; tunnels lead to valves HW, QF, VL, RV, AD
Valve SI has flow rate=0; tunnels lead to valves JG, YX
Valve WF has flow rate=0; tunnels lead to valves LZ, RE
Valve HQ has flow rate=0; tunnels lead to valves IU, UH
Valve LZ has flow rate=5; tunnels lead to valves SC, FL, WF, RL, RT
Valve UH has flow rate=0; tunnels lead to valves IY, HQ
Valve CY has flow rate=21; tunnel leads to valve HW
Valve NH has flow rate=0; tunnels lead to valves EB, YV
Valve TT has flow rate=0; tunnels lead to valves YV, YI
Valve OH has flow rate=0; tunnels lead to valves KJ, BF
Valve EN has flow rate=0; tunnels lead to valves RT, AA
Valve NY has flow rate=0; tunnels lead to valves OM, XF
Valve ZB has flow rate=8; tunnel leads to valve SX
4 changes: 2 additions & 2 deletions _freeze/2022/R/day16/execute-results/html.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"hash": "6a25bc5739310e419033dcbae5670a06",
"hash": "ba1db0b8363fc37a2239575abad8dd21",
"result": {
"engine": "knitr",
"markdown": "---\ntitle: \"Day 16\"\ndate: 2022-12-16\nauthor:\n name: https://adventofcode.com/2022/day/16\n url: https://adventofcode.com/2022/day/16\n---\n\n\n\n\n\n## Setup\n\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Libraries\nlibrary(tidyverse)\nlibrary(igraph)\n\n# Read input from file\ninput <- read_lines(\"../input/day16.txt\", skip_empty_rows = TRUE) |> \n unglue::unglue_data(\n c(\n \"Valve {source} has flow rate={rate}; tunnels lead to valves {target}\",\n \"Valve {source} has flow rate={rate}; tunnel leads to valve {target}\"\n ),\n convert = TRUE\n ) |> \n mutate(target = map(target, \\(x) str_split_1(x, \", \")))\n```\n:::\n\n\n\n\n\n## Part 1\n\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nedges <- input |>\n unnest(target) |> \n pmap(function(source, target, ...) c(source, target)) |> \n unique() |> \n unlist() |> \n make_graph(directed = TRUE)\n\nplot(edges)\n```\n\n::: {.cell-output-display}\n![](day16_files/figure-html/unnamed-chunk-1-1.png){width=672}\n:::\n:::\n",
"markdown": "---\ntitle: \"Day 16\"\ndate: 2022-12-16\nauthor:\n name: https://adventofcode.com/2022/day/16\n url: https://adventofcode.com/2022/day/16\n---\n\n\n\n\n## Setup\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Libraries\nlibrary(tidyverse)\nlibrary(igraph)\n\n# Read input from file\ninput <- read_lines(\"../input/day16.txt\", skip_empty_rows = TRUE) |> \n unglue::unglue_data(\n c(\n \"Valve {source} has flow rate={rate}; tunnels lead to valves {target}\",\n \"Valve {source} has flow rate={rate}; tunnel leads to valve {target}\"\n ),\n convert = TRUE\n ) |> \n mutate(target = map(target, \\(x) str_split_1(x, \", \")))\n```\n:::\n\n\n\n\n## Part 1\n\nRepresent tunnels and valves as a graph:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\ng <- input |>\n unnest(target) |> \n pmap(function(source, target, ...) c(source, target)) |> \n unique() |> \n unlist() |> \n make_graph(directed = TRUE) |> \n as_undirected()\n```\n:::\n\n\n\n\nGet the list of valves with nonzero flow:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nflows <- input |> \n filter(rate > 0 | source == \"AA\") |> \n select(source, rate) |> \n deframe()\n\nnon_init_flows <- flows |> \n discard_at(\"AA\")\n\nvalves <- names(flows)\n\ndists <- distances(g, valves, valves)\n```\n:::\n\n\n\n\nList all permutations of possible valves to visit with a total distance less than 30:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nget_path <- function(choices, last, cur_length, max_length) {\n if (cur_length >= max_length)\n return(head(last, -1))\n if (length(choices) == 0)\n return(last)\n \n ls <- list()\n for (valve in choices) {\n ls <- append(\n ls, \n list(get_path(\n choices[choices != valve], \n c(last, valve), \n cur_length + dists[tail(last, 1), valve] + 1,\n max_length\n ))\n )\n }\n ls |> \n discard(is_null) |> \n list_flatten() |> \n unique()\n}\n\ncombos <- get_path(names(non_init_flows), names(flows[\"AA\"]), 0, 30)\n```\n:::\n\n\n\n\nCompute total pressure released for each permutation:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nget_pressures <- function(paths, max_time) {\n map_dbl(paths, \\(path) {\n valve <- tail(path, -1)\n valve_lag <- head(path, -1)\n flow <- tail(flows[path], -1)\n \n dist <- map2_int(valve_lag, valve, \\(src, target) dists[src, target])\n time_start <- cumsum(dist) + 1:length(dist) + 1\n pressure <- (max_time - time_start + 1) * flow\n \n sum(pressure[time_start <= max_time])\n })\n}\n\npressures <- get_pressures(combos, 30)\n```\n:::\n\n\n\n\nFind the permutation that gives the maximum pressure:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nmax_idx <- which.max(pressures)\npressures[max_idx]\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 1947\n```\n\n\n:::\n:::\n\n\n\n\n## Part 2\n\nList all permutations of possible valves to visit with a total distance less than 26:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nel_combos <- get_path(names(non_init_flows), names(flows[\"AA\"]), 0, 26)\nel_pressures <- get_pressures(el_combos, 26)\n```\n:::\n\n\n\n\nFor each set of permutations, get the best pressure.\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nel_best <- tibble(valves = map(el_combos, sort), pressure = el_pressures) |> \n slice_max(pressure, by = valves, with_ties = FALSE)\n```\n:::\n\n\n\n\nGet best combinations of permutations between yourself and the elephant:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nel_best |> \n rename(el_valves = valves, el_pressure = pressure) |> \n pmap_dbl(\\(el_valves, el_pressure) {\n el_valves <- el_valves[el_valves != \"AA\"]\n el_pressure + el_best |>\n filter(map_lgl(valves, ~ length(intersect(.x, el_valves)) == 0)) |> \n pull(pressure) |> \n max()\n }) |> \n max()\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 2556\n```\n\n\n:::\n:::\n",
"supporting": [
"day16_files"
],
Expand Down
2 changes: 2 additions & 0 deletions _freeze/site_libs/quarto-listing/list.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit fd6c906

Please sign in to comment.