-
-
Save per-gron/d9224069b95e882f838cc968861aec8c to your computer and use it in GitHub Desktop.
Advent of Code 3 2017 part 1
This file contains hidden or 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
/* | |
the largest number in the square of (odd) width x is a(x) = x^2 | |
the smallest number in the square of (odd) width x is b(x) = a(x-2)+1 = (x-2)^2+1 = x^2 - 4x5 | |
the number x is in the square of width c(x) = ceil((x^0.5-1)/2)*2+1 | |
the number of steps that x is from the smallest number in its square is d(x) = x - b(c(x)) | |
assuming the number x is on the top row, its distance from the rightmost number is e(x) = d(x) - c(x) + 2 | |
the distance from the centermost number out to the edge is then h(x) = floor(c(x)/2)) | |
its distance from the centermost number is then f(x) = abs(e(x) - h(x)) | |
the manhattan distance to the center is then g(x) = f(x) + h(x) | |
g(x) = abs(e(x) - floor(c(x)/2)) + floor(c(x)/2) | |
g(x) = abs(d(x) - c(x) + 2 - floor(c(x)/2)) + floor(c(x)/2) | |
g(x) = abs(x - b(c(x)) - c(x) + 2 - floor(c(x)/2)) + floor(c(x)/2) | |
g(x) = abs(x - b(ceil((x^0.5-1)/2)*2+1) - ceil((x^0.5-1)/2)*2+1 + 2 - floor(ceil((x^0.5-1)/2)*2+1/2)) + floor(ceil((x^0.5-1)/2)*2+1/2) | |
g(x) = abs(x - (ceil((x^0.5-1)/2)*2+1-2)^2+1 - ceil((x^0.5-1)/2)*2+1 + 2 - floor(ceil((x^0.5-1)/2)*2+1/2)) + floor(ceil((x^0.5-1)/2)*2+1/2) | |
g(x) = abs(x - (ceil((x^0.5-1)/2)*2+1-2)^2+1 - ceil((x^0.5-1)/2)*2+1 + 2 - floor(ceil((x^0.5-1)/2)*2+1/2)) + floor(ceil((x^0.5-1)/2)*2+1/2) | |
c(347991) = 591 | |
b(c(347991)) = 591^2 - 4*591 + 5 = 346922 | |
347991 - b(c(347991)) = 347991 - 346922 = 1069 | |
1069 - 591 = 478 | |
*/ | |
/// The largest number in the square of (odd) width x | |
fn a(x: f64) -> f64 { | |
x * x | |
} | |
/// The smallest number in the square of (odd) width x | |
fn b(x: f64) -> f64 { | |
a(x - 2.) + 1. | |
} | |
/// The width of the square that x is in | |
fn c(x: f64) -> f64 { | |
((x.sqrt() - 1.) / 2.).ceil() * 2. + 1. | |
} | |
/// The number of steps that x is from the smallest number in its square | |
fn d(x: f64) -> f64 { | |
x - b(c(x)) | |
} | |
/// Assuming the number is on the top row, returns its distance from the | |
/// rightmost number. | |
fn e(x: f64) -> f64 { | |
d(x) - c(x) + 2. | |
} | |
/// The distance from the centermost number to the edge | |
fn f(x: f64) -> f64 { | |
(e(x) - h(x)).abs() | |
} | |
/// The Manhattan distance from the center to this number | |
fn g(x: f64) -> f64 { | |
f(x) + h(x) | |
} | |
/// The distance from 1 out to the square that x is in | |
fn h(x: f64) -> f64 { | |
(c(x) / 2.).floor() | |
} | |
fn main() { | |
println!("{}", g(347991.)); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment