annotate day2.txt @ 39:0e17e4bd97a9 default tip

Rewrite as four-dimensional route finding The grid isn't just a 2D grid. The constraints make it 4D: * X * Y * Last direction * Number of steps in that direction By tracking all four dimensions, we can find the shortest route for _all_ combinations of the constraint. Previously, we were dropping routes that were currently longer but ended up shorter because they could take subsequent steps that other routes couldn't.
author IBBoard <dev@ibboard.co.uk>
date Sun, 22 Sep 2024 11:30:53 +0100
parents 1e16a25a9553
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 --- Day 2: Cube Conundrum ---
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2
19
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
3 There is a small bag and some cubes which are either red, green, or blue. Each time you play this game, a secret number of cubes of each color are hidden in the bag, and your goal is to figure out information about the number of cubes.
1
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
19
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
5 To get information, a handful of random cubes will be taken, shown to you, and then put back in the bag. This will be repeated
1
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 You play several games and record the information from each game (your puzzle input). Each game is listed with its ID number (like the 11 in Game 11: ...) followed by a semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 red, 5 green, 4 blue).
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 For example, the record of a few games might look like this:
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 In game 1, three sets of cubes are revealed from the bag (and then put back again). The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is only 2 green cubes.
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18
19
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
19 Which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes?
1
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20
19
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
21 In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. However, game 3 would have been impossible because at one point you saw 20 red cubes at once; similarly, game 4 would also have been impossible because you saw 15 blue cubes at once. If you add up the IDs of the games that would have been possible, you get 8.
1
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
49dd1ae93696 Implement day 2 with map and reduce
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those games?
19
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
24
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
25 --- Part Two ---
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
26
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
27 In each game you played, what is the fewest number of cubes of each color that could have been in the bag to make the game possible?
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
28
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
29 Again consider the example games from earlier:
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
30
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
31 Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
32 Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
33 Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
34 Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
35 Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
36
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
37 In game 1, the game could have been played with as few as 4 red, 2 green, and 6 blue cubes. If any color had even one fewer cube, the game would have been impossible.
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
38 Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
39 Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
40 Game 4 required at least 14 red, 3 green, and 15 blue cubes.
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
41 Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
42
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
43 The power of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. The power of the minimum set of cubes in game 1 is 48. In games 2-5 it was 12, 1560, 630, and 36, respectively. Adding up these five powers produces the sum 2286.
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
44
1e16a25a9553 Strip down the text to just the puzzle, not the fluff
IBBoard <dev@ibboard.co.uk>
parents: 1
diff changeset
45 For each game, find the minimum set of cubes that must have been present. What is the sum of the power of these sets?