Add solutions to day 19.

This commit is contained in:
Bert Peters
2016-12-28 15:27:27 +01:00
parent 7416df5689
commit 221aef65ca
2 changed files with 47 additions and 0 deletions

6
2016/day-19/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "day-19"
version = "0.1.0"
authors = ["Bert Peters <bert.ljpeters@gmail.com>"]
[dependencies]

41
2016/day-19/src/main.rs Normal file
View File

@@ -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);
}