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>)
}
Posts by Alistair Beith
Well-tempered would have been great for integration with (Ave) MariaDB.
Day 23 #AdventOfCode #rstats
Sorry. Sometimes you just have to get the star.
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.
Day 22 #AdventOfCode #rstats
I feel like I could have lived without knowing that raw vectors in R are little-endian.
Day 20 #AdventOfCode #rstats
Quite proud of this one.
Day 19 #AdventOfCode #rstats
Fun way to learn that overlapping regex is hard in R.
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.
Day 15 #AdventOfCode #rstats
Too many lines of code for this one so I'm just sharing a video instead.
It’s the fewest lines of code I’ve written for one of these in days.
Day 14 #AdventOfCode #rstats
My favourite day so far!
Reduce and purrr::reduce have been handy. I also used Vectorize today and feel like that deserves more love.
I like that there are more lines parsing the input than solving the problem!
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!
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.
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.
That feels like cheating! I think I got close to a fast solution with igraph.
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!
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.
Day 10 #AdventOfCode #rstats
First use of curly-curly.
Day 9 (Part 2) #AdventOfCode #rstats
Day 9 (Part 1) #AdventOfCode #rstats
In spite of the evidence I'm still convinced this is a run-length encoding problem!
## 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!
Day 7 #AdventOfCode #rstats
Yes, the `%||%` operator is a relic of a failed eval(parse(text = x)) approach.
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!
Day 5 #AdventOfCode #rstats
Messy solution in the tidyverse!
Day 4
Day 3
Day 2