Skip to content

Commit 3310190

Browse files
committed
Add buy
1 parent bf150e2 commit 3310190

File tree

5 files changed

+56
-14
lines changed

5 files changed

+56
-14
lines changed

src/main.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,14 @@ use std::io::Write;
77
use icfpc::models::*;
88
use icfpc::parse::read_all_inputs;
99
use icfpc::solve::solve_small_while;
10+
use icfpc::solve::determine_buy;
1011
use std::time::Duration;
1112

12-
fn solve<W: Write>(task: Task, f: &mut W, duration: Duration) {
13-
let cmds = solve_small_while(task, duration);
13+
fn solve<W: Write>(task: Task, f: &mut W, b: &mut W, duration: Duration) {
14+
let buy = determine_buy(&task);
15+
let cmds = solve_small_while(task, &buy, duration);
1416
write!(f, "{}", cmds).unwrap();
17+
write!(b, "{}", buy).unwrap();
1518
}
1619

1720
fn main() {
@@ -49,15 +52,17 @@ fn main() {
4952
let inputs = read_all_inputs(&input_root);
5053
let progress_bar = ProgressBar::new(inputs.len() as u64);
5154
inputs.into_par_iter().for_each(|input| {
52-
let mut output_file: Box<dyn Write> = match output_root {
55+
let (mut output_file, mut buy_file): (Box<dyn Write>, Box<dyn Write>) = match output_root {
5356
Some(output_root) => {
5457
let output_path = format!("{}/{}", output_root, input.output_file_name());
5558
let output_file = File::create(&output_path).unwrap();
56-
Box::new(output_file)
59+
let buy_path = format!("{}/{}", output_root, input.buy_file_name());
60+
let buy_file = File::create(&buy_path).unwrap();
61+
(Box::new(output_file), Box::new(buy_file))
5762
}
58-
None => Box::new(std::io::stdout()),
63+
None => (Box::new(std::io::stdout()), Box::new(std::io::sink())),
5964
};
60-
solve(input.task, &mut output_file, duration);
65+
solve(input.task, &mut output_file, &mut buy_file, duration);
6166
progress_bar.inc(1);
6267
});
6368
progress_bar.finish();

src/mine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl Client {
131131
info!("solving puzzle");
132132
let puzzle_answer = solve_puzzle(puzzle);
133133
info!("solving task");
134-
let task_answer = solve_small_while(task, std::time::Duration::from_secs(300));
134+
let task_answer = solve_small_while(task, &Buy::empty(), std::time::Duration::from_secs(300));
135135
info!("dumping");
136136

137137
self.dump_task_answer(block, task_answer);

src/models.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,32 @@ impl fmt::Display for Commands {
411411
)
412412
}
413413
}
414+
415+
pub struct Buy(Vec<BoosterType>);
416+
417+
impl Buy {
418+
pub fn empty() -> Buy {
419+
Buy::new(Vec::new())
420+
}
421+
pub fn new(bs: Vec<BoosterType>) -> Buy {
422+
Buy(bs)
423+
}
424+
pub fn push(&mut self, b: &BoosterType) {
425+
self.0.push(b.clone());
426+
}
427+
}
428+
429+
impl fmt::Display for Buy {
430+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
431+
write!(
432+
f,
433+
"{}",
434+
self.0
435+
.iter()
436+
.map(|b| {
437+
format!("{}", b)
438+
})
439+
.collect::<String>()
440+
)
441+
}
442+
}

src/parse.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ impl Input {
2222
pub fn output_file_name(&self) -> String {
2323
format!("prob-{}.sol", self.id)
2424
}
25+
pub fn buy_file_name(&self) -> String {
26+
format!("prob-{}.buy", self.id)
27+
}
2528
}
2629

2730
pub fn read_all_inputs(dir: &str) -> Vec<Input> {

src/solve.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub struct State<'a> {
110110
}
111111

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

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

141-
if task.boosters.iter().all(|b| b.kind != BoosterType::Spawn) {
141+
let no_spawn = task.boosters.iter().all(|b| b.kind != BoosterType::Spawn);
142+
if no_spawn {
142143
remaining_clone = 0;
143144
}
144145

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

469-
pub fn solve_small_while(task: Task, duration: Duration) -> Commands {
470-
let mut res = solve_small(task.clone());
470+
pub fn solve_small_while(task: Task, buy: &Buy, duration: Duration) -> Commands {
471+
let mut res = solve_small(task.clone(), buy);
471472
let now = Instant::now();;
472473
loop {
473474
if now.elapsed() >= duration {
474475
break;
475476
}
476-
let new = solve_small(task.clone());
477+
let new = solve_small(task.clone(), buy);
477478
if new.len() < res.len() {
478479
res = new;
479480
}
480481
}
481482
res
482483
}
483484

484-
pub fn solve_small(task: Task) -> Commands {
485-
let mut state = State::initialize(&task);
485+
pub fn solve_small(task: Task, buy: &Buy) -> Commands {
486+
let mut state = State::initialize(&task, buy);
486487
loop {
487488
if !state.next_state() {
488489
break;
489490
}
490491
}
491492
state.commands()
492493
}
494+
495+
pub fn determine_buy(task: &Task) -> Buy {
496+
Buy::empty()
497+
}

0 commit comments

Comments
 (0)