Skip to content

Commit 9ec57cc

Browse files
committed
2022 Day 18
1 parent 92c4275 commit 9ec57cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1723
-278
lines changed

2022/R/day18.qmd

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: "Day 18"
3+
date: 2022-12-18
4+
author:
5+
name: https://adventofcode.com/2022/day/18
6+
url: https://adventofcode.com/2022/day/18
7+
---
8+
9+
## Setup
10+
11+
```{r setup}
12+
13+
# Libraries
14+
library(tidyverse)
15+
library(igraph)
16+
17+
# Read input from file
18+
input <- read_lines("../input/day18.txt", skip_empty_rows = TRUE) |>
19+
unglue::unglue_data("{x},{y},{z}", convert = TRUE) |>
20+
mutate(id = row_number(), .before = everything())
21+
22+
```
23+
24+
## Part 1
25+
26+
Create a containing box for the set of cubes, padded by 1 voxel of air in each direction, and convert the full rectangular area into a graph:
27+
28+
```{r}
29+
30+
xrange <- (min(input$x) - 1):(max(input$x) + 1)
31+
yrange <- (min(input$y) - 1):(max(input$y) + 1)
32+
zrange <- (min(input$z) - 1):(max(input$z) + 1)
33+
34+
# Fill out each coordinate of the containing box with air
35+
container <- input |>
36+
mutate(type = "cube") |>
37+
complete(x = xrange, y = yrange, z = zrange, fill = list(type = "air")) |>
38+
mutate(id = row_number())
39+
40+
# For each coordinate, create edges between adjacent coords of the same type
41+
edges <- container |>
42+
arrange(x, y, z) |>
43+
mutate(
44+
edge_x1 = case_when(type == lag(type) ~ lag(id)),
45+
edge_x2 = case_when(type == lead(type) ~ lead(id)),
46+
.by = c(y, z)
47+
) |>
48+
mutate(
49+
edge_y1 = case_when(type == lag(type) ~ lag(id)),
50+
edge_y2 = case_when(type == lead(type) ~ lead(id)),
51+
.by = c(x, z)
52+
) |>
53+
mutate(
54+
edge_z1 = case_when(type == lag(type) ~ lag(id)),
55+
edge_z2 = case_when(type == lead(type) ~ lead(id)),
56+
.by = c(x, y)
57+
) |>
58+
mutate(across(starts_with("edge"), \(col) {
59+
case_when(!is.na(col) ~ map2(id, col, ~ c(.x, .y)))
60+
})) |>
61+
select(starts_with("edge")) |>
62+
pivot_longer(everything()) |>
63+
pull(value) |>
64+
unlist()
65+
66+
67+
# Convert to a graph
68+
g <- make_graph(edges = edges, n = max(container$id), directed = TRUE) |>
69+
as_undirected()
70+
71+
```
72+
73+
Compute the surface area of the cubes. Start by giving every cube a surface area of 6, then subtract the cube's vertex degree (which is the number of adjacent cubes):
74+
75+
```{r}
76+
77+
# Get the vertex IDs of all cubes
78+
cube_ids <- container |>
79+
filter(type == "cube") |>
80+
pull(id)
81+
82+
# Compute the surface area of all cubes
83+
g |>
84+
degree() |>
85+
keep_at(cube_ids) |>
86+
map_dbl(~ 6 - .x) |>
87+
sum()
88+
89+
```
90+
91+
## Part 2
92+
93+
To compute the external surface area, we compute the total surface area of the outermost containing box of air, then subtract away its known rectangular external surface area.
94+
95+
```{r}
96+
97+
# First vertex is external air padding, so we pull all vertices having its group
98+
outside_ids <- which(components(g)$membership == components(g)$membership[1])
99+
100+
# Compute the surface area of the external air voxels using their vertex degree
101+
total_sa <- g |>
102+
degree() |>
103+
keep_at(outside_ids) |>
104+
map_dbl(~ 6 - .x) |>
105+
sum()
106+
107+
# Compute the outer surface of the bounding box
108+
xlen <- max(xrange) - min(xrange) + 1
109+
ylen <- max(yrange) - min(yrange) + 1
110+
zlen <- max(zrange) - min(zrange) + 1
111+
bounding_sa <- 2 * (xlen * ylen + xlen * zlen + ylen * zlen)
112+
113+
# Subtract the outer surface area from the total surface area of the air padding
114+
total_sa - bounding_sa
115+
116+
```
117+
118+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"hash": "bdaeb04d174693a0b55cd5135dbd7641",
3+
"result": {
4+
"engine": "knitr",
5+
"markdown": "---\ntitle: \"Day 18\"\ndate: 2022-12-18\nauthor:\n name: https://adventofcode.com/2022/day/18\n url: https://adventofcode.com/2022/day/18\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/day18.txt\", skip_empty_rows = TRUE) |> \n unglue::unglue_data(\"{x},{y},{z}\", convert = TRUE) |> \n mutate(id = row_number(), .before = everything())\n```\n:::\n\n\n\n\n## Part 1\n\nCreate a containing box for the set of cubes, padded by 1 voxel of air in each direction, and convert the full rectangular area into a graph:\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\nxrange <- (min(input$x) - 1):(max(input$x) + 1)\nyrange <- (min(input$y) - 1):(max(input$y) + 1)\nzrange <- (min(input$z) - 1):(max(input$z) + 1)\n\n# Fill out each coordinate of the containing box with air\ncontainer <- input |> \n mutate(type = \"cube\") |> \n complete(x = xrange, y = yrange, z = zrange, fill = list(type = \"air\")) |> \n mutate(id = row_number())\n\n# For each coordinate, create edges between adjacent coords of the same type\nedges <- container |>\n arrange(x, y, z) |> \n mutate(\n edge_x1 = case_when(type == lag(type) ~ lag(id)),\n edge_x2 = case_when(type == lead(type) ~ lead(id)),\n .by = c(y, z)\n ) |> \n mutate(\n edge_y1 = case_when(type == lag(type) ~ lag(id)),\n edge_y2 = case_when(type == lead(type) ~ lead(id)),\n .by = c(x, z)\n ) |> \n mutate(\n edge_z1 = case_when(type == lag(type) ~ lag(id)),\n edge_z2 = case_when(type == lead(type) ~ lead(id)),\n .by = c(x, y)\n ) |> \n mutate(across(starts_with(\"edge\"), \\(col) {\n case_when(!is.na(col) ~ map2(id, col, ~ c(.x, .y)))\n })) |> \n select(starts_with(\"edge\")) |> \n pivot_longer(everything()) |> \n pull(value) |> \n unlist()\n\n\n# Convert to a graph\ng <- make_graph(edges = edges, n = max(container$id), directed = TRUE) |> \n as_undirected()\n```\n:::\n\n\n\n\nCompute the surface area of the cubes. Start by giving every cube a surface area of 6, then subtract the cube's vertex degree (which is the number of adjacent cubes):\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# Get the vertex IDs of all cubes\ncube_ids <- container |> \n filter(type == \"cube\") |> \n pull(id)\n\n# Compute the surface area of all cubes\ng |> \n degree() |> \n keep_at(cube_ids) |> \n map_dbl(~ 6 - .x) |> \n sum()\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 3470\n```\n\n\n:::\n:::\n\n\n\n\n## Part 2\n\nTo compute the external surface area, we compute the total surface area of the outermost containing box of air, then subtract away its known rectangular external surface area.\n\n\n\n\n::: {.cell}\n\n```{.r .cell-code}\n# First vertex is external air padding, so we pull all vertices having its group\noutside_ids <- which(components(g)$membership == components(g)$membership[1])\n\n# Compute the surface area of the external air voxels using their vertex degree\ntotal_sa <- g |> \n degree() |> \n keep_at(outside_ids) |> \n map_dbl(~ 6 - .x) |> \n sum()\n\n# Compute the outer surface of the bounding box\nxlen <- max(xrange) - min(xrange) + 1\nylen <- max(yrange) - min(yrange) + 1\nzlen <- max(zrange) - min(zrange) + 1\nbounding_sa <- 2 * (xlen * ylen + xlen * zlen + ylen * zlen)\n\n# Subtract the outer surface area from the total surface area of the air padding\ntotal_sa - bounding_sa\n```\n\n::: {.cell-output .cell-output-stdout}\n\n```\n[1] 1986\n```\n\n\n:::\n:::\n",
6+
"supporting": [],
7+
"filters": [
8+
"rmarkdown/pagebreak.lua"
9+
],
10+
"includes": {},
11+
"engineDependencies": {},
12+
"preserve": {},
13+
"postProcess": true
14+
}
15+
}

docs/2022/R/day01.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ <h1 class="quarto-secondary-nav-title">Day 1</h1>
406406
<a href="../../2022/R/day17.html" class="sidebar-item-text sidebar-link">
407407
<span class="menu-text">Day 17</span></a>
408408
</div>
409+
</li>
410+
<li class="sidebar-item">
411+
<div class="sidebar-item-container">
412+
<a href="../../2022/R/day18.html" class="sidebar-item-text sidebar-link">
413+
<span class="menu-text">Day 18</span></a>
414+
</div>
409415
</li>
410416
</ul>
411417
</li>

docs/2022/R/day02.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ <h1 class="quarto-secondary-nav-title">Day 2</h1>
406406
<a href="../../2022/R/day17.html" class="sidebar-item-text sidebar-link">
407407
<span class="menu-text">Day 17</span></a>
408408
</div>
409+
</li>
410+
<li class="sidebar-item">
411+
<div class="sidebar-item-container">
412+
<a href="../../2022/R/day18.html" class="sidebar-item-text sidebar-link">
413+
<span class="menu-text">Day 18</span></a>
414+
</div>
409415
</li>
410416
</ul>
411417
</li>

docs/2022/R/day03.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ <h1 class="quarto-secondary-nav-title">Day 3</h1>
406406
<a href="../../2022/R/day17.html" class="sidebar-item-text sidebar-link">
407407
<span class="menu-text">Day 17</span></a>
408408
</div>
409+
</li>
410+
<li class="sidebar-item">
411+
<div class="sidebar-item-container">
412+
<a href="../../2022/R/day18.html" class="sidebar-item-text sidebar-link">
413+
<span class="menu-text">Day 18</span></a>
414+
</div>
409415
</li>
410416
</ul>
411417
</li>

docs/2022/R/day04.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ <h1 class="quarto-secondary-nav-title">Day 4</h1>
406406
<a href="../../2022/R/day17.html" class="sidebar-item-text sidebar-link">
407407
<span class="menu-text">Day 17</span></a>
408408
</div>
409+
</li>
410+
<li class="sidebar-item">
411+
<div class="sidebar-item-container">
412+
<a href="../../2022/R/day18.html" class="sidebar-item-text sidebar-link">
413+
<span class="menu-text">Day 18</span></a>
414+
</div>
409415
</li>
410416
</ul>
411417
</li>

docs/2022/R/day05.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ <h1 class="quarto-secondary-nav-title">Day 5</h1>
406406
<a href="../../2022/R/day17.html" class="sidebar-item-text sidebar-link">
407407
<span class="menu-text">Day 17</span></a>
408408
</div>
409+
</li>
410+
<li class="sidebar-item">
411+
<div class="sidebar-item-container">
412+
<a href="../../2022/R/day18.html" class="sidebar-item-text sidebar-link">
413+
<span class="menu-text">Day 18</span></a>
414+
</div>
409415
</li>
410416
</ul>
411417
</li>

docs/2022/R/day06.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ <h1 class="quarto-secondary-nav-title">Day 6</h1>
406406
<a href="../../2022/R/day17.html" class="sidebar-item-text sidebar-link">
407407
<span class="menu-text">Day 17</span></a>
408408
</div>
409+
</li>
410+
<li class="sidebar-item">
411+
<div class="sidebar-item-container">
412+
<a href="../../2022/R/day18.html" class="sidebar-item-text sidebar-link">
413+
<span class="menu-text">Day 18</span></a>
414+
</div>
409415
</li>
410416
</ul>
411417
</li>

docs/2022/R/day07.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ <h1 class="quarto-secondary-nav-title">Day 7</h1>
406406
<a href="../../2022/R/day17.html" class="sidebar-item-text sidebar-link">
407407
<span class="menu-text">Day 17</span></a>
408408
</div>
409+
</li>
410+
<li class="sidebar-item">
411+
<div class="sidebar-item-container">
412+
<a href="../../2022/R/day18.html" class="sidebar-item-text sidebar-link">
413+
<span class="menu-text">Day 18</span></a>
414+
</div>
409415
</li>
410416
</ul>
411417
</li>

docs/2022/R/day08.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ <h1 class="quarto-secondary-nav-title">Day 8</h1>
406406
<a href="../../2022/R/day17.html" class="sidebar-item-text sidebar-link">
407407
<span class="menu-text">Day 17</span></a>
408408
</div>
409+
</li>
410+
<li class="sidebar-item">
411+
<div class="sidebar-item-container">
412+
<a href="../../2022/R/day18.html" class="sidebar-item-text sidebar-link">
413+
<span class="menu-text">Day 18</span></a>
414+
</div>
409415
</li>
410416
</ul>
411417
</li>

0 commit comments

Comments
 (0)