r/adventofcode • • Dec 09 '18

SOLUTION MEGATHREAD -🎄- 2018 Day 9 Solutions -🎄-

--- Day 9: Marble Mania ---


Post your solution as a comment or, for longer solutions, consider linking to your repo (e.g. GitHub/gists/Pastebin/blag or whatever).

Note: The Solution Megathreads are for solutions only. If you have questions, please post your own thread and make sure to flair it with Help.


Advent of Code: The Party Game!

Click here for rules

Please prefix your card submission with something like [Card] to make scanning the megathread easier. THANK YOU!

Card prompt: Day 9

Transcript:

Studies show that AoC programmers write better code after being exposed to ___.


This thread will be unlocked when there are a significant number of people on the leaderboard with gold stars for today's puzzle.

edit: Leaderboard capped, thread unlocked at 00:29:13!

22 Upvotes

283 comments sorted by

View all comments

Show parent comment

2

u/Grzi_ Dec 09 '18 edited Dec 09 '18

Glad to see some Rust

Take a look at this, I thing I did a pretty good one for this day (In Rust)

use std::collections::VecDeque;

fn calculate_high_score(_player_nb: usize, _marble_number: usize ) -> usize{
    let mut players_score = vec!(0; _player_nb);
    let mut ring = VecDeque::new();
    ring.push_front(0);

    for marble in 1.._marble_number {
        if marble % 23 == 0{
            // rotate of 7 behind + delete
            (0..7).for_each(|_| { 
                let tmp = ring.pop_back().expect("Rotate problem"); 
                ring.push_front(tmp); 
            } );
            players_score[marble % _player_nb] += 
                    marble + ring.pop_front().expect("No value in the ring");
        }else{
            // rotate of 2 ahead + insert
            (0..2).for_each(|_| { 
                let tmp = ring.pop_front().expect("Rotate problem");        
                ring.push_back(tmp);
            });
            ring.push_front(marble);
        }
    }
    *players_score.iter().max().expect("No value in the player scores")
}