day11 part2 done
This commit is contained in:
parent
da8635c06b
commit
5441906ea8
1 changed files with 18 additions and 128 deletions
|
@ -24,10 +24,10 @@ impl Default for Operation {
|
|||
}
|
||||
|
||||
impl Monkey {
|
||||
pub fn inspect(&mut self) -> Vec<(usize, u128)> {
|
||||
pub fn inspect(&mut self, reducer: &dyn Fn(u128) -> u128) -> Vec<(usize, u128)> {
|
||||
let mut out = Vec::with_capacity(self.items.len());
|
||||
while let Some(item) = self.items.pop_front() {
|
||||
let item = self.op(item) / 3;
|
||||
let item = reducer(self.op(item));
|
||||
if item % self.tmod == 0 {
|
||||
out.push((self.target.0, item));
|
||||
} else {
|
||||
|
@ -115,7 +115,7 @@ fn part1(troop: &[Monkey]) -> u128 {
|
|||
for _round in 0..20 {
|
||||
for i in 0..troop.len() {
|
||||
let monkey = &mut troop[i];
|
||||
let items = monkey.inspect();
|
||||
let items = monkey.inspect(&|item| item / 3);
|
||||
for (target, item) in items {
|
||||
let m = &mut troop[target];
|
||||
m.items.push_back(item);
|
||||
|
@ -128,7 +128,20 @@ fn part1(troop: &[Monkey]) -> u128 {
|
|||
|
||||
#[aoc_run(day11, part2)]
|
||||
fn part2(troop: &[Monkey]) -> u128 {
|
||||
0
|
||||
let max: u128 = troop.iter().map(|m| m.tmod).product();
|
||||
let mut troop = troop.to_owned();
|
||||
for _round in 0..10_000 {
|
||||
for i in 0..troop.len() {
|
||||
let monkey = &mut troop[i];
|
||||
let items = monkey.inspect(&|item| item % max);
|
||||
for (target, item) in items {
|
||||
let m = &mut troop[target];
|
||||
m.items.push_back(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
troop.sort_by(|m1, m2| m2.business.cmp(&m1.business));
|
||||
troop.iter().take(2).map(|m| m.business).product()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -172,129 +185,6 @@ Monkey 3:
|
|||
#[test]
|
||||
fn part2_test() {
|
||||
let v = parse_input(INPUT);
|
||||
assert_eq!(part2(&v), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leah_test() {
|
||||
let troop = parse_input(
|
||||
"Monkey 0:
|
||||
Starting items: 54, 82, 90, 88, 86, 54
|
||||
Operation: new = old * 7
|
||||
Test: divisible by 11
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 6
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 91, 65
|
||||
Operation: new = old * 13
|
||||
Test: divisible by 5
|
||||
If true: throw to monkey 7
|
||||
If false: throw to monkey 4
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 62, 54, 57, 92, 83, 63, 63
|
||||
Operation: new = old + 1
|
||||
Test: divisible by 7
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 7
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 67, 72, 68
|
||||
Operation: new = old * old
|
||||
Test: divisible by 2
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 6
|
||||
|
||||
Monkey 4:
|
||||
Starting items: 68, 89, 90, 86, 84, 57, 72, 84
|
||||
Operation: new = old + 7
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 3
|
||||
If false: throw to monkey 5
|
||||
|
||||
Monkey 5:
|
||||
Starting items: 79, 83, 64, 58
|
||||
Operation: new = old + 6
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 3
|
||||
If false: throw to monkey 0
|
||||
|
||||
Monkey 6:
|
||||
Starting items: 96, 72, 89, 70, 88
|
||||
Operation: new = old + 4
|
||||
Test: divisible by 3
|
||||
If true: throw to monkey 1
|
||||
If false: throw to monkey 2
|
||||
|
||||
Monkey 7:
|
||||
Starting items: 79
|
||||
Operation: new = old + 8
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 4
|
||||
If false: throw to monkey 5",
|
||||
);
|
||||
assert_eq!(part1(&troop), 78960);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rippy_part1() {
|
||||
let input = "Monkey 0:
|
||||
Starting items: 96, 60, 68, 91, 83, 57, 85
|
||||
Operation: new = old * 2
|
||||
Test: divisible by 17
|
||||
If true: throw to monkey 2
|
||||
If false: throw to monkey 5
|
||||
|
||||
Monkey 1:
|
||||
Starting items: 75, 78, 68, 81, 73, 99
|
||||
Operation: new = old + 3
|
||||
Test: divisible by 13
|
||||
If true: throw to monkey 7
|
||||
If false: throw to monkey 4
|
||||
|
||||
Monkey 2:
|
||||
Starting items: 69, 86, 67, 55, 96, 69, 94, 85
|
||||
Operation: new = old + 6
|
||||
Test: divisible by 19
|
||||
If true: throw to monkey 6
|
||||
If false: throw to monkey 5
|
||||
|
||||
Monkey 3:
|
||||
Starting items: 88, 75, 74, 98, 80
|
||||
Operation: new = old + 5
|
||||
Test: divisible by 7
|
||||
If true: throw to monkey 7
|
||||
If false: throw to monkey 1
|
||||
|
||||
Monkey 4:
|
||||
Starting items: 82
|
||||
Operation: new = old + 8
|
||||
Test: divisible by 11
|
||||
If true: throw to monkey 0
|
||||
If false: throw to monkey 2
|
||||
|
||||
Monkey 5:
|
||||
Starting items: 72, 92, 92
|
||||
Operation: new = old * 5
|
||||
Test: divisible by 3
|
||||
If true: throw to monkey 6
|
||||
If false: throw to monkey 3
|
||||
|
||||
Monkey 6:
|
||||
Starting items: 74, 61
|
||||
Operation: new = old * old
|
||||
Test: divisible by 2
|
||||
If true: throw to monkey 3
|
||||
If false: throw to monkey 1
|
||||
|
||||
Monkey 7:
|
||||
Starting items: 76, 86, 83, 55
|
||||
Operation: new = old + 4
|
||||
Test: divisible by 5
|
||||
If true: throw to monkey 4
|
||||
If false: throw to monkey 0";
|
||||
let troop = parse_input(input);
|
||||
assert_eq!(part1(&troop), 56595);
|
||||
assert_eq!(part2(&v), 2713310158);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue