Skip to content

Commit 96b2c33

Browse files
committed
Update
1 parent c89b9a2 commit 96b2c33

File tree

4 files changed

+98
-15
lines changed

4 files changed

+98
-15
lines changed

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jsonrpc-client-core = "0.5.0"
1414
jsonrpc-client-http = "0.5.0"
1515
serde = {version = "1.0.92", features = ["derive"]}
1616
serde_json = "1.0.39"
17+
chrono = "0.4.6"
1718

1819
[lib]
1920
name = "icfpc"

src/bin/stats.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use icfpc::mine::Client;
2+
use std::collections::HashMap;
3+
4+
fn main() {
5+
let mut client = Client::new();
6+
let team_id = 42;
7+
let latest_block = client.latest_block().unwrap();
8+
let mut last_balances = HashMap::new();
9+
for b in 33..=latest_block {
10+
let info = client.get_block_info(b).unwrap();
11+
let mut increses = Vec::new();
12+
let my_last = last_balances.get(&team_id).unwrap_or(&0);
13+
let my_increase = info.balances.get(&team_id).unwrap_or(my_last) - my_last;
14+
for (&k, &v) in &info.balances {
15+
increses.push((k, v - last_balances.get(&k).unwrap_or(&0)));
16+
last_balances.insert(k, v);
17+
}
18+
increses.sort_by_key(|(_, v)| *v);
19+
increses.reverse();
20+
let rank = increses
21+
.iter()
22+
.enumerate()
23+
.find(|(_, (k, _))| *k == team_id)
24+
.map(|(r, _)| r + 1)
25+
.unwrap_or(0);
26+
println!("{}({}): +{} (rank {})", b, info.time(), my_increase, rank);
27+
for (k, v) in increses.iter().take(3) {
28+
println!("\t{}: {}", k, v);
29+
}
30+
}
31+
}

src/mine.rs

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,44 @@
11
use jsonrpc_client_http::{HttpHandle, HttpTransport};
22
use serde::{Deserialize, Serialize};
33
use serde_json::Value;
4-
use std::{thread, time};
4+
55

66
use crate::models::*;
77
use crate::parse::{read_puzzle, read_task};
88
use crate::puzzle::solve_puzzle;
99
use crate::solve::solve_small_while;
10-
10+
use chrono::prelude::*;
11+
use std::collections::HashMap;
12+
use std::{thread, time};
1113
#[derive(Debug, Serialize, Deserialize)]
1214
pub struct BlockChainInfo {
13-
block: usize,
14-
block_subs: usize,
15-
block_ts: f64,
16-
total_subs: usize,
15+
pub block: usize,
16+
pub block_subs: usize,
17+
pub block_ts: f64,
18+
pub total_subs: usize,
1719
}
1820

1921
#[derive(Debug, Serialize, Deserialize)]
2022
pub struct MiningInfo {
21-
block: usize,
23+
pub block: usize,
2224
//excluded: Vec<usize>,
23-
puzzle: String,
24-
task: String,
25+
pub puzzle: String,
26+
pub task: String,
2527
}
2628

2729
#[derive(Debug, Serialize, Deserialize)]
2830
pub struct BlockInfo {
29-
block: usize,
30-
puzzle: String,
31-
task: String,
31+
pub block: usize,
32+
pub block_ts: f64,
33+
pub puzzle: String,
34+
pub task: String,
35+
pub balances: HashMap<usize, usize>,
36+
}
37+
38+
impl BlockInfo {
39+
pub fn time(&self) -> DateTime<Local> {
40+
Local.timestamp(self.block_ts as i64, 0)
41+
}
3242
}
3343

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

4151
pub struct Client {
4252
api: LambdaClient<HttpHandle>,
53+
last_block: usize,
4354
}
4455

4556
impl Default for Client {
@@ -53,7 +64,10 @@ impl Client {
5364
let transport = HttpTransport::new().standalone().unwrap();
5465
let transport_handle = transport.handle("http://localhost:8332").unwrap();
5566
let client = LambdaClient::new(transport_handle);
56-
Client { api: client }
67+
Client {
68+
api: client,
69+
last_block: 0,
70+
}
5771
}
5872

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

83+
pub fn get_block_info(&mut self, bucket: usize) -> Option<BlockInfo> {
84+
match self.api.getblockinfo(bucket).call() {
85+
Ok(m) => Some(m),
86+
Err(e) => {
87+
eprintln!("{}", e);
88+
None
89+
}
90+
}
91+
}
92+
6993
pub fn submit_latest(&mut self) {
7094
if let Some(block) = self.latest_block() {
95+
if block == self.last_block {
96+
return;
97+
}
98+
eprintln!("Start {}", block);
99+
self.last_block = block;
71100
if self.generate_solution(block) {
72101
match self
73102
.api
@@ -96,7 +125,7 @@ impl Client {
96125

97126
let puzzle = read_puzzle(&blockinfo.puzzle);
98127
let task = read_task(&blockinfo.task);
99-
let task_answer = solve_small_while(task, std::time::Duration::from_secs(180));
128+
let task_answer = solve_small_while(task, std::time::Duration::from_secs(300));
100129
let puzzle_answer = solve_puzzle(puzzle);
101130

102131
self.dump_task_answer(block, task_answer);
@@ -135,7 +164,7 @@ impl Client {
135164
pub fn execute(&mut self) {
136165
loop {
137166
self.submit_latest();
138-
thread::sleep(time::Duration::from_secs(60));
167+
thread::sleep(time::Duration::from_secs(10));
139168
}
140169
}
141170
}

0 commit comments

Comments
 (0)