Skip to content
Permalink
Browse files

Add buy

  • Loading branch information...
ichyo committed Jun 24, 2019
1 parent bf150e2 commit 33101907de8c46b3965f84ff93646ab334231f51
Showing with 56 additions and 14 deletions.
  1. +11 −6 src/main.rs
  2. +1 −1 src/mine.rs
  3. +29 −0 src/models.rs
  4. +3 −0 src/parse.rs
  5. +12 −7 src/solve.rs
@@ -7,11 +7,14 @@ use std::io::Write;
use icfpc::models::*;
use icfpc::parse::read_all_inputs;
use icfpc::solve::solve_small_while;
use icfpc::solve::determine_buy;
use std::time::Duration;

fn solve<W: Write>(task: Task, f: &mut W, duration: Duration) {
let cmds = solve_small_while(task, duration);
fn solve<W: Write>(task: Task, f: &mut W, b: &mut W, duration: Duration) {
let buy = determine_buy(&task);
let cmds = solve_small_while(task, &buy, duration);
write!(f, "{}", cmds).unwrap();
write!(b, "{}", buy).unwrap();
}

fn main() {
@@ -49,15 +52,17 @@ fn main() {
let inputs = read_all_inputs(&input_root);
let progress_bar = ProgressBar::new(inputs.len() as u64);
inputs.into_par_iter().for_each(|input| {
let mut output_file: Box<dyn Write> = match output_root {
let (mut output_file, mut buy_file): (Box<dyn Write>, Box<dyn Write>) = match output_root {
Some(output_root) => {
let output_path = format!("{}/{}", output_root, input.output_file_name());
let output_file = File::create(&output_path).unwrap();
Box::new(output_file)
let buy_path = format!("{}/{}", output_root, input.buy_file_name());
let buy_file = File::create(&buy_path).unwrap();
(Box::new(output_file), Box::new(buy_file))
}
None => Box::new(std::io::stdout()),
None => (Box::new(std::io::stdout()), Box::new(std::io::sink())),
};
solve(input.task, &mut output_file, duration);
solve(input.task, &mut output_file, &mut buy_file, duration);
progress_bar.inc(1);
});
progress_bar.finish();
@@ -131,7 +131,7 @@ impl Client {
info!("solving puzzle");
let puzzle_answer = solve_puzzle(puzzle);
info!("solving task");
let task_answer = solve_small_while(task, std::time::Duration::from_secs(300));
let task_answer = solve_small_while(task, &Buy::empty(), std::time::Duration::from_secs(300));
info!("dumping");

self.dump_task_answer(block, task_answer);
@@ -411,3 +411,32 @@ impl fmt::Display for Commands {
)
}
}

pub struct Buy(Vec<BoosterType>);

impl Buy {
pub fn empty() -> Buy {
Buy::new(Vec::new())
}
pub fn new(bs: Vec<BoosterType>) -> Buy {
Buy(bs)
}
pub fn push(&mut self, b: &BoosterType) {
self.0.push(b.clone());
}
}

impl fmt::Display for Buy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
self.0
.iter()
.map(|b| {
format!("{}", b)
})
.collect::<String>()
)
}
}
@@ -22,6 +22,9 @@ impl Input {
pub fn output_file_name(&self) -> String {
format!("prob-{}.sol", self.id)
}
pub fn buy_file_name(&self) -> String {
format!("prob-{}.buy", self.id)
}
}

pub fn read_all_inputs(dir: &str) -> Vec<Input> {
@@ -110,7 +110,7 @@ pub struct State<'a> {
}

impl<'a> State<'a> {
fn initialize(task: &'a Task) -> State<'a> {
fn initialize(task: &'a Task, buy: &Buy) -> State<'a> {
let map_points = task.map.enumerate_points();

let width = task.width;
@@ -138,7 +138,8 @@ impl<'a> State<'a> {
}
}

if task.boosters.iter().all(|b| b.kind != BoosterType::Spawn) {
let no_spawn = task.boosters.iter().all(|b| b.kind != BoosterType::Spawn);
if no_spawn {
remaining_clone = 0;
}

@@ -466,27 +467,31 @@ impl<'a> State<'a> {
}
}

pub fn solve_small_while(task: Task, duration: Duration) -> Commands {
let mut res = solve_small(task.clone());
pub fn solve_small_while(task: Task, buy: &Buy, duration: Duration) -> Commands {
let mut res = solve_small(task.clone(), buy);
let now = Instant::now();;
loop {
if now.elapsed() >= duration {
break;
}
let new = solve_small(task.clone());
let new = solve_small(task.clone(), buy);
if new.len() < res.len() {
res = new;
}
}
res
}

pub fn solve_small(task: Task) -> Commands {
let mut state = State::initialize(&task);
pub fn solve_small(task: Task, buy: &Buy) -> Commands {
let mut state = State::initialize(&task, buy);
loop {
if !state.next_state() {
break;
}
}
state.commands()
}

pub fn determine_buy(task: &Task) -> Buy {
Buy::empty()
}

0 comments on commit 3310190

Please sign in to comment.
You can’t perform that action at this time.