diff options
author | Justin Worthe <justin.worthe@gmail.com> | 2016-12-19 10:42:58 +0200 |
---|---|---|
committer | Justin Worthe <justin.worthe@gmail.com> | 2016-12-19 10:42:58 +0200 |
commit | bfa01237199e257611eaaf820e966b73b35fc421 (patch) | |
tree | d3be8dde8345e71c7378a16b6ab3ac6769115585 /aoc19 | |
parent | 3d6b8afea8dfd397186f0be13d6c064e3e7a8ffe (diff) |
AOC19 part 2
Works, but the performance is awful. There must be a better way.
Diffstat (limited to 'aoc19')
-rw-r--r-- | aoc19/src/main.rs | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/aoc19/src/main.rs b/aoc19/src/main.rs index f76fce9..1930d8d 100644 --- a/aoc19/src/main.rs +++ b/aoc19/src/main.rs @@ -1,4 +1,9 @@ fn main() { + part1(); + part2(); +} + +fn part1() { let mut elves = (0..3017957).map(|i| i+1).collect::<Vec<_>>(); let mut eliminate_mod = 1; @@ -14,5 +19,24 @@ fn main() { } let elf = elves[0]; - println!("Elf {} gets all the presents", elf); + 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); + } |