[WIP] Working on day 16p1, but the current solution is way to slow
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
<component name="ProjectRunConfigurationManager">
|
||||
<configuration default="false" name="Run-aoc2022-bench-dayX" type="CargoCommandRunConfiguration" factoryName="Cargo Command">
|
||||
<option name="command" value="run --package advent-of-code-2022-rust --bin advent-of-code-2022-rust --release -- -d 13 -b 100" />
|
||||
<option name="command" value="run --package advent-of-code-2022-rust --bin advent-of-code-2022-rust --release -- -d 15 -b 10" />
|
||||
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
|
||||
<option name="channel" value="DEFAULT" />
|
||||
<option name="requiredFeatures" value="true" />
|
||||
|
||||
59
input/day16.txt
Normal file
59
input/day16.txt
Normal file
@@ -0,0 +1,59 @@
|
||||
Valve FY has flow rate=0; tunnels lead to valves TG, CD
|
||||
Valve EK has flow rate=12; tunnels lead to valves JE, VE, PJ, CS, IX
|
||||
Valve NU has flow rate=0; tunnels lead to valves FG, HJ
|
||||
Valve AY has flow rate=0; tunnels lead to valves EG, KR
|
||||
Valve DH has flow rate=0; tunnels lead to valves FX, VW
|
||||
Valve IX has flow rate=0; tunnels lead to valves VW, EK
|
||||
Valve DZ has flow rate=0; tunnels lead to valves HT, FG
|
||||
Valve YE has flow rate=0; tunnels lead to valves CI, MS
|
||||
Valve OO has flow rate=0; tunnels lead to valves FX, CS
|
||||
Valve SB has flow rate=0; tunnels lead to valves RR, AP
|
||||
Valve HT has flow rate=4; tunnels lead to valves DZ, GA, CI, DE, JS
|
||||
Valve MS has flow rate=11; tunnels lead to valves PJ, WG, CA, YE
|
||||
Valve CD has flow rate=0; tunnels lead to valves UW, FY
|
||||
Valve IZ has flow rate=0; tunnels lead to valves XF, AP
|
||||
Valve JE has flow rate=0; tunnels lead to valves EK, TQ
|
||||
Valve DN has flow rate=0; tunnels lead to valves KR, VE
|
||||
Valve VW has flow rate=13; tunnels lead to valves DH, IX
|
||||
Valve UH has flow rate=0; tunnels lead to valves MN, TQ
|
||||
Valve TB has flow rate=0; tunnels lead to valves AP, BJ
|
||||
Valve XT has flow rate=0; tunnels lead to valves TQ, UW
|
||||
Valve RR has flow rate=0; tunnels lead to valves FG, SB
|
||||
Valve BJ has flow rate=0; tunnels lead to valves TB, AA
|
||||
Valve DE has flow rate=0; tunnels lead to valves HT, WI
|
||||
Valve MT has flow rate=0; tunnels lead to valves EW, FG
|
||||
Valve HJ has flow rate=0; tunnels lead to valves KS, NU
|
||||
Valve WI has flow rate=3; tunnels lead to valves XF, DX, DE, EW
|
||||
Valve KI has flow rate=0; tunnels lead to valves GW, TQ
|
||||
Valve JS has flow rate=0; tunnels lead to valves UW, HT
|
||||
Valve XF has flow rate=0; tunnels lead to valves WI, IZ
|
||||
Valve VE has flow rate=0; tunnels lead to valves DN, EK
|
||||
Valve CI has flow rate=0; tunnels lead to valves YE, HT
|
||||
Valve GW has flow rate=0; tunnels lead to valves EG, KI
|
||||
Valve TQ has flow rate=14; tunnels lead to valves WG, KI, JE, UH, XT
|
||||
Valve AA has flow rate=0; tunnels lead to valves BJ, CF, DX, RB, AQ
|
||||
Valve EW has flow rate=0; tunnels lead to valves MT, WI
|
||||
Valve UW has flow rate=6; tunnels lead to valves XT, CD, NZ, JS
|
||||
Valve MN has flow rate=0; tunnels lead to valves KR, UH
|
||||
Valve FG has flow rate=8; tunnels lead to valves NU, RR, MT, MK, DZ
|
||||
Valve RB has flow rate=0; tunnels lead to valves NZ, AA
|
||||
Valve AQ has flow rate=0; tunnels lead to valves AA, MK
|
||||
Valve WG has flow rate=0; tunnels lead to valves TQ, MS
|
||||
Valve YW has flow rate=0; tunnels lead to valves CA, KR
|
||||
Valve CA has flow rate=0; tunnels lead to valves YW, MS
|
||||
Valve PJ has flow rate=0; tunnels lead to valves MS, EK
|
||||
Valve EG has flow rate=23; tunnels lead to valves AY, GW
|
||||
Valve NC has flow rate=0; tunnels lead to valves TG, KS
|
||||
Valve WY has flow rate=16; tunnel leads to valve VQ
|
||||
Valve AP has flow rate=7; tunnels lead to valves IZ, VQ, TB, SB
|
||||
Valve CF has flow rate=0; tunnels lead to valves GA, AA
|
||||
Valve FX has flow rate=20; tunnels lead to valves DH, OO
|
||||
Valve NZ has flow rate=0; tunnels lead to valves RB, UW
|
||||
Valve KS has flow rate=19; tunnels lead to valves NC, HJ
|
||||
Valve VQ has flow rate=0; tunnels lead to valves WY, AP
|
||||
Valve TG has flow rate=17; tunnels lead to valves NC, FY
|
||||
Valve GA has flow rate=0; tunnels lead to valves CF, HT
|
||||
Valve CS has flow rate=0; tunnels lead to valves OO, EK
|
||||
Valve MK has flow rate=0; tunnels lead to valves AQ, FG
|
||||
Valve KR has flow rate=18; tunnels lead to valves MN, DN, YW, AY
|
||||
Valve DX has flow rate=0; tunnels lead to valves AA, WI
|
||||
10
input/day16_example.txt
Normal file
10
input/day16_example.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
Valve AA has flow rate=0; tunnels lead to valves DD, II, BB
|
||||
Valve BB has flow rate=13; tunnels lead to valves CC, AA
|
||||
Valve CC has flow rate=2; tunnels lead to valves DD, BB
|
||||
Valve DD has flow rate=20; tunnels lead to valves CC, AA, EE
|
||||
Valve EE has flow rate=3; tunnels lead to valves FF, DD
|
||||
Valve FF has flow rate=0; tunnels lead to valves EE, GG
|
||||
Valve GG has flow rate=0; tunnels lead to valves FF, HH
|
||||
Valve HH has flow rate=22; tunnel leads to valve GG
|
||||
Valve II has flow rate=0; tunnels lead to valves AA, JJ
|
||||
Valve JJ has flow rate=21; tunnel leads to valve II
|
||||
87
src/day16.rs
Normal file
87
src/day16.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
use crate::day_solver::DaySolver;
|
||||
|
||||
use super::util;
|
||||
|
||||
pub struct Day16 {
|
||||
valves: Vec<Valve>,
|
||||
initial_valve_idx: usize,
|
||||
}
|
||||
|
||||
impl Day16 {
|
||||
|
||||
pub fn create() -> Self {
|
||||
let lines = util::read_file("input/day16_example.txt");
|
||||
// let lines = util::read_file("input/day16.txt");
|
||||
|
||||
// We first map all the valve names to indexes for performance reasons:
|
||||
let valve_names = lines.iter()
|
||||
.map(|s| s.split_whitespace().skip(1).next().unwrap().to_string())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
// Put the input into the day struct
|
||||
return Day16 {
|
||||
valves: lines.iter().enumerate().map(|(i, s)| {
|
||||
let mut s_split = s.split(";");
|
||||
let flow_rate = s_split.next().unwrap().split("rate=").skip(1).next().unwrap().parse::<u32>().unwrap();
|
||||
let connections = (&s_split.next().unwrap()["tunnels lead to valves ".len()..].trim()).split(", ")
|
||||
.map(|n| valve_names.iter().position(|nn| n.eq(nn)).unwrap())
|
||||
.collect();
|
||||
Valve {
|
||||
id: i, flow_rate, connections
|
||||
}
|
||||
}).collect(),
|
||||
initial_valve_idx: valve_names.iter().position(|n| n == "AA").unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
fn is_open(valve_idx: &usize, open_valves_mask: &u64) -> bool {
|
||||
open_valves_mask & (1u64 << valve_idx) != 0
|
||||
}
|
||||
|
||||
fn is_closed(valve_idx: &usize, open_valves_mask: &u64) -> bool {
|
||||
!Self::is_open(valve_idx, open_valves_mask)
|
||||
}
|
||||
|
||||
fn set_open(valve_idx: &usize, cur_open_valves_mask: &u64) -> u64 {
|
||||
cur_open_valves_mask.clone() | (1u64 << valve_idx)
|
||||
}
|
||||
|
||||
fn find_best_option(&self, cur_valve_idx: &usize, open_valves_mask: u64, cur_flow_rate: u32, minutes_left: u32) -> u32 {
|
||||
|
||||
if minutes_left == 0 { return 0; }
|
||||
|
||||
let cur_valve = &self.valves.get(*cur_valve_idx).unwrap();
|
||||
let mut next_step_options = Vec::new();
|
||||
if Self::is_closed(&cur_valve_idx, &open_valves_mask) && cur_valve.flow_rate > 0 {
|
||||
// One option is to open the valve at the current position
|
||||
next_step_options.push(
|
||||
self.find_best_option(cur_valve_idx, Self::set_open(&cur_valve_idx, &open_valves_mask), cur_flow_rate + cur_valve.flow_rate, minutes_left - 1)
|
||||
);
|
||||
} else {
|
||||
// We try and move to an adjacent valve, see if we can be more useful there
|
||||
for connection in &cur_valve.connections {
|
||||
next_step_options.push(self.find_best_option(connection, open_valves_mask, cur_flow_rate, minutes_left - 1));
|
||||
}
|
||||
}
|
||||
return cur_flow_rate + next_step_options.iter().max().unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl DaySolver for Day16 {
|
||||
|
||||
|
||||
fn solve_part1(&mut self) -> String {
|
||||
self.find_best_option(&self.initial_valve_idx, 0, 0, 10).to_string()
|
||||
}
|
||||
|
||||
fn solve_part2(&mut self) -> String {
|
||||
return 0.to_string();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Valve {
|
||||
id: usize,
|
||||
flow_rate: u32,
|
||||
connections: Vec<usize>
|
||||
}
|
||||
@@ -14,6 +14,7 @@ use crate::day12::Day12;
|
||||
use crate::day13::Day13;
|
||||
use crate::day14::Day14;
|
||||
use crate::day15::Day15;
|
||||
use crate::day16::Day16;
|
||||
use crate::day_solver::DaySolver;
|
||||
|
||||
mod util;
|
||||
@@ -33,6 +34,7 @@ mod day12;
|
||||
mod day13;
|
||||
mod day14;
|
||||
mod day15;
|
||||
mod day16;
|
||||
|
||||
const MAX_DAY: u8 = 15;
|
||||
const DEFAULT_BENCHMARK_AMOUNT: u32 = 100;
|
||||
@@ -113,6 +115,7 @@ fn build_day_solver(day: u8) -> Option<Box<dyn DaySolver>> {
|
||||
13 => Some(Box::new(Day13::create())),
|
||||
14 => Some(Box::new(Day14::create())),
|
||||
15 => Some(Box::new(Day15::create())),
|
||||
16 => Some(Box::new(Day16::create())),
|
||||
_ => None
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user