Skip to content

Commit fa115f6

Browse files
committed
Day 24 Part 1
1 parent 5bc8f69 commit fa115f6

File tree

11 files changed

+2430
-61
lines changed

11 files changed

+2430
-61
lines changed

2022/R/day16.qmd

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: "Day 16"
3+
date: 2022-12-16
4+
author:
5+
name: https://adventofcode.com/2022/day/16
6+
url: https://adventofcode.com/2022/day/16
7+
---
8+
9+
## Setup
10+
11+
```{r setup}
12+
# Libraries
13+
library(tidyverse)
14+
library(igraph)
15+
16+
# Read input from file
17+
input <- read_lines("../input/day16.txt", skip_empty_rows = TRUE) |>
18+
unglue::unglue_data(
19+
c(
20+
"Valve {source} has flow rate={rate}; tunnels lead to valves {target}",
21+
"Valve {source} has flow rate={rate}; tunnel leads to valve {target}"
22+
),
23+
convert = TRUE
24+
) |>
25+
mutate(target = map(target, \(x) str_split_1(x, ", ")))
26+
```
27+
28+
## Part 1
29+
30+
```{r}
31+
edges <- input |>
32+
unnest(target) |>
33+
pmap(function(source, target, ...) c(source, target)) |>
34+
unique() |>
35+
unlist() |>
36+
make_graph(directed = TRUE)
37+
38+
plot(edges)
39+
```
40+

2022/input/day16.txt

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,10 @@
1-
Valve RT has flow rate=0; tunnels lead to valves EN, LZ
2-
Valve VB has flow rate=0; tunnels lead to valves SZ, BF
3-
Valve AD has flow rate=0; tunnels lead to valves EB, JF
4-
Valve RE has flow rate=4; tunnels lead to valves QB, IF, XT, WF, KW
5-
Valve RL has flow rate=0; tunnels lead to valves DQ, LZ
6-
Valve OK has flow rate=0; tunnels lead to valves QH, BF
7-
Valve RV has flow rate=0; tunnels lead to valves IU, JF
8-
Valve TE has flow rate=0; tunnels lead to valves HE, XF
9-
Valve WW has flow rate=0; tunnels lead to valves QH, YZ
10-
Valve HB has flow rate=15; tunnel leads to valve OM
11-
Valve IY has flow rate=14; tunnels lead to valves UH, KW, BN, LW, UY
12-
Valve QF has flow rate=0; tunnels lead to valves JF, PL
13-
Valve YZ has flow rate=0; tunnels lead to valves JG, WW
14-
Valve QB has flow rate=0; tunnels lead to valves SP, RE
15-
Valve SO has flow rate=0; tunnels lead to valves QH, SZ
16-
Valve EB has flow rate=7; tunnels lead to valves IF, NH, AD, VI, DQ
17-
Valve VL has flow rate=0; tunnels lead to valves JF, YV
18-
Valve BF has flow rate=18; tunnels lead to valves OK, VB, OH, SX
19-
Valve UC has flow rate=0; tunnels lead to valves SC, YV
20-
Valve OQ has flow rate=0; tunnels lead to valves XT, AA
21-
Valve YV has flow rate=6; tunnels lead to valves YX, TT, VL, UC, NH
22-
Valve KJ has flow rate=0; tunnels lead to valves OH, JG
23-
Valve QH has flow rate=20; tunnels lead to valves SO, OK, WW
24-
Valve KW has flow rate=0; tunnels lead to valves RE, IY
25-
Valve PL has flow rate=0; tunnels lead to valves JG, QF
26-
Valve DQ has flow rate=0; tunnels lead to valves EB, RL
27-
Valve AA has flow rate=0; tunnels lead to valves YI, EN, UK, OQ, VI
28-
Valve XT has flow rate=0; tunnels lead to valves OQ, RE
29-
Valve SZ has flow rate=24; tunnels lead to valves VB, SO
30-
Valve IU has flow rate=25; tunnels lead to valves RV, HE, HQ
31-
Valve OM has flow rate=0; tunnels lead to valves NY, HB
32-
Valve YX has flow rate=0; tunnels lead to valves YV, SI
33-
Valve SX has flow rate=0; tunnels lead to valves ZB, BF
34-
Valve KD has flow rate=0; tunnels lead to valves XF, LW
35-
Valve SP has flow rate=0; tunnels lead to valves XF, QB
36-
Valve UY has flow rate=0; tunnels lead to valves UK, IY
37-
Valve XF has flow rate=22; tunnels lead to valves SP, TE, KD, NY
38-
Valve SC has flow rate=0; tunnels lead to valves LZ, UC
39-
Valve UK has flow rate=0; tunnels lead to valves UY, AA
40-
Valve LW has flow rate=0; tunnels lead to valves KD, IY
41-
Valve FL has flow rate=0; tunnels lead to valves BN, LZ
42-
Valve VI has flow rate=0; tunnels lead to valves AA, EB
43-
Valve HW has flow rate=0; tunnels lead to valves JF, CY
44-
Valve YI has flow rate=0; tunnels lead to valves AA, TT
45-
Valve HE has flow rate=0; tunnels lead to valves IU, TE
46-
Valve JG has flow rate=10; tunnels lead to valves PL, YZ, SI, KJ
47-
Valve BN has flow rate=0; tunnels lead to valves IY, FL
48-
Valve IF has flow rate=0; tunnels lead to valves EB, RE
49-
Valve JF has flow rate=19; tunnels lead to valves HW, QF, VL, RV, AD
50-
Valve SI has flow rate=0; tunnels lead to valves JG, YX
51-
Valve WF has flow rate=0; tunnels lead to valves LZ, RE
52-
Valve HQ has flow rate=0; tunnels lead to valves IU, UH
53-
Valve LZ has flow rate=5; tunnels lead to valves SC, FL, WF, RL, RT
54-
Valve UH has flow rate=0; tunnels lead to valves IY, HQ
55-
Valve CY has flow rate=21; tunnel leads to valve HW
56-
Valve NH has flow rate=0; tunnels lead to valves EB, YV
57-
Valve TT has flow rate=0; tunnels lead to valves YV, YI
58-
Valve OH has flow rate=0; tunnels lead to valves KJ, BF
59-
Valve EN has flow rate=0; tunnels lead to valves RT, AA
60-
Valve NY has flow rate=0; tunnels lead to valves OM, XF
61-
Valve ZB has flow rate=8; tunnel leads to valve SX
1+
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
2+
Valve BB has flow rate=13; tunnels lead to valves CC, AA
3+
Valve CC has flow rate=2; tunnels lead to valves DD, BB
4+
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
5+
Valve EE has flow rate=3; tunnels lead to valves FF, DD
6+
Valve FF has flow rate=0; tunnels lead to valves EE, GG
7+
Valve GG has flow rate=0; tunnels lead to valves FF, HH
8+
Valve HH has flow rate=22; tunnel leads to valve GG
9+
Valve II has flow rate=0; tunnels lead to valves AA, JJ
10+
Valve JJ has flow rate=21; tunnel leads to valve II

2024/R/day24.qmd

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: "Day 24"
3+
date: 2024-12-24
4+
author:
5+
name: https://adventofcode.com/2024/day/24
6+
url: https://adventofcode.com/2024/day/24
7+
---
8+
9+
## Setup
10+
11+
```{r setup}
12+
# Libraries
13+
library(tidyverse)
14+
15+
# Read input from file
16+
input <- read_lines("../input/day24.txt", skip_empty_rows = TRUE)
17+
```
18+
19+
## Part 1
20+
21+
Convert input to lists of gates and wires:
22+
23+
```{r}
24+
25+
init <- input |>
26+
unglue::unglue_data("{wire}: {value}", convert = TRUE) |>
27+
filter(!is.na(wire))
28+
29+
gates <- input |>
30+
unglue::unglue_data("{src1} {gate} {src2} -> {target}") |>
31+
filter(!is.na(gate)) |>
32+
mutate(gate = case_match(gate, "AND" ~ "&", "OR" ~ "|", "XOR" ~ "xor"))
33+
34+
wires <- init |>
35+
complete(wire = unique(c(gates$src1, gates$src2, gates$target))) |>
36+
deframe()
37+
38+
```
39+
40+
Loop through the lists of gates and wires, and whenever a new gate can be activated, get its resulting value. Repeat until all wires have a final output.
41+
42+
```{r}
43+
44+
df <- wires |>
45+
enframe(name = "wire") |>
46+
left_join(gates, join_by(wire == target))
47+
48+
repeat {
49+
50+
cur_values <- df |>
51+
select(wire, value) |>
52+
deframe()
53+
54+
df <- df |>
55+
mutate(
56+
val1 = cur_values[src1],
57+
val2 = cur_values[src2],
58+
value = coalesce(
59+
value,
60+
pmap_int(list(gate, val1, val2), \(gate, val1, val2) {
61+
if (!is.na(gate) & !is.na(val1) & !is.na(val2))
62+
get(gate)(val1, val2)
63+
else
64+
NA_integer_
65+
})
66+
)
67+
)
68+
69+
if (all(!is.na(df$value))) break
70+
}
71+
72+
```
73+
74+
Convert the Z-coded wires to a binary number:
75+
76+
```{r}
77+
78+
df |>
79+
filter(str_starts(wire, "z")) |>
80+
arrange(wire) |>
81+
pull(value) |>
82+
imap_dbl(\(x, i) x * 2^(i - 1)) |>
83+
sum() |>
84+
format(scientific = FALSE)
85+
86+
```
87+

0 commit comments

Comments
 (0)