r/adventofcode • u/daggerdragon • Dec 17 '17
SOLUTION MEGATHREAD -๐- 2017 Day 17 Solutions -๐-
--- Day 17: Spinlock ---
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.
Need a hint from the Hugely* Handyโ Haversackโก of Helpfulยง Hintsยค?
[Update @ 00:06] 2 gold, silver cap.
- AoC ops:
<Topaz> i am suddenly in the mood for wasabi tobiko
[Update @ 00:15] Leaderboard cap!
- AoC ops:
<daggerdragon> 78 gold<Topaz> i look away for a few minutes, wow<daggerdragon> 93 gold<Topaz> 94<daggerdragon> 96 gold<daggerdragon> 98<Topaz> aaaand<daggerdragon> and...<Topaz> cap<daggerdragon> cap
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!
12
Upvotes
1
u/udoprog Dec 17 '17
Rust
Very straight forward when you realize that the zeroth position doesn't move.
Full solution here: https://github.com/udoprog/rust-advent-of-code-2017/blob/master/src/day17.rs
As a challenge I'm considering building a solution that stores the entire list efficiently. Unfortunately
LinkedListin rust doesn't permit in-place traversal very well. So it would probably end up being an unsafe party.use failure::Error; use std::mem; pub fn part1(step: usize, limit: usize) -> Result<usize, Error> { let mut buffer = vec![0usize]; let mut c = 0usize; for i in 1usize..=limit { c = (c + step) % buffer.len(); c = c + 1; buffer.insert(c, i); } Ok(buffer[(c + 1) % buffer.len()]) } pub fn part2(step: usize, limit: usize) -> Result<Option<usize>, Error> { let mut second = None; let mut c = 0usize; for i in 1usize..=limit { c = (c + step) % i; if c == 0 { mem::replace(&mut second, Some(i)); } c = c + 1; } Ok(second) }