D2P2 in ysh

This commit is contained in:
Abdulkadir Furkan Şanlı 2023-12-10 13:15:12 +01:00
parent 8679c5766f
commit 2558608d35
Signed by: afk
GPG Key ID: C8F00588EE6ED1FE
5 changed files with 103 additions and 39 deletions

71
2023/2/2/puzzle.md Normal file
View File

@ -0,0 +1,71 @@
\--- Day 2: Cube Conundrum ---
----------
You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile of leaves. It's quite cold, but you don't see much snow. An Elf runs over to greet you.
The Elf explains that you've arrived at *Snow Island* and apologizes for the lack of snow. He'll be happy to explain the situation, but it's a bit of a walk, so you have some time. They don't get many visitors up here; would you like to play a game in the meantime?
As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to figure out information about the number of cubes.
To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random cubes, show them to you, and then put them back in the bag. He'll do this a few times per game.
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`).
For example, the record of a few games might look like this:
```
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
```
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.
The Elf would first like to know which games would have been possible if the bag contained *only 12 red cubes, 13 green cubes, and 14 blue cubes*?
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 the Elf showed you 20 red cubes at once; similarly, game 4 would also have been *impossible* because the Elf showed you 15 blue cubes at once. If you add up the IDs of the games that would have been possible, you get `*8*`.
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?*
Your puzzle answer was `2727`.
\--- Part Two ---
----------
The Elf says they've stopped producing snow because they aren't getting any *water*! He isn't sure why the water stopped; however, he can show you how to get to the water source to check it out for yourself. It's just up ahead!
As you continue your walk, the Elf poses a second question: 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?
Again consider the example games from earlier:
```
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
```
* 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.
* Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.
* Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.
* Game 4 required at least 14 red, 3 green, and 15 blue cubes.
* Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.
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*`.
For each game, find the minimum set of cubes that must have been present. *What is the sum of the power of these sets?*
Your puzzle answer was `56580`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, you should [return to your Advent calendar](/2023) and try another puzzle.
If you still want to see it, you can [get your puzzle input](2/input).
You can also [Shareon [Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+%22Cube+Conundrum%22+%2D+Day+2+%2D+Advent+of+Code+2023&url=https%3A%2F%2Fadventofcode%2Ecom%2F2023%2Fday%2F2&related=ericwastl&hashtags=AdventOfCode) [Mastodon](javascript:void(0);)] this puzzle.

View File

@ -1,34 +0,0 @@
--- Part Two ---
The Elf says they've stopped producing snow because they aren't getting any
water! He isn't sure why the water stopped; however, he can show you how to get
to the water source to check it out for yourself. It's just up ahead!
As you continue your walk, the Elf poses a second question: 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?
Again consider the example games from earlier:
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
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.
Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.
Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.
Game 4 required at least 14 red, 3 green, and 15 blue cubes.
Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.
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.
For each game, find the minimum set of cubes that must have been present. What
is the sum of the power of these sets?

View File

@ -1,13 +1,13 @@
#!/usr/bin/env bash #!/usr/bin/env bash
#set -o errexit set -o errexit
set -o nounset set -o nounset
#set -o pipefail set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then if [[ "${TRACE-0}" == "1" ]]; then
set -o xtrace set -o xtrace
fi fi
cd "$(dirname "$0")" INPUT=$1
main () main ()
{ {
@ -19,14 +19,14 @@ main ()
IFS=';' read -r -a sets <<< "${stripped_game}" IFS=';' read -r -a sets <<< "${stripped_game}"
for (( i=0; i<${#sets[@]}; i++ )); do for (( i=0; i<${#sets[@]}; i++ )); do
for color in "${colors[@]}"; do for color in "${colors[@]}"; do
count=$(grep -o "[0-9]* ${color}" <<< "${sets[$i]}" | grep -o '[0-9]*') count=$(grep -o "[0-9]* ${color}" <<< "${sets[$i]}" | grep -o '[0-9]*' || true)
if [[ $count -gt ${colors_max[$color]} ]]; then if [[ $count -gt ${colors_max[$color]} ]]; then
colors_max[$color]=$count colors_max[$color]=$count
fi fi
done done
done done
(( powers+=colors_max[red]*colors_max[green]*colors_max[blue] )) (( powers+=colors_max[red]*colors_max[green]*colors_max[blue] ))
done < input.txt done < ${INPUT}
echo $powers echo $powers
} }

27
2023/2/2/solution.ysh.sh Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/env ysh
const INPUT = $1
proc main {
var powers = 0
while read -r game {
var colors = {'red': 0, 'green': 0, 'blue': 0}
var sets = game=>split(':')[1]
setvar sets = sets=>split(';')
for i in (0 .. len(sets)) {
for color in (colors->keys()) {
var pair = $(grep -o "[0-9]* ${color}" <<< $[sets[i]] || true)
if (len(pair) !== 0) {
var count = int(pair=>split()[0])
if (count > colors[color]) {
setvar colors[color] = count
}
}
}
}
setvar powers += colors['red'] * colors['green'] * colors['blue']
} < ${INPUT}
echo ${powers}
}
main