Skip to content
Permalink
Browse files

Update

  • Loading branch information...
ichyo committed Jun 23, 2019
1 parent c89b9a2 commit 96b2c3385cf066fd9e49b196c3779594d744d4d6
Showing with 98 additions and 15 deletions.
  1. +22 −0 Cargo.lock
  2. +1 −0 Cargo.toml
  3. +31 −0 src/bin/stats.rs
  4. +44 −15 src/mine.rs

Some generated files are not rendered by default. Learn more.

@@ -14,6 +14,7 @@ jsonrpc-client-core = "0.5.0"
jsonrpc-client-http = "0.5.0"
serde = {version = "1.0.92", features = ["derive"]}
serde_json = "1.0.39"
chrono = "0.4.6"

[lib]
name = "icfpc"
@@ -0,0 +1,31 @@
use icfpc::mine::Client;
use std::collections::HashMap;

fn main() {
let mut client = Client::new();
let team_id = 42;
let latest_block = client.latest_block().unwrap();
let mut last_balances = HashMap::new();
for b in 33..=latest_block {
let info = client.get_block_info(b).unwrap();
let mut increses = Vec::new();
let my_last = last_balances.get(&team_id).unwrap_or(&0);
let my_increase = info.balances.get(&team_id).unwrap_or(my_last) - my_last;
for (&k, &v) in &info.balances {
increses.push((k, v - last_balances.get(&k).unwrap_or(&0)));
last_balances.insert(k, v);
}
increses.sort_by_key(|(_, v)| *v);
increses.reverse();
let rank = increses
.iter()
.enumerate()
.find(|(_, (k, _))| *k == team_id)
.map(|(r, _)| r + 1)
.unwrap_or(0);
println!("{}({}): +{} (rank {})", b, info.time(), my_increase, rank);
for (k, v) in increses.iter().take(3) {
println!("\t{}: {}", k, v);
}
}
}
@@ -1,34 +1,44 @@
use jsonrpc_client_http::{HttpHandle, HttpTransport};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{thread, time};


use crate::models::*;
use crate::parse::{read_puzzle, read_task};
use crate::puzzle::solve_puzzle;
use crate::solve::solve_small_while;

use chrono::prelude::*;
use std::collections::HashMap;
use std::{thread, time};
#[derive(Debug, Serialize, Deserialize)]
pub struct BlockChainInfo {
block: usize,
block_subs: usize,
block_ts: f64,
total_subs: usize,
pub block: usize,
pub block_subs: usize,
pub block_ts: f64,
pub total_subs: usize,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MiningInfo {
block: usize,
pub block: usize,
//excluded: Vec<usize>,
puzzle: String,
task: String,
pub puzzle: String,
pub task: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct BlockInfo {
block: usize,
puzzle: String,
task: String,
pub block: usize,
pub block_ts: f64,
pub puzzle: String,
pub task: String,
pub balances: HashMap<usize, usize>,
}

impl BlockInfo {
pub fn time(&self) -> DateTime<Local> {
Local.timestamp(self.block_ts as i64, 0)
}
}

jsonrpc_client!(pub struct LambdaClient {
@@ -40,6 +50,7 @@ jsonrpc_client!(pub struct LambdaClient {

pub struct Client {
api: LambdaClient<HttpHandle>,
last_block: usize,
}

impl Default for Client {
@@ -53,7 +64,10 @@ impl Client {
let transport = HttpTransport::new().standalone().unwrap();
let transport_handle = transport.handle("http://localhost:8332").unwrap();
let client = LambdaClient::new(transport_handle);
Client { api: client }
Client {
api: client,
last_block: 0,
}
}

pub fn latest_block(&mut self) -> Option<usize> {
@@ -66,8 +80,23 @@ impl Client {
}
}

pub fn get_block_info(&mut self, bucket: usize) -> Option<BlockInfo> {
match self.api.getblockinfo(bucket).call() {
Ok(m) => Some(m),
Err(e) => {
eprintln!("{}", e);
None
}
}
}

pub fn submit_latest(&mut self) {
if let Some(block) = self.latest_block() {
if block == self.last_block {
return;
}
eprintln!("Start {}", block);
self.last_block = block;
if self.generate_solution(block) {
match self
.api
@@ -96,7 +125,7 @@ impl Client {

let puzzle = read_puzzle(&blockinfo.puzzle);
let task = read_task(&blockinfo.task);
let task_answer = solve_small_while(task, std::time::Duration::from_secs(180));
let task_answer = solve_small_while(task, std::time::Duration::from_secs(300));
let puzzle_answer = solve_puzzle(puzzle);

self.dump_task_answer(block, task_answer);
@@ -135,7 +164,7 @@ impl Client {
pub fn execute(&mut self) {
loop {
self.submit_latest();
thread::sleep(time::Duration::from_secs(60));
thread::sleep(time::Duration::from_secs(10));
}
}
}

0 comments on commit 96b2c33

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