|
| 1 | +use std::io::Read; |
| 2 | + |
| 3 | +fn find_max( |
| 4 | + costs: &[[u16; 3]; 4], |
| 5 | + inventory: [u16; 4], |
| 6 | + bots: [u16; 4], |
| 7 | + time: u16, |
| 8 | + level: u8, |
| 9 | + best: &mut u16, |
| 10 | +) { |
| 11 | + const MAX_TIME: u16 = 24; |
| 12 | + let mut time_to_geode_bot = u16::MAX; |
| 13 | + let mut built_bot = false; |
| 14 | + |
| 15 | + 'outer: for (n, (&cost, &bot)) in costs.iter().zip(&bots).enumerate().rev() { |
| 16 | + if n < costs[0].len() && costs.iter().all(|cost| bot >= cost[n]) { |
| 17 | + continue; |
| 18 | + } |
| 19 | + |
| 20 | + let mut needed_time = 0; |
| 21 | + for ((&cost, &inventory), &bots) in cost.iter().zip(&inventory).zip(&bots) { |
| 22 | + if inventory < cost { |
| 23 | + if bots == 0 { |
| 24 | + continue 'outer; |
| 25 | + } |
| 26 | + let needed_resources = cost - inventory; |
| 27 | + needed_time = std::cmp::max( |
| 28 | + needed_time, |
| 29 | + needed_resources / bots + if needed_resources % bots != 0 { 1 } else { 0 }, |
| 30 | + ) |
| 31 | + } |
| 32 | + } |
| 33 | + needed_time += 1; |
| 34 | + |
| 35 | + if n == bots.len() - 1 { |
| 36 | + time_to_geode_bot = needed_time; |
| 37 | + } else if needed_time >= time_to_geode_bot { |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + if time + needed_time >= MAX_TIME { |
| 42 | + continue; |
| 43 | + } |
| 44 | + |
| 45 | + let mut new_inventory = inventory; |
| 46 | + for (inv, &bots) in new_inventory.iter_mut().zip(&bots) { |
| 47 | + *inv += bots * needed_time; |
| 48 | + } |
| 49 | + for (inv, &cost) in new_inventory.iter_mut().zip(&cost) { |
| 50 | + *inv -= cost; |
| 51 | + } |
| 52 | + |
| 53 | + let mut new_bots = bots; |
| 54 | + new_bots[n] += 1; |
| 55 | + |
| 56 | + let remaining_time = MAX_TIME - (time + needed_time); |
| 57 | + if new_inventory.last().unwrap() + |
| 58 | + new_bots.last().unwrap() * remaining_time + |
| 59 | + remaining_time * (remaining_time - 1) / 2 <= *best { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + find_max(costs, new_inventory, new_bots, time + needed_time, level + 1, best); |
| 64 | + built_bot = true; |
| 65 | + } |
| 66 | + |
| 67 | + if !built_bot { |
| 68 | + *best = std::cmp::max(*best, inventory.last().unwrap() + bots.last().unwrap() * (MAX_TIME - time)); |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +fn main() { |
| 73 | + let mut input = String::new(); |
| 74 | + std::io::stdin().read_to_string(&mut input).unwrap(); |
| 75 | + |
| 76 | + println!("{}", input.lines().map(|line| { |
| 77 | + let mut words = line.split_whitespace(); |
| 78 | + let id: u16 = words.nth(1).unwrap().trim_end_matches(':').parse().unwrap(); |
| 79 | + let costs: [[u16; 3]; 4] = [ |
| 80 | + [words.nth(4).unwrap().parse().unwrap(), 0, 0], |
| 81 | + [words.nth(5).unwrap().parse().unwrap(), 0, 0], |
| 82 | + [words.nth(5).unwrap().parse().unwrap(), words.nth(2).unwrap().parse().unwrap(), 0], |
| 83 | + [words.nth(5).unwrap().parse().unwrap(), 0, words.nth(2).unwrap().parse().unwrap()], |
| 84 | + ]; |
| 85 | + |
| 86 | + let mut result = 0; |
| 87 | + find_max(&costs, [0; 4], [1, 0, 0, 0], 0, 0, &mut result); |
| 88 | + id * result |
| 89 | + }).sum::<u16>()); |
| 90 | +} |
0 commit comments