blob: 1930d8db61acca8994de2635020ccf7ea566467b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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);
}
|