day 4 completed
This commit is contained in:
@@ -1,3 +1,15 @@
|
||||
|
||||
Advent of Code
|
||||
|
||||
[About][Events][Shop][Settings][Log Out]
|
||||
|
||||
SpaceMoehre 7*
|
||||
{year=>2023}
|
||||
|
||||
[Calendar][AoC++][Sponsors][Leaderboard][Stats]
|
||||
|
||||
Our sponsors help make Advent of Code possible:
|
||||
Boot.dev - Ready to become a backend developer? If you like AoC, you might be like us. We think smartest way to learn to code is to ensure you're never bored. Try the most captivating, addictive way to learn to code on Boot.dev.
|
||||
--- Day 4: Scratchcards ---
|
||||
|
||||
The gondola takes you up. Strangely, though, the ground doesn't seem to be coming with you; you're not climbing a mountain. As the circle of Snow Island recedes below you, an entire new landmass suddenly appears above you! The gondola carries you to the surface of the new island and lurches into the station.
|
||||
@@ -35,7 +47,42 @@ So, in this example, the Elf's pile of scratchcards is worth 13 points.
|
||||
|
||||
Take a seat in the large pile of colorful cards. How many points are they worth in total?
|
||||
|
||||
To begin, get your puzzle input.
|
||||
Your puzzle answer was 21105.
|
||||
|
||||
The first half of this puzzle is complete! It provides one gold star: *
|
||||
--- Part Two ---
|
||||
|
||||
Just as you're about to report your findings to the Elf, one of you realizes that the rules have actually been printed on the back of every card this whole time.
|
||||
|
||||
There's no such thing as "points". Instead, scratchcards only cause you to win more scratchcards equal to the number of winning numbers you have.
|
||||
|
||||
Specifically, you win copies of the scratchcards below the winning card equal to the number of matches. So, if card 10 were to have 5 matching numbers, you would win one copy each of cards 11, 12, 13, 14, and 15.
|
||||
|
||||
Copies of scratchcards are scored like normal scratchcards and have the same card number as the card they copied. So, if you win a copy of card 10 and it has 5 matching numbers, it would then win a copy of the same cards that the original card 10 won: cards 11, 12, 13, 14, and 15. This process repeats until none of the copies cause you to win any more cards. (Cards will never make you copy a card past the end of the table.)
|
||||
|
||||
This time, the above example goes differently:
|
||||
|
||||
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
|
||||
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
|
||||
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1
|
||||
Card 4: 41 92 73 84 69 | 59 84 76 51 58 5 54 83
|
||||
Card 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36
|
||||
Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11
|
||||
|
||||
Card 1 has four matching numbers, so you win one copy each of the next four cards: cards 2, 3, 4, and 5.
|
||||
Your original card 2 has two matching numbers, so you win one copy each of cards 3 and 4.
|
||||
Your copy of card 2 also wins one copy each of cards 3 and 4.
|
||||
Your four instances of card 3 (one original and three copies) have two matching numbers, so you win four copies each of cards 4 and 5.
|
||||
Your eight instances of card 4 (one original and seven copies) have one matching number, so you win eight copies of card 5.
|
||||
Your fourteen instances of card 5 (one original and thirteen copies) have no matching numbers and win no more cards.
|
||||
Your one instance of card 6 (one original) has no matching numbers and wins no more cards.
|
||||
|
||||
Once all of the originals and copies have been processed, you end up with 1 instance of card 1, 2 instances of card 2, 4 instances of card 3, 8 instances of card 4, 14 instances of card 5, and 1 instance of card 6. In total, this example pile of scratchcards causes you to ultimately have 30 scratchcards!
|
||||
|
||||
Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, how many total scratchcards do you end up with?
|
||||
|
||||
Answer:
|
||||
You can also [Shareon Twitter Mastodon] this puzzle.
|
||||
|
||||
Although it hasn't changed, you can still get your puzzle input.
|
||||
|
||||
You can also [Shareon Twitter Mastodon] this puzzle.
|
||||
|
||||
@@ -186,4 +186,4 @@ Card 185: 6 48 27 60 13 5 36 25 78 95 | 53 22 98 75 60 24 27 55 69 34 41 82 1
|
||||
Card 186: 22 31 70 64 72 11 48 63 94 95 | 43 10 45 61 99 86 51 37 81 34 24 60 31 67 30 74 55 42 52 19 73 82 96 32 83
|
||||
Card 187: 24 19 70 82 52 64 8 86 23 54 | 92 14 11 7 13 67 26 86 72 62 50 94 40 52 48 33 49 96 68 29 30 1 73 88 41
|
||||
Card 188: 32 86 69 20 23 64 67 78 46 61 | 81 94 62 47 72 48 16 83 93 64 8 21 37 41 51 13 50 58 92 11 1 98 74 39 76
|
||||
Card 189: 70 29 33 24 9 10 48 42 60 65 | 26 92 27 76 90 1 37 78 35 66 20 43 45 99 7 87 32 83 67 52 58 15 94 63 89
|
||||
Card 189: 70 29 33 24 9 10 48 42 60 65 | 26 92 27 76 90 1 37 78 35 66 20 43 45 99 7 87 32 83 67 52 58 15 94 63 89
|
||||
@@ -1,21 +1,22 @@
|
||||
use std::fs;
|
||||
|
||||
use aochelpers;
|
||||
use regex::Regex;
|
||||
|
||||
#[macro_use(c)]
|
||||
extern crate cute;
|
||||
|
||||
fn main() {
|
||||
/* battleplan
|
||||
/* battleplan part 1
|
||||
1. create a list for all the cards
|
||||
1.1 parse input
|
||||
1.1.1split on '|' and parse via regex both parts or whole thing via regex
|
||||
1.2 build list
|
||||
1.2.1 each value of the list represents a card
|
||||
1.2.2 representation of a card = 2 lists, winning numbers, and drawn numbers
|
||||
1.3 map the list to convert card info into the amount of points
|
||||
1.4 sum the points*/
|
||||
|
||||
let content = fs::read_to_string("input").expect("Should have been able to read the file");
|
||||
2. map the list to convert card info into the amount of points
|
||||
3. sum the points
|
||||
*/
|
||||
let content = fs::read_to_string("input").expect("Could not read the file");
|
||||
let lines_array = content.split("\n").collect::<Vec<_>>();
|
||||
let number_regex = Regex::new(r"\d+").unwrap();
|
||||
let mut all_cards: Vec<(Vec<i32>, Vec<i32>)> = Vec::new();
|
||||
@@ -46,9 +47,31 @@ fn main() {
|
||||
.collect();
|
||||
let part1: i32 = part1_array
|
||||
.iter()
|
||||
.map(|x| i32::pow(2, x.clone().len() as u32 -1))
|
||||
.map(|x| i32::pow(2, x.clone().len() as u32 - 1))
|
||||
.collect::<Vec<i32>>()
|
||||
.iter()
|
||||
.sum();
|
||||
println!("Part 1: {part1}");
|
||||
|
||||
/*
|
||||
battleplan part 2
|
||||
1. get the intersection between similar numbers left and right
|
||||
2. add the amount of current card to subsequent cards
|
||||
3. sum the newly created list of amount of cards
|
||||
*/
|
||||
let mut part2_array: Vec<i32> = c! [1, for _x in 0..all_cards.len()];
|
||||
for (card_index, card) in all_cards.iter().enumerate() {
|
||||
let similar_amount = card
|
||||
.1
|
||||
.clone()
|
||||
.into_iter()
|
||||
.filter(|y| card.0.contains(y))
|
||||
.collect::<Vec<i32>>()
|
||||
.len() as i32;
|
||||
for i in 1..similar_amount+1 {
|
||||
part2_array[i32::min(card_index as i32 + i, 189) as usize] += part2_array[card_index];
|
||||
}
|
||||
}
|
||||
let part2 = part2_array.iter().sum::<i32>();
|
||||
println!("Part 2: {part2}");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user