Day 17 - base strudture & parsing
This commit is contained in:
parent
c443abf34b
commit
052e381611
4 changed files with 60 additions and 30 deletions
1
res/17/input.txt
Normal file
1
res/17/input.txt
Normal file
File diff suppressed because one or more lines are too long
|
@ -1,5 +1,5 @@
|
||||||
use std::fs;
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
pub fn solve() {
|
pub fn solve() {
|
||||||
let path = "res/16/input.txt";
|
let path = "res/16/input.txt";
|
||||||
|
@ -7,43 +7,38 @@ pub fn solve() {
|
||||||
let contents = fs::read_to_string(path).expect("File read error");
|
let contents = fs::read_to_string(path).expect("File read error");
|
||||||
|
|
||||||
// maps valve name to list of adjacents with (name, flow rate (of this valve), minutes)
|
// maps valve name to list of adjacents with (name, flow rate (of this valve), minutes)
|
||||||
let mut valves = Vec::<(&str, i32, Vec<&str>)>::new();
|
let mut valves = Vec::<(&str, i32, Vec<String>)>::new();
|
||||||
contents
|
contents
|
||||||
.lines()
|
.lines()
|
||||||
.filter(|line| *line != "")
|
.filter(|line| *line != "")
|
||||||
.for_each(|line| {
|
.for_each(|line| {
|
||||||
let (valve, conn_valves) = line.split_at(line.find(";").unwrap());
|
let (valve, conn_valves) = line.split_at(line.find(";").unwrap());
|
||||||
|
|
||||||
let conn_valves2 = conn_valves
|
let conn_valves = conn_valves
|
||||||
|
.clone()
|
||||||
.replace("; tunnels lead to valve", "")
|
.replace("; tunnels lead to valve", "")
|
||||||
.replace("s", "")
|
.replace("s", "")
|
||||||
.replace(" ", "");
|
.replace(" ", "");
|
||||||
let conn_valves3: Vec<&str> = conn_valves2.split(",").collect();
|
|
||||||
|
let conn_valves: Vec<String> =
|
||||||
|
conn_valves.split(",").map(|str| str.to_string()).collect();
|
||||||
|
|
||||||
let valve: Vec<&str> = valve.split_whitespace().collect();
|
let valve: Vec<&str> = valve.split_whitespace().collect();
|
||||||
let flow_rate = valve[4].replace("rate=", "").parse::<i32>().unwrap();
|
let flow_rate = valve[4].replace("rate=", "").parse::<i32>().unwrap();
|
||||||
|
|
||||||
valves.push((valve[1], flow_rate, conn_valves3));
|
valves.push((valve[1], flow_rate, conn_valves));
|
||||||
});
|
});
|
||||||
|
|
||||||
dbg!(valves);
|
//dbg!(valves);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// maps valve name to array index
|
// maps valve name to array index
|
||||||
let mut v_id = HashMap::<&str, i32>::new();
|
let mut v_id = HashMap::<&str, i32>::new();
|
||||||
valves
|
valves.iter().enumerate().for_each(|(i, valve)| {
|
||||||
.iter()
|
v_id.insert(valve.0, i as i32);
|
||||||
.enumerate()
|
});
|
||||||
.for_each(|(i, valve)| { v_id.insert(valve.0, i as i32);} );
|
|
||||||
|
|
||||||
// amount of valves with flow rates > 0
|
// amount of valves with flow rates > 0
|
||||||
let v_pos_num = valves
|
let v_pos_num = valves.iter().filter(|valve| valve.1 > 0).count();
|
||||||
.iter()
|
|
||||||
.filter(|valve| valve.1 > 0)
|
|
||||||
.count();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let v_num = valves.len();
|
let v_num = valves.len();
|
||||||
let v_pos_bits = 1 << v_pos_num; // 2 ^ v_pos_num ?
|
let v_pos_bits = 1 << v_pos_num; // 2 ^ v_pos_num ?
|
||||||
|
@ -51,32 +46,39 @@ pub fn solve() {
|
||||||
let mut valves_by_id = vec![(0, Vec::<i32>::new()); v_num];
|
let mut valves_by_id = vec![(0, Vec::<i32>::new()); v_num];
|
||||||
valves.iter().for_each(|valve| {
|
valves.iter().for_each(|valve| {
|
||||||
let i = v_id[valve.0];
|
let i = v_id[valve.0];
|
||||||
valves_by_id[i as usize] = (valve.1, valve.2.iter().map(|adj| v_id[*adj]).collect::<Vec::<i32>>());
|
valves_by_id[i as usize] = (
|
||||||
|
valve.1,
|
||||||
|
valve
|
||||||
|
.2
|
||||||
|
.iter()
|
||||||
|
.map(|adj| v_id[adj.as_str()])
|
||||||
|
.collect::<Vec<i32>>(),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let mut dp = vec![vec![vec![0; v_pos_bits]; v_num]; 30];
|
||||||
let mut dp = vec![vec![vec![0;v_pos_bits];v_num];30];
|
|
||||||
|
|
||||||
for t in 1..30 {
|
for t in 1..30 {
|
||||||
for i in 0..v_num {
|
for i in 0..v_num {
|
||||||
let i_bit = 1 << i; // i'th valve is open
|
let i_bit = 1 << i; // i'th valve is open
|
||||||
for j in 0..v_pos_bits {
|
for j in 0..v_pos_bits {
|
||||||
let mut curr = dp[t][i][j];
|
let mut curr = dp[t][i][j];
|
||||||
if i_bit & j != 0 && t >= 2 { // current j represents state where i'th valve is open
|
if i_bit & j != 0 && t >= 2 {
|
||||||
|
// current j represents state where i'th valve is open
|
||||||
curr = curr.max(
|
curr = curr.max(
|
||||||
dp[t-1][i][j-i_bit] // value without valve open
|
dp[t-1][i][j-i_bit] // value without valve open
|
||||||
+ valves_by_id[i].0 * t as i32) // plus valve open at current time
|
+ valves_by_id[i].0 * t as i32,
|
||||||
|
) // plus valve open at current time
|
||||||
}
|
}
|
||||||
valves_by_id[i].1.iter().for_each(|adj| {
|
valves_by_id[i].1.iter().for_each(|adj| {
|
||||||
curr = curr.max(dp[t-1][*adj as usize][j]); // if larger, come from adjacent
|
curr = curr.max(dp[t - 1][*adj as usize][j]); // if larger, come from adjacent
|
||||||
});
|
});
|
||||||
dp[t][i][j] = curr;
|
dp[t][i][j] = curr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let res = dp[29][*v_id.get(&"AA").unwrap() as usize][v_pos_bits-1];
|
let res = dp[29][*v_id.get(&"AA").unwrap() as usize][v_pos_bits - 1];
|
||||||
println!("Result 1: {res:?}");
|
println!("Result 1: {res:?}");
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
27
src/days/d17.rs
Normal file
27
src/days/d17.rs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
const WIDTH: usize = 7;
|
||||||
|
|
||||||
|
pub fn solve() {
|
||||||
|
let path = "res/17/input.txt";
|
||||||
|
|
||||||
|
let contents = fs::read_to_string(path).expect("I/O error, wrong path?");
|
||||||
|
|
||||||
|
let directions: Vec<Dir> = contents
|
||||||
|
.chars()
|
||||||
|
.filter(|c| *c != '\n')
|
||||||
|
.map(|c| match c {
|
||||||
|
'>' => Dir::Right,
|
||||||
|
'<' => Dir::Left,
|
||||||
|
_ => std::unreachable!(),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
// [width][height]
|
||||||
|
let chamber = vec![Vec::<bool>::new(); WIDTH];
|
||||||
|
}
|
||||||
|
|
||||||
|
enum Dir {
|
||||||
|
Right,
|
||||||
|
Left,
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
pub mod days;
|
pub mod days;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
days::d16::solve()
|
days::d17::solve()
|
||||||
//_all_days()
|
//_all_days()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue