diff --git a/2016/resources/day19.input b/2016/resources/day19.input new file mode 100644 index 0000000..48d3844 --- /dev/null +++ b/2016/resources/day19.input @@ -0,0 +1 @@ +3018458 \ No newline at end of file diff --git a/2016/src/day19.rs b/2016/src/day19.rs new file mode 100644 index 0000000..7121262 --- /dev/null +++ b/2016/src/day19.rs @@ -0,0 +1,37 @@ +use std::str::FromStr; +use crate::file_input::read_input_file; +use std::collections::VecDeque; +use std::time::Instant; + +pub fn part1() { + let mut n = usize::from_str(read_input_file("day19.input").as_str()).unwrap(); + let mut first = 0; + let mut exponent = 1; + while n > 1 { + println!("{} left, {} is first", n, first+1); + if n & 1 == 1 { + first += 1 << exponent; + } + n /= 2; + exponent += 1; + } + println!("{} wins!", first+1); +} + +pub fn part2() { + // This will work, but is hideously slow due to vector resizing + let n = usize::from_str(read_input_file("day19.input").as_str()).unwrap(); + let mut table = VecDeque::new(); + for i in 0..n { + table.push_back(i); + } + let mut current_player = 0; + while table.len() > 1 { + let out_index = (current_player + (table.len() / 2)) % table.len(); + table.remove(out_index); + if out_index > current_player { + current_player = (current_player + 1) % table.len(); + } + } + println!("{} wins!", table[0]+1); +} diff --git a/2016/src/main.rs b/2016/src/main.rs index d3ea4c6..bb4b2ae 100644 --- a/2016/src/main.rs +++ b/2016/src/main.rs @@ -22,6 +22,7 @@ mod day15; mod day16; mod day17; mod day18; +mod day19; fn main() { let mut selection= String::new(); @@ -123,6 +124,11 @@ fn main() { Some("2") => day18::part2(), _ => () }, + Some("19") => match choices.next() { + Some("1") => day19::part1(), + Some("2") => day19::part2(), + _ => () + }, _ => () } let run_time = Instant::now() - start;