Advent of Code F# – Day 19

ps. look out for all my other solutions for Advent of Code challenges here.

 

Day 19

See details of the challenge here.

I initially approached today’s challenge as a dynamic programming exercise, but it quickly transpired that there’s a much better way to do it once I realised that part 1 is in fact the Josephus Problem and there’s a simple solution to it.

To understand the above, watch the YouTube video in the links section below.

 

Part 2

Realizing the folly of their present-exchange rules, the Elves agree to instead
steal presents from the Elf directly across the circle. If two Elves are across
the circle, the one on the left (from the perspective of the stealer) is stolen
from. The other rules remain unchanged: Elves with no presents are removed from
the circle entirely, and the other elves move in slightly to keep the circle
evenly spaced.

For example, with five Elves (again numbered 1 to 5):

  • The Elves sit in a circle; Elf 1 goes first:
  1
5   2
 4 3
  • Elves 3 and 4 are across the circle; Elf 3’s present is stolen, being the one to
    the left. Elf 3 leaves the circle, and the rest of the Elves move in:
  1           1
5   2  -->  5   2
 4 -          4
  • Elf 2 steals from the Elf directly across the circle, Elf 5:
  1         1 
-   2  -->     2
  4         4 
  • Next is Elf 4 who, choosing between Elves 1 and 2, steals from Elf 1:
 -          2  
    2  -->
 4          4
  • Finally, Elf 2 steals from Elf 4:
 2
    -->  2  
 -

So, with five Elves, the Elf that sits starting in position 2 gets all the
presents.

With the number of Elves given in your puzzle input, which Elf now gets all the
presents?

I’m not aware of this variation to the Josephus Problem, but I’d wager that there would be some pattern to the results similar to part 1, so I put together a dynamic programming solution to get some outputs. (ps. the solution is not good enough for the input as it’ll take too long to return)

With the help of this I can see a pattern emerging:

n : answer

1 : 1
2 : 2
3 : 3
4 : 1
5 : 2
6 : 3
7 : 5
8 : 7
9 : 9
10 : 1
11 : 2
12 : 3
13 : 4
14 : 5
15 : 6
16 : 7
17 : 8
18 : 9
19 : 11
20 : 13
21 : 15
22 : 17
23 : 19
24 : 21
25 : 23
26 : 25
27 : 27
28 : 1
29 : 2
30 : 3

  • where n is a power of 3 then the answer is itself
  • else n can be expressed as m + l where m is a power of 3
  • where l <= m (eg. n = 5 = 3 + 2 where m = 3 and l = 2) then the answer is just l
  • else the answer is m + (l – m) * 2 (eg. n = 7 = 3 + 4 where m = 3 and l = 4 and m + (l – m) * 2 = 5)

 

Links

Advent of Code F# – Day 18

ps. look out for all my other solutions for Advent of Code challenges here.

 

Day 18

See details of the challenge here.

To solve both part 1 and 2:

 

Links

Advent of Code F# – Day 17

ps. look out for all my other solutions for Advent of Code challenges here.

 

Day 17

See details of the challenge here.

First, let’s add a hash function (that returns the MD5 as a hexadecimal string as we have done so often this year).

Then, add a step function that’ll take a (x, y) position and the path (eg. “hijklDU”) leading up to it and returns the a tuple of new position and path if we’re not going to move off grid. We can then use this to construct the more useful functions to take a step in the up, down, left and right directions.

Now we can put everything together and write a function to find all the paths from (0, 0) to (3, 3) in the grid.

One minor detail you might have noticed on ln 5 above is that we’re short-circuiting paths that have reached the target, this is in accordance with the further details revealed in part 2 below.

For each of the positions (and the path that lead to it), we use the aforementioned up, down, left, and right functions to find the next positions we can reach from here (provided the right doors are open as per determined by the MD5 hash value).

And yes, it’s the Level Order Tree Traveral problem again! Funny we see different reincarnations of this particular problem over and over during this year’s AOC event.

One more detail to look out for in the code snippet above – that it’s possible for there to not be a path (as per the example in the description of the problem). Hence why we terminate the Seq.unfold when none of the current positions yields a possible next move.

With this, we can now answer part 1 really easily.

 

Part 2

You’re curious how robust this security solution really is, and so you decide to
find longer and longer paths which still provide access to the vault. You
remember that paths always end the first time they reach the bottom-right room
(that is, they can never pass through it, only end in it).

For example:

If your passcode were ihgpwlah, the longest path would take 370 steps.
With kglvqrro, the longest path would be 492 steps long.
With ulqzkmiv, the longest path would be 830 steps long.
What is the length of the longest path that reaches the vault?

 

Links

Advent of Code F# – Day 16

ps. look out for all my other solutions for Advent of Code challenges here.

 

Day 16

See details of the challenge here.

I also tried an implememtation using string instead of bool[] and there is no distinguishable difference in terms of performance. Now that we have the solve function we can answer both part 1 and part 2 really easily.

        let part1 = solve 272 input
        let part2 = solve 35651584 input

 

Links

Advent of Code F# – Day 15

ps. look out for all my other solutions for Advent of Code challenges here.

 

Day 15

See details of the challenge here.

Today’s challenge is pretty straight forward, and a brute force approach would have suffice – try every t from 0 to inifinity and return the first t that satisfies this equation for all the discs:

        (t + disc.Number – (disc.Positions – disc.Time0Position)) % disc.Positions = 0

That said, there’s a simple optimization we can apply by restricting ourselves to values of t that gets you through the first slot. Give my input for the challenge:

Disc #1 has 13 positions; at time=0, it is at position 11.
Disc #2 has 5 positions; at time=0, it is at position 0.
Disc #3 has 17 positions; at time=0, it is at position 11.
Disc #4 has 3 positions; at time=0, it is at position 0.
Disc #5 has 7 positions; at time=0, it is at position 2.
Disc #6 has 19 positions; at time=0, it is at position 17.

The first t that will get us through disc #1 is t=1 where disc #1 reaches position 0 on t=2 (which is 1s away from t). From there, we only need to check every 13s, ie t=14, t=27, t=40, …

 

Part 2

After getting the first capsule (it contained a star! what great fortune!), the
machine detects your success and begins to rearrange itself.

When it’s done, the discs are back in their original configuration as if it were
time=0 again, but a new disc with 11 positions and starting at position 0 has
appeared exactly one second below the previously-bottom disc.

With this new disc, and counting again starting from time=0 with the
configuration in your puzzle input, what is the first time you can press the
button to get another capsule?

 

Links