Skip to content

Instantly share code, notes, and snippets.

@xvrdm
Last active December 2, 2021 13:58
Show Gist options
  • Save xvrdm/6bb602e103667c89822aaadaafd587c9 to your computer and use it in GitHub Desktop.
Save xvrdm/6bb602e103667c89822aaadaafd587c9 to your computer and use it in GitHub Desktop.

Revisions

  1. xvrdm renamed this gist Dec 2, 2021. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. xvrdm revised this gist Dec 2, 2021. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions main.R
    Original 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() %>%
  3. xvrdm revised this gist Dec 2, 2021. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions main.R
    Original 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()

    length()
  4. xvrdm created this gist Dec 2, 2021.
    50 changes: 50 additions & 0 deletions main.R
    Original 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()