diff options
Diffstat (limited to '2016/aoc19')
-rw-r--r-- | 2016/aoc19/Cargo.lock | 4 | ||||
-rw-r--r-- | 2016/aoc19/Cargo.toml | 6 | ||||
-rw-r--r-- | 2016/aoc19/src/main.rs | 42 |
3 files changed, 52 insertions, 0 deletions
diff --git a/2016/aoc19/Cargo.lock b/2016/aoc19/Cargo.lock new file mode 100644 index 0000000..7dc8a26 --- /dev/null +++ b/2016/aoc19/Cargo.lock @@ -0,0 +1,4 @@ +[root] +name = "aoc19" +version = "0.1.0" + diff --git a/2016/aoc19/Cargo.toml b/2016/aoc19/Cargo.toml new file mode 100644 index 0000000..17889e3 --- /dev/null +++ b/2016/aoc19/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "aoc19" +version = "0.1.0" +authors = ["Justin Worthe <justin.worthe@gmail.com>"] + +[dependencies] diff --git a/2016/aoc19/src/main.rs b/2016/aoc19/src/main.rs new file mode 100644 index 0000000..1930d8d --- /dev/null +++ b/2016/aoc19/src/main.rs @@ -0,0 +1,42 @@ +fn main() { + part1(); + part2(); +} + +fn part1() { + let mut elves = (0..3017957).map(|i| i+1).collect::<Vec<_>>(); + + let mut eliminate_mod = 1; + + while elves.len() > 1 { + let next_mod = if elves.len() % 2 == eliminate_mod { 0 } else { 1 }; + elves = elves.iter() + .enumerate() + .filter(|&(i, _)| i % 2 != eliminate_mod) + .map(|(_, &e)| e) + .collect(); + eliminate_mod = next_mod; + } + + let elf = elves[0]; + println!("Elf {} gets all the presents in part 1", elf); +} + +fn part2() { + let mut elves = (0..3017957).map(|i| i+1).collect::<Vec<_>>(); + + let mut i = 0; + while elves.len() > 1 { + i = i % elves.len(); + let to_eliminate = (i + elves.len() / 2) % elves.len(); +// println!("Elf {} is taking presents from elf {}", elves[i], elves[to_eliminate]); + elves.remove(to_eliminate); + if to_eliminate > i { + i += 1; + } + } + + let elf = elves[0]; + println!("Elf {} gets all the presents in part 2", elf); + +} |