-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.R
42 lines (36 loc) · 1016 Bytes
/
06.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
# https://adventofcode.com/2023/day/6
# init --------------------------------------------------------------------
library(tidyverse)
source("aoc.R")
x <- read_lines(get_aoc())
# 1 -----------------------------------------------------------------------
x_ <- read_lines(
"Time: 7 15 30
Distance: 9 40 200
")
x_ |>
str_extract_all("\\d+") |>
set_names("time", "dist") |>
map(as.numeric) |>
do.call(what = cbind) |>
as_tibble() |>
rowwise() |>
mutate(wins = sum(1:(time-1) * (time-1):1 > dist)) |>
pull(wins) |>
prod()
#> [1] 288
# 2 -----------------------------------------------------------------------
x_ |>
str_extract_all("\\d+") |>
map(str_c, collapse = "") |>
unlist() |>
as.numeric() -> race
race
#> [1] 71530 940200
# as.numeric(): with "integer" storage mode actual dataset generates
# integer overflow
a <- 1:(race[1]-1) |> as.numeric()
b <- rev(a)
sum((a * b) > race[2])
#> [1] 71503
# -------------------------------------------------------------------------