Skip to content

Instantly share code, notes, and snippets.

@jostly
Created December 8, 2022 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jostly/8ecb4db720b60e07421040c6cd0b643d to your computer and use it in GitHub Desktop.
Save jostly/8ecb4db720b60e07421040c6cd0b643d to your computer and use it in GitHub Desktop.
fn main() {
let n = usize::MAX/10;
println!("Part 1");
println!("Q: {}, A: {}", n, steal_from_left(n));
println!("Part 2");
println!("Q: {}, A: {}", n, steal_across(n));
}
/*
OUTPUT:
Part 1
Q: 1844674407370955161, A: 1383505805528216371
Part 2
Q: 1844674407370955161, A: 493822689697963072
*/
fn steal_from_left(n: usize) -> usize {
return match n {
x if x <= 2 => 1,
x if is_odd(x) => steal_from_left(n / 2) * 2 + 1,
_ => (steal_from_left(n / 2) - 1) * 2 + 1
};
}
fn steal_across(n: usize) -> usize {
let mut base = 1;
let mut limit = 3;
while n > limit {
base = limit;
limit *= 3;
}
return half_dist(n-base, limit-base);
}
fn half_dist(i: usize, n: usize) -> usize {
if i <= n / 2 {
i
} else {
(i - n / 2) * 2 + n / 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment