day(11): refactor simulation
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
d56154f5c2
commit
837a579827
1 changed files with 38 additions and 49 deletions
|
@ -119,7 +119,7 @@ impl FromStr for Monkey {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn round(monkeys: &mut Input, lcm: usize, div: bool) {
|
fn round(monkeys: &mut Input, lcm: usize, modular: bool) {
|
||||||
let mut q: VecDeque<(usize, usize)> = VecDeque::new();
|
let mut q: VecDeque<(usize, usize)> = VecDeque::new();
|
||||||
|
|
||||||
for i in 0..monkeys.len() {
|
for i in 0..monkeys.len() {
|
||||||
|
@ -127,10 +127,11 @@ fn round(monkeys: &mut Input, lcm: usize, div: bool) {
|
||||||
|
|
||||||
for j in 0..m.items.len() {
|
for j in 0..m.items.len() {
|
||||||
let mut worry = m.operation.apply(m.items[j]);
|
let mut worry = m.operation.apply(m.items[j]);
|
||||||
if div {
|
if modular {
|
||||||
|
worry %= lcm;
|
||||||
|
} else {
|
||||||
worry /= 3;
|
worry /= 3;
|
||||||
}
|
}
|
||||||
worry %= lcm;
|
|
||||||
|
|
||||||
q.push_back((m.test.get(worry), worry));
|
q.push_back((m.test.get(worry), worry));
|
||||||
m.inspections += 1;
|
m.inspections += 1;
|
||||||
|
@ -156,36 +157,7 @@ fn euclid(a: usize, b: usize) -> usize {
|
||||||
a
|
a
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Day11;
|
fn simulate_rounds(input: &Input, rounds: usize, modular: bool) -> Output {
|
||||||
impl Solution<Input, Output> for Day11 {
|
|
||||||
fn parse_input<P: AsRef<Path>>(pathname: P) -> Input {
|
|
||||||
file_to_string(pathname)
|
|
||||||
.split("\n\n")
|
|
||||||
.map(|m| m.parse().unwrap())
|
|
||||||
.collect_vec()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn part_1(input: &Input) -> Output {
|
|
||||||
let mut monkeys = input.clone();
|
|
||||||
|
|
||||||
let lcm = monkeys
|
|
||||||
.iter()
|
|
||||||
.map(|m| m.test.0)
|
|
||||||
.fold(1, |x, y| x * y / euclid(x, y));
|
|
||||||
|
|
||||||
for _ in 0..20 {
|
|
||||||
round(&mut monkeys, lcm, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
monkeys
|
|
||||||
.iter()
|
|
||||||
.map(|m| m.inspections)
|
|
||||||
.sorted_by_key(|&i| Reverse(i))
|
|
||||||
.take(2)
|
|
||||||
.product()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn part_2(input: &Input) -> Output {
|
|
||||||
let mut monkeys = input.clone();
|
let mut monkeys = input.clone();
|
||||||
|
|
||||||
let lcm = monkeys
|
let lcm = monkeys
|
||||||
|
@ -197,8 +169,8 @@ impl Solution<Input, Output> for Day11 {
|
||||||
1, 20, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 1000,
|
1, 20, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 1000,
|
||||||
];
|
];
|
||||||
|
|
||||||
for r in 0..10000 {
|
for r in 0..rounds {
|
||||||
round(&mut monkeys, lcm, false);
|
round(&mut monkeys, lcm, modular);
|
||||||
|
|
||||||
if inspect.contains(&(r + 1)) {
|
if inspect.contains(&(r + 1)) {
|
||||||
debug!(
|
debug!(
|
||||||
|
@ -216,6 +188,23 @@ impl Solution<Input, Output> for Day11 {
|
||||||
.take(2)
|
.take(2)
|
||||||
.product()
|
.product()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Day11;
|
||||||
|
impl Solution<Input, Output> for Day11 {
|
||||||
|
fn parse_input<P: AsRef<Path>>(pathname: P) -> Input {
|
||||||
|
file_to_string(pathname)
|
||||||
|
.split("\n\n")
|
||||||
|
.map(|m| m.parse().unwrap())
|
||||||
|
.collect_vec()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_1(input: &Input) -> Output {
|
||||||
|
simulate_rounds(input, 20, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part_2(input: &Input) -> Output {
|
||||||
|
simulate_rounds(input, 10000, true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
|
|
Loading…
Reference in a new issue