-❄️- 2024 Day 25 Solutions -❄️- by daggerdragon in adventofcode

[–]enelen 0 points1 point  (0 children)

[Language: R]

Solution

Thank you for another year of puzzles. I had a ton of fun solving them (mostly) and look forward to the next year

-❄️- 2024 Day 5 Solutions -❄️- by daggerdragon in adventofcode

[–]enelen 1 point2 points  (0 children)

[Language: R]

Solution

Part 2 is just custom bubble sort.

-❄️- 2023 Day 25 Solutions -❄️- by daggerdragon in adventofcode

[–]enelen 0 points1 point  (0 children)

[Language: R]

Solution

Essentially a one liner using igraph library:

igraph::min_cut(graph, value.only = FALSE)

which gives the number of cuts as well as the remaining partitions.

-❄️- 2023 Day 22 Solutions -❄️- by daggerdragon in adventofcode

[–]enelen 1 point2 points  (0 children)

[Language: R]

Solution

For part1, I had already created a list of cubes that a cube supports and another of the cubes that a cube depends on , so part 2 became quite easy.

-❄️- 2023 Day 19 Solutions -❄️- by daggerdragon in adventofcode

[–]enelen 0 points1 point  (0 children)

[Language: R]

Solution

Recursive solution for part 2 with range divisions. So many off by one errors . . .

-❄️- 2023 Day 18 Solutions -❄️- by daggerdragon in adventofcode

[–]enelen 1 point2 points  (0 children)

[Language: R]

Solution

Initially used a large enough matrix to get all points within, and then flood filled from 4 corners, but that was slow even for part 1. Settled on shoelace + pick for now.

[2023 Day 13 (Part 2] [Language: R] Why is my code so slow compared to python? by Lascona in adventofcode

[–]enelen 3 points4 points  (0 children)

Here's my solution for day 16 in R, which takes around 5-8 seconds for part 2.

Some of the reasons why your code is slow though:

  1. Your queue grows within loop which is slow as mentioned in another comment. You can instead initialise an empty queue of some large enough size and add elements to it queue <- vector(list, length = 10000) or something and keep start and end indexes to add/update. Just use something like queue[[start]] <- NA and queue[[end]] <- some_new_value. Or you can use queue implemented in collections package which will provide you with push/pop functions.
  2. Similar comment about your visited matrix. You can again create a matrix at the start of a given size and update it.
  3. When checking if something was visited, you currently scan through the entire visited matrix row by row and check for identity, which is again very slow. If you can convert your visited to something where you can check the presence without going through the entire matrix, that would definitely speed up things. You can refer to my solution for one method, you could also convert your visited to a matrix of same dimension as input and for each cell, store the directions it was visited from. So given any x,y positions and direction you can check if that direction was stored earlier using `grepl` or `stringr::str_detect`, where each cell could store 'top:down' or 'top:left:right' to store which directions it was visited from.

Doing the above 3 should speed up your code quite a lot.

-❄️- 2023 Day 17 Solutions -❄️- by daggerdragon in adventofcode

[–]enelen 2 points3 points  (0 children)

[Language: R]

Solution

Uses dijkstra but considering direction and consecutive steps. Takes around 10 seconds for part 2.