Advertisement · 728 × 90

Posts by Alistair Beith

The ifelse call isn’t modifying your tibble but you’re returning the tibble. I’d do:
if (<condition>) {
<modify tibble>
return(<your tibble>)
} else {
<modify tibble differently>
return(<your tibble>)
}

1 year ago 1 0 1 0

Well-tempered would have been great for integration with (Ave) MariaDB.

1 year ago 1 0 1 0
Post image

Day 23 #AdventOfCode #rstats

Sorry. Sometimes you just have to get the star.

1 year ago 2 0 0 0

My brain didn’t manage that `!=` == xor but I spotted the powers of 2 early and didn’t want to fix the integer overflow issue.

1 year ago 1 0 0 0
Post image

Day 22 #AdventOfCode #rstats

I feel like I could have lived without knowing that raw vectors in R are little-endian.

1 year ago 3 0 2 0
Post image

Day 20 #AdventOfCode #rstats

Quite proud of this one.

1 year ago 4 0 1 0
Post image

Day 19 #AdventOfCode #rstats

Fun way to learn that overlapping regex is hard in R.

1 year ago 3 0 1 0

Skipping days 16-18 for now. I particularly disliked Day 16 and still don't know why my solution to part 2 didn't work.

1 year ago 0 0 1 0
Video

Day 15 #AdventOfCode #rstats

Too many lines of code for this one so I'm just sharing a video instead.

1 year ago 8 0 1 0
Advertisement

It’s the fewest lines of code I’ve written for one of these in days.

1 year ago 1 0 0 0
Post image

Day 14 #AdventOfCode #rstats

My favourite day so far!

1 year ago 4 0 1 0

Reduce and purrr::reduce have been handy. I also used Vectorize today and feel like that deserves more love.

1 year ago 1 0 0 0

I like that there are more lines parsing the input than solving the problem!

1 year ago 1 0 1 0
Post image

Day 13 #AdventOfCode #rstats

I resisted the urge to use complex numbers for the coordinates in part 1 only to find out that was the solution to part 2!

1 year ago 4 0 2 0
Post image

Day 12 #AdventOfCode #rstats

There was a beautiful base R solution that worked with the sample input, but I gave up and used a package.

1 year ago 4 0 1 0

But… “every problem has a solution that completes in at most 15 seconds on ten-year-old hardware”. But I guess 15 seconds running parallel with C++ can be a long time on 1 thread in R.

1 year ago 1 0 1 0

That feels like cheating! I think I got close to a fast solution with igraph.

1 year ago 0 0 1 0

I had to abandon my first attempt because an exponential linear model said it’d take a few years. So I’ll take this victory for now!

1 year ago 1 0 1 0
Post image

Day 11 #AdventOfCode #rstats

I shouldn't admit to this one. How long is a blink? Because the last blink in my loop was almost 14 seconds.

1 year ago 1 0 3 0
Advertisement
Post image

Day 10 #AdventOfCode #rstats

First use of curly-curly.

1 year ago 4 0 2 0
Post image

Day 9 (Part 2) #AdventOfCode #rstats

1 year ago 2 0 1 0
Post image

Day 9 (Part 1) #AdventOfCode #rstats

In spite of the evidence I'm still convinced this is a run-length encoding problem!

1 year ago 2 0 1 0
  ## setup
  input <- readLines('../data/aoc8.txt') |>
    sapply(strsplit, split = '') |>
    do.call(what = rbind)
  rownames(input) <- NULL

  antenna_symbols <- input[input != '.'] |> unique()

  ## part 1
  get_nodes <- function(antenna_symbol){
    antenna_positions <- which(input == antenna_symbol, arr.ind = TRUE) |>
      apply(1, \(x) complex(real = x['col'], imaginary = x['row']))
    antenna_pairs <- gtools::permutations(length(antenna_positions), 2, antenna_positions)
    nodes <- apply(antenna_pairs, 1, diff) + antenna_pairs[,2]
    return(nodes)
  }

  check_node <- function(node){
    if(Im(node) < 1 | Im(node) > nrow(input)) return(FALSE)
    if(Re(node) < 1 | Re(node) > ncol(input)) return(FALSE)
    return(TRUE)
  }

  nodes <- sapply(antenna_symbols, get_nodes) |> unlist()
  nodes <- nodes[sapply(nodes, check_node)]

  ## part 1 answer
  nodes |> unique() |> length()

  ## part 2

  get_harmonics <- function(origin, wavelength){
    nodes <- c(origin)
    for(i in seq_len(prod(dim(input)))){
      node <- origin + (wavelength * i)
      if(!check_node(node)) return(nodes)
      nodes <- c(nodes, node)
    }
    stop("This can never happen")
  }

  get_node_harmonics <- function(antenna_symbol) {
    antenna_positions <- which(input == antenna_symbol, arr.ind = TRUE) |>
      apply(1, \(x) complex(real = x['col'], imaginary = x['row']))
    antenna_pairs <- gtools::permutations(length(antenna_positions), 2, antenna_positions)
    wavelengths <- apply(antenna_pairs, 1, diff)
    origins <- antenna_pairs[,2]
    nodes <- purrr::map2(origins, wavelengths, get_harmonics) |>
      unlist()
    return(nodes)
  }

  harmonics <- sapply(antenna_symbols, get_node_harmonics) |>
    unlist() |>
    unique()

  ## part 2 answer
  harmonics |> length()

## setup input <- readLines('../data/aoc8.txt') |> sapply(strsplit, split = '') |> do.call(what = rbind) rownames(input) <- NULL antenna_symbols <- input[input != '.'] |> unique() ## part 1 get_nodes <- function(antenna_symbol){ antenna_positions <- which(input == antenna_symbol, arr.ind = TRUE) |> apply(1, \(x) complex(real = x['col'], imaginary = x['row'])) antenna_pairs <- gtools::permutations(length(antenna_positions), 2, antenna_positions) nodes <- apply(antenna_pairs, 1, diff) + antenna_pairs[,2] return(nodes) } check_node <- function(node){ if(Im(node) < 1 | Im(node) > nrow(input)) return(FALSE) if(Re(node) < 1 | Re(node) > ncol(input)) return(FALSE) return(TRUE) } nodes <- sapply(antenna_symbols, get_nodes) |> unlist() nodes <- nodes[sapply(nodes, check_node)] ## part 1 answer nodes |> unique() |> length() ## part 2 get_harmonics <- function(origin, wavelength){ nodes <- c(origin) for(i in seq_len(prod(dim(input)))){ node <- origin + (wavelength * i) if(!check_node(node)) return(nodes) nodes <- c(nodes, node) } stop("This can never happen") } get_node_harmonics <- function(antenna_symbol) { antenna_positions <- which(input == antenna_symbol, arr.ind = TRUE) |> apply(1, \(x) complex(real = x['col'], imaginary = x['row'])) antenna_pairs <- gtools::permutations(length(antenna_positions), 2, antenna_positions) wavelengths <- apply(antenna_pairs, 1, diff) origins <- antenna_pairs[,2] nodes <- purrr::map2(origins, wavelengths, get_harmonics) |> unlist() return(nodes) } harmonics <- sapply(antenna_symbols, get_node_harmonics) |> unlist() |> unique() ## part 2 answer harmonics |> length()

Day 8 #AdventOfCode #rstats

Finally caught up!

1 year ago 3 0 1 0
Post image

Day 7 #AdventOfCode #rstats

Yes, the `%||%` operator is a relic of a failed eval(parse(text = x)) approach.

1 year ago 3 0 1 0
Post image

Day 6 #AdventOfCode #rstats

Resorted to brute force and patience for part 2 in the end. Probably only a couple of conditionals away from being a not terrible solution!

1 year ago 3 0 1 0
Post image

Day 5 #AdventOfCode #rstats

Messy solution in the tidyverse!

1 year ago 4 0 1 0
Post image

Day 4

1 year ago 1 0 1 0
Post image

Day 3

1 year ago 1 0 1 0
Post image

Day 2

1 year ago 1 0 1 0
Advertisement
Post image

Here's a thread of my #aoc solutions in #rstats.

Day 1

1 year ago 5 2 1 0