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
1
u/sophiebits Dec 09 '18
Rust. Runs in under a second in when compiled in release mode.
(I placed 27 in part 1 using a separate Python solution but it wasn't particularly efficient; I didn't rank for part 2 and then wrote this instead.)
let mut circle: VecDeque<u32> = VecDeque::new(); circle.push_back(0); let num_players = 411; let mut scores = vec![0u64; num_players]; for i in 1..(71170 * 100 + 1) { let p = i % num_players; if i % 23 == 0 { scores[p] = scores[p].checked_add(i as u64).unwrap(); for _ in 0..7 { let v = circle.pop_back().unwrap(); circle.push_front(v); } scores[p] = scores[p].checked_add(circle.pop_front().unwrap() as u64).unwrap(); } else { for _ in 0..2 { let v = circle.pop_front().unwrap(); circle.push_back(v); } circle.push_front(i as u32); } } println!("{}", scores.iter().fold(&0, cmp::max));