-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday06.R
53 lines (39 loc) · 1.22 KB
/
day06.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
library(tidyverse)
library(janitor)
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
input.raw = read_delim(file="input06.txt", skip_empty_rows = F, delim = "\n", col_names ="x")
#allgemein (nach teil 2)
position = function(dff, anzahl){
result = 1:(nchar(dff)-anzahl+1) |>
as_tibble() |>
select(nummer = 1) |>
mutate(y= str_sub(string = dff, start=row_number(), end = row_number()+anzahl-1)) |>
filter(nchar(y)==anzahl) |>
mutate(nummer = row_number()+anzahl-1) |>
mutate(a = map(.x=y, ~str_split(.x,""))) |>
unnest(a) |>
mutate(d= map(.x=a, ~duplicated(.x))) |>
mutate(dd = map(.x= d, ~!any(.x))) |>
filter(dd==TRUE) |>
head(1) |>
select(nummer) |>
deframe()
return(result)
}
position(input.raw |> deframe(), 4)
position(input.raw |> deframe(), 14)
#teil 1 ursprünglich
input.raw |>
mutate(a= map(.x=x, ~str_split(.x,""))) |>
unnest(a) |>
unnest_longer(a) |>
select(-x) |>
mutate(b = lead(a,1)) |>
mutate(c = lead(a,2)) |>
mutate(d = lead(a,3)) |>
mutate(l = !(a==b|a==c|a==d|b==c|b==d|c==d)) |>
mutate(z = ifelse(l,row_number()+3,0)) |>
filter(z!=0) |>
head(1) |>
select(z) |>
deframe()