Last active
December 2, 2021 13:58
-
-
Save xvrdm/6bb602e103667c89822aaadaafd587c9 to your computer and use it in GitHub Desktop.
Revisions
-
xvrdm renamed this gist
Dec 2, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
xvrdm revised this gist
Dec 2, 2021 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,6 +29,8 @@ partition <- function(x, n) { r <- embed(x, n)[, n:1] split(r, row(r)) } # Or # partition <- \(x,n) Map(\(i) x[i:(i+n)], seq(length(x)-n)) readr::read_lines("./days/d01/data.txt") %>% as.integer() %>% -
xvrdm revised this gist
Dec 2, 2021 . 1 changed file with 1 addition and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,9 +13,6 @@ readr::read_lines("./days/d01/data.txt") %>% as.integer() %>% purrr::reduce(find_increase, .init = list(previous=NA_integer_, count=0)) # Challenge 2 readr::read_lines("./days/d01/data.txt") %>% @@ -46,5 +43,4 @@ readr::read_lines("./days/d01/data.txt") %>% partition(2) %>% purrr::map_depth(2, sum) %>% purrr::keep(~ .[[2]] > .[[1]]) %>% length() -
xvrdm created this gist
Dec 2, 2021 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ # ------------------------------------------------------------------------------------------ # Solution with Reduce # Challenge 1 find_increase <- function(accumulated, next_val) { is_increase = !is.na(accumulated$previous) && accumulated$previous < next_val list( previous = next_val, count = accumulated$count + ifelse(is_increase, 1, 0) ) } readr::read_lines("./days/d01/data.txt") %>% as.integer() %>% purrr::reduce(find_increase, .init = list(previous=NA_integer_, count=0)) # Challenge 2 readr::read_lines("./days/d01/data.txt") %>% as.integer() %>% slider::slide(~sum(.x), .after = 2) %>% purrr::reduce(find_increase, .init = list(previous=NA_integer_, count=0)) # ------------------------------------------------------------------------------------------ # Solution with Partition # Challenge 1 partition <- function(x, n) { r <- embed(x, n)[, n:1] split(r, row(r)) } readr::read_lines("./days/d01/data.txt") %>% as.integer() %>% partition(2) %>% purrr::keep(~ .[2] > .[1]) %>% length() # Challenge 2 readr::read_lines("./days/d01/data.txt") %>% as.integer() %>% partition(3) %>% partition(2) %>% purrr::map_depth(2, sum) %>% purrr::keep(~ .[[2]] > .[[1]]) %>% length()