Created
December 6, 2017 07:40
-
-
Save haudan/d98056e26235c328b63725118a7b3634 to your computer and use it in GitHub Desktop.
Advent of Code 2017 #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[derive(Debug, PartialEq, Copy, Clone)] | |
enum Dir { | |
Up, | |
Down, | |
Left, | |
Right, | |
} | |
#[derive(Clone, Copy, PartialEq)] | |
struct Point { | |
x: i32, | |
y: i32, | |
} | |
fn calc_position(val: u32) -> Point { | |
let (mut point, mut dir) = (Point { x: 0, y: 0 }, Dir::Right); | |
let (mut breakpoint, mut travel) = (2, 1); | |
for i in 1..val { | |
if i == breakpoint { | |
// Increase travel when a corner is hit. | |
if dir == Dir::Up || dir == Dir::Down { | |
travel += 1; | |
} | |
// Choose new dir. | |
dir = match dir { | |
Dir::Up => Dir::Left, | |
Dir::Down => Dir::Right, | |
Dir::Left => Dir::Down, | |
Dir::Right => Dir::Up, | |
}; | |
// Set next breakpoint | |
breakpoint = i + travel; | |
} | |
match dir { | |
Dir::Up => point.y += 1, | |
Dir::Down => point.y -= 1, | |
Dir::Left => point.x -= 1, | |
Dir::Right => point.x += 1, | |
}; | |
} | |
point | |
} | |
fn main() { // 361527 | |
let Point {x, y} = calc_position(361527); | |
let answer = x + y; | |
println!("X: {}, Y: {}; Answer = {}", x, y, answer); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment