mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Add solutions to day 19.
This commit is contained in:
6
2016/day-19/Cargo.toml
Normal file
6
2016/day-19/Cargo.toml
Normal 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
41
2016/day-19/src/main.rs
Normal 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);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user