diff --git a/2016/day-19/Cargo.toml b/2016/day-19/Cargo.toml new file mode 100644 index 0000000..9ef63fd --- /dev/null +++ b/2016/day-19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "day-19" +version = "0.1.0" +authors = ["Bert Peters "] + +[dependencies] diff --git a/2016/day-19/src/main.rs b/2016/day-19/src/main.rs new file mode 100644 index 0000000..58f43d5 --- /dev/null +++ b/2016/day-19/src/main.rs @@ -0,0 +1,41 @@ +// Part 1 is the josephus function +fn josephus(input: u32) +{ + let input_2 = 1 << (31 - input.leading_zeros()); + + let remainder = input - input_2; + + println!("Position for {} is {}", input, remainder * 2 + 1); +} + +fn power_3(input: u32) -> u32 +{ + let mut base = 1; + while 3 * base < input { + base = base * 3; + } + + return base; +} + +// Part two is similar, but in base 3. +fn alternative(input: u32) +{ + let log = power_3(input); + let remainder = input - log; + + let pos; + + if remainder <= log { + pos = remainder; + } else { + pos = 2 * remainder - log; + } + + println!("Alternative for {} is: {}", input, pos); +} + +fn main() { + josephus(3014603); + alternative(3014603); +}