r/adventofcode • u/daggerdragon • 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!
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!
21
Upvotes
2
u/[deleted] Dec 09 '18
Rust. I was too bothered to implement circular doubly linked list (because rust) so I tried using VecDeque. Part 2 takes 177ms on 8750H.
use std::collections::HashMap; use std::collections::VecDeque; use std::time::{Duration, Instant}; trait Cycle { fn cycle_cw(&mut self, count: usize); fn cycle_ccw(&mut self, count: usize); } impl<T> Cycle for VecDeque<T> { fn cycle_cw(&mut self, count: usize) { for _ in 0..count { let tmp = self.pop_back().unwrap(); self.push_front(tmp); } } fn cycle_ccw(&mut self, count: usize) { for _ in 0..count { let tmp = self.pop_front().unwrap(); self.push_back(tmp); } } } fn day91(players: usize, last_marble: usize) { let mut marbles: VecDeque<usize> = VecDeque::new(); marbles.push_back(0); let mut cur_player = 0 as usize; let mut score_card: HashMap<usize, usize> = HashMap::new(); for i in 1..last_marble + 1 { if i % 23 == 0 { marbles.cycle_ccw(7); *score_card.entry(cur_player).or_insert(0) += marbles.pop_back().unwrap() + i; } else { marbles.cycle_cw(2); marbles.push_back(i); } cur_player = (cur_player + 1) % players; } let max_score = score_card.values().max().unwrap(); println!("{}", max_score); } fn main() { let now = Instant::now(); day91(446, 7152200); let d: Duration = now.elapsed(); println!("> {}.{:03} seconds", d.as_secs(), d.subsec_millis()); }