-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.R
66 lines (60 loc) · 2.1 KB
/
21.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
library(tidyverse)
player_hit_points <- 100
read_delim("input21", delim = ": ", col_names = c("property", "value")) %>%
pivot_wider(names_from = "property", values_from = "value") -> boss
"Dagger 8 4 0
Shortsword 10 5 0
Warhammer 25 6 0
Longsword 40 7 0
Greataxe 74 8 0" %>%
str_split_1("\\n") %>%
tibble %>%
separate(col = ".", into = c("Name", "Cost", "Damage", "Armor")) %>%
mutate_at(-1, as.integer) -> weapons
"Leather 13 0 1
Chainmail 31 0 2
Splintmail 53 0 3
Bandedmail 75 0 4
Platemail 102 0 5" %>%
str_split_1("\\n") %>%
tibble %>%
separate(col = ".", into = c("Name", "Cost", "Damage", "Armor")) %>%
mutate_at(-1, as.integer) %>%
rbind(0) -> armors
"Damage +1 25 1 0
Damage +2 50 2 0
Damage +3 100 3 0
Defense +1 20 0 1
Defense +2 40 0 2
Defense +3 80 0 3" %>%
str_split_1("\\n") %>%
str_replace(" \\+", "\\+") %>%
tibble %>%
separate(col = ".", into = c("Name", "Cost", "Damage", "Armor"), sep = "\\s+") %>%
mutate_at(-1, as.integer) %>%
rbind(0) %>%
rbind(0) -> rings
min_cost <- sum(c(weapons$Cost, armors$Cost, rings$Cost))
max_cost <- 0
for (weap in 1:nrow(weapons))
for (arm in 1:nrow(armors))
for (ring1 in 1:(nrow(rings) - 1))
for (ring2 in (ring1 + 1):nrow(rings)) {
equip <- weapons[weap,] %>%
rbind(armors[arm,]) %>%
rbind(rings[ring1,]) %>%
rbind(rings[ring2,])
cost <- sum(equip$Cost)
damage <- sum(equip$Damage)
armor <- sum(equip$Armor)
player_to_boss <- max(1, damage - boss$Armor)
boss_to_player <- max(1, boss$Damage - armor)
if (ceiling(player_hit_points / boss_to_player) >=
ceiling(boss$`Hit Points` / player_to_boss))
min_cost <- min(min_cost, cost)
if (ceiling(player_hit_points / boss_to_player) <
ceiling(boss$`Hit Points` / player_to_boss))
max_cost <- max(max_cost, cost)
}
print(min_cost)
print(max_cost)