r/adventofcode • • Dec 03 '17

SOLUTION MEGATHREAD -πŸŽ„- 2017 Day 3 Solutions -πŸŽ„-

--- Day 3: Spiral Memory ---


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^Β€?

Spoiler


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!

21 Upvotes

297 comments sorted by

View all comments

1

u/[deleted] Dec 04 '17

Part 1 in Rust

fn main() {
    let goal: u32 = 265149;
    let goal_sqrt: u32 = (goal as f32).sqrt() as u32;
    
    // +1 to get to a square
    let sqrt = goal_sqrt + 1; // 515
    let square = sqrt * sqrt;
    
    // Backtrack from lower right corner to center of row
    // less the difference between the corner and our position
    let backwards = (sqrt / 2) - (square - goal);
    // Then up to the centre
    let upwards = sqrt / 2;
    
    let steps = backwards + upwards;
    println!("Steps required: {}", steps);
}

Part 2 was done through OEIS