Skip to content
Permalink
Browse files

clone

  • Loading branch information...
ichyo committed Jun 23, 2019
1 parent 733b9aa commit 25abfbcc69ac2c9941c87e3a06052fee0e2aa320
Showing with 169 additions and 40 deletions.
  1. +6 −5 src/bin/score.rs
  2. +1 −1 src/main.rs
  3. +0 −2 src/mine.rs
  4. +54 −11 src/models.rs
  5. +61 −4 src/parse.rs
  6. +47 −17 src/solve.rs
@@ -1,6 +1,7 @@
use clap::{App, Arg};
use icfpc::models::*;
use icfpc::parse::read_all_inputs;
use icfpc::parse::read_commands;
use icfpc::utils::Matrix;
use std::fs::File;
use std::io::Read;
@@ -40,7 +41,7 @@ impl ScoreInfo {
}
}

fn score_small(task: Task, output_len: usize) -> ScoreInfo {
fn score_small(task: Task, commands: Commands) -> ScoreInfo {
let map_points = task.map.enumerate_points();

let width = map_points.iter().map(|p| p.x).max().unwrap() + 1;
@@ -69,7 +70,7 @@ fn score_small(task: Task, output_len: usize) -> ScoreInfo {
width: width as usize,
height: height as usize,
best_estimated: remaining * 3 / 5,
team_time: output_len,
team_time: commands.len(),
}
}

@@ -95,14 +96,14 @@ fn main() {

let mut sum_scores = 0.0;
for input in inputs {
let output_len = {
let commands = {
let output_path = format!("{}/{}", output_root, input.output_file_name());
let mut output_file = File::open(&output_path).unwrap();
let mut output_str = String::new();
output_file.read_to_string(&mut output_str).unwrap();
output_str.trim_end().len()
read_commands(&output_str)
};
let score_info = score_small(input.task, output_len);
let score_info = score_small(input.task, commands);
eprintln!("{}: {}", input.id, score_info.debug());
sum_scores += score_info.score();
}
@@ -9,7 +9,7 @@ use icfpc::parse::read_all_inputs;
use icfpc::solve::solve_small_while;

fn solve<W: Write>(task: Task, f: &mut W) {
let cmds = solve_small_while(task, std::time::Duration::from_secs(2));
let cmds = solve_small_while(task, std::time::Duration::from_millis(100));
write!(f, "{}", cmds).unwrap();
}

@@ -1,8 +1,6 @@
use jsonrpc_client_http::{HttpHandle, HttpTransport};
use rand::prelude::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::{HashMap, HashSet, VecDeque};
use std::{thread, time};

use crate::models::*;
@@ -36,7 +36,7 @@ impl Point {
Move::MoveDown => Point::new(x, y - 1),
Move::MoveRight => Point::new(x + 1, y),
Move::MoveLeft => Point::new(x - 1, y),
_ => self,
Move::Noop => Point::new(x, y),
}
}

@@ -47,7 +47,7 @@ impl Point {
Move::MoveDown => Point::new(x, y + 1),
Move::MoveRight => Point::new(x - 1, y),
Move::MoveLeft => Point::new(x + 1, y),
_ => unreachable!(),
Move::Noop => Point::new(x, y),
}
}
}
@@ -62,7 +62,15 @@ pub struct Map(pub Vec<Point>);

impl fmt::Display for Map {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.iter().map(|p| format!("{}", p)).collect::<Vec<_>>().join(","))?;
write!(
f,
"{}",
self.0
.iter()
.map(|p| format!("{}", p))
.collect::<Vec<_>>()
.join(",")
)?;
Ok(())
}
}
@@ -129,6 +137,10 @@ impl Map {
pub fn len(&self) -> usize {
self.0.len()
}

pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
@@ -186,13 +198,28 @@ impl fmt::Display for Task {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}#", self.map)?;
write!(f, "{}#", self.initial)?;
write!(f, "{}#", self.obstacles.iter().map(|o| format!("{}", o)).collect::<Vec<_>>().join(";"))?;
write!(f, "{}", self.boosters.iter().map(|b| format!("{}", b)).collect::<Vec<_>>().join(";"))?;
write!(
f,
"{}#",
self.obstacles
.iter()
.map(|o| format!("{}", o))
.collect::<Vec<_>>()
.join(";")
)?;
write!(
f,
"{}",
self.boosters
.iter()
.map(|b| format!("{}", b))
.collect::<Vec<_>>()
.join(";")
)?;
Ok(())
}
}


#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Move {
MoveUp,
@@ -212,6 +239,7 @@ pub enum Command {
Drill,
ResetBeacon,
ShiftBeacon(Point),
Cloning,
}

impl fmt::Display for Command {
@@ -224,11 +252,12 @@ impl fmt::Display for Command {
Command::Move(Move::Noop) => write!(f, "Z"),
Command::TurnRight => write!(f, "E"),
Command::TurnLeft => write!(f, "Q"),
Command::NewHand(p) => write!(f, "B({}, {})", p.x, p.y),
Command::NewHand(p) => write!(f, "B({},{})", p.x, p.y),
Command::FastWheel => write!(f, "F"),
Command::Drill => write!(f, "L"),
Command::ResetBeacon => write!(f, "R"),
Command::ShiftBeacon(p) => write!(f, "T({}, {})", p.x, p.y),
Command::ShiftBeacon(p) => write!(f, "T({},{})", p.x, p.y),
Command::Cloning => write!(f, "C"),
}
}
}
@@ -260,12 +289,26 @@ impl Commands {
pub fn len(&self) -> usize {
self.0[0].len()
}
pub fn is_empty(&self) -> bool {
self.0[0].is_empty()
}
}

impl fmt::Display for Commands {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0.iter().map(|cmds| {
cmds.iter().map(|c| format!("{}", c)).collect::<Vec<_>>().join("")
}).collect::<Vec<_>>().join("#"))
write!(
f,
"{}",
self.0
.iter()
.map(|cmds| {
cmds.iter()
.map(|c| format!("{}", c))
.collect::<Vec<_>>()
.join("")
})
.collect::<Vec<_>>()
.join("#")
)
}
}
@@ -1,6 +1,5 @@
use crate::models::*;
use glob::glob;
use std::convert::TryFrom;
use std::fs::File;
use std::io::Read;
use std::iter::Peekable;
@@ -51,8 +50,28 @@ fn skip_or_empty(iter: &mut Peekable<Chars>, expected: char) {
}
}

fn read_i32(iter: &mut Peekable<Chars>, last: char) -> i32 {
let mut x = 0i32;
let minus = if let Some('-') = iter.peek() {
iter.next();
true
} else {
false
};
loop {
let c = iter.next().unwrap();
if c.is_digit(10) {
x = x * 10 + i32::from(c as u8 - b'0');
} else {
assert!(c == last);
return if minus { -x } else { x };
}
}
}

fn read_usize(iter: &mut Peekable<Chars>, last: char) -> usize {
let mut x = 0usize;
assert!(*iter.peek().unwrap() == '-');
loop {
let c = iter.next().unwrap();
if c.is_digit(10) {
@@ -66,9 +85,9 @@ fn read_usize(iter: &mut Peekable<Chars>, last: char) -> usize {

fn read_point(iter: &mut Peekable<Chars>) -> Point {
skip(iter, '(');
let x = read_usize(iter, ',');
let y = read_usize(iter, ')');
Point::new(i32::try_from(x).unwrap(), i32::try_from(y).unwrap())
let x = read_i32(iter, ',');
let y = read_i32(iter, ')');
Point::new(x, y)
}

fn read_map_internal(mut iter: &mut Peekable<Chars>) -> (Map, char) {
@@ -150,6 +169,44 @@ pub fn read_task(s: &str) -> Task {
}
}

fn read_command_internal(iter: &mut Peekable<Chars>) -> Vec<Command> {
let mut res = Vec::new();
while let Some(c) = iter.next() {
let cmd = match c {
'W' => Command::Move(Move::MoveUp),
'S' => Command::Move(Move::MoveDown),
'A' => Command::Move(Move::MoveLeft),
'D' => Command::Move(Move::MoveRight),
'Z' => Command::Move(Move::Noop),
'E' => Command::TurnRight,
'Q' => Command::TurnLeft,
'B' => {
let p = read_point(iter);
Command::NewHand(p)
}
'F' => Command::FastWheel,
'L' => Command::Drill,
'R' => Command::ResetBeacon,
'T' => {
let p = read_point(iter);
Command::ShiftBeacon(p)
}
'C' => Command::Cloning,
_ => unreachable!(),
};
res.push(cmd);
}
res
}

fn read_command(s: &str) -> Vec<Command> {
read_command_internal(&mut s.chars().peekable())
}

pub fn read_commands(s: &str) -> Commands {
Commands::new(s.split('#').map(|s| read_command(&s)).collect::<Vec<_>>())
}

fn find_files(input_root: &str) -> Vec<String> {
glob(&format!("{}/prob-*.desc", input_root))
.expect("glob pattern")

0 comments on commit 25abfbc

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