2014-04-21 16:06:59 -05:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 19:32:48 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-05-11 13:14:14 -05:00
|
|
|
// no-pretty-expanded
|
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
#![feature(phase)]
|
|
|
|
#[phase(syntax)] extern crate green;
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-04-02 18:54:22 -05:00
|
|
|
use std::strbuf::StrBuf;
|
2014-04-21 16:06:59 -05:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
green_start!(main)
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
fn print_complements() {
|
2013-06-17 15:37:11 -05:00
|
|
|
let all = [Blue, Red, Yellow];
|
2013-08-03 11:45:23 -05:00
|
|
|
for aa in all.iter() {
|
|
|
|
for bb in all.iter() {
|
2014-04-21 16:06:59 -05:00
|
|
|
println!("{} + {} -> {}", *aa, *bb, transform(*aa, *bb));
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
enum Color { Red, Yellow, Blue }
|
|
|
|
impl fmt::Show for Color {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let str = match *self {
|
|
|
|
Red => "red",
|
|
|
|
Yellow => "yellow",
|
|
|
|
Blue => "blue",
|
|
|
|
};
|
2014-05-11 13:14:14 -05:00
|
|
|
write!(f, "{}", str)
|
2014-04-21 16:06:59 -05:00
|
|
|
}
|
|
|
|
}
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2013-01-28 20:55:44 -06:00
|
|
|
struct CreatureInfo {
|
|
|
|
name: uint,
|
2014-04-21 16:06:59 -05:00
|
|
|
color: Color
|
2013-01-28 20:55:44 -06:00
|
|
|
}
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
fn show_color_list(set: Vec<Color>) -> StrBuf {
|
2014-04-02 18:54:22 -05:00
|
|
|
let mut out = StrBuf::new();
|
2013-08-03 11:45:23 -05:00
|
|
|
for col in set.iter() {
|
2013-06-11 21:13:42 -05:00
|
|
|
out.push_char(' ');
|
2014-04-21 16:06:59 -05:00
|
|
|
out.push_str(col.to_str());
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
2014-04-10 05:55:34 -05:00
|
|
|
out
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
2014-04-10 05:55:34 -05:00
|
|
|
fn show_digit(nn: uint) -> &'static str {
|
2014-01-19 02:21:14 -06:00
|
|
|
match nn {
|
2014-04-21 16:06:59 -05:00
|
|
|
0 => {" zero"}
|
|
|
|
1 => {" one"}
|
|
|
|
2 => {" two"}
|
|
|
|
3 => {" three"}
|
|
|
|
4 => {" four"}
|
|
|
|
5 => {" five"}
|
|
|
|
6 => {" six"}
|
|
|
|
7 => {" seven"}
|
|
|
|
8 => {" eight"}
|
|
|
|
9 => {" nine"}
|
2013-10-21 15:08:31 -05:00
|
|
|
_ => {fail!("expected digits from 0 to 9...")}
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
struct Number(uint);
|
|
|
|
impl fmt::Show for Number {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let mut out = vec![];
|
|
|
|
let Number(mut num) = *self;
|
|
|
|
if num == 0 { out.push(show_digit(0)) };
|
|
|
|
|
|
|
|
while num != 0 {
|
|
|
|
let dig = num % 10;
|
|
|
|
num = num / 10;
|
|
|
|
let s = show_digit(dig);
|
|
|
|
out.push(s);
|
|
|
|
}
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
for s in out.iter().rev() {
|
2014-05-11 13:14:14 -05:00
|
|
|
try!(write!(f, "{}", s))
|
2014-04-21 16:06:59 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
2014-04-10 05:55:34 -05:00
|
|
|
}
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
fn transform(aa: Color, bb: Color) -> Color {
|
2012-08-06 14:34:08 -05:00
|
|
|
match (aa, bb) {
|
2012-08-03 21:59:04 -05:00
|
|
|
(Red, Red ) => { Red }
|
|
|
|
(Red, Yellow) => { Blue }
|
|
|
|
(Red, Blue ) => { Yellow }
|
|
|
|
(Yellow, Red ) => { Blue }
|
|
|
|
(Yellow, Yellow) => { Yellow }
|
|
|
|
(Yellow, Blue ) => { Red }
|
|
|
|
(Blue, Red ) => { Yellow }
|
|
|
|
(Blue, Yellow) => { Red }
|
|
|
|
(Blue, Blue ) => { Blue }
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn creature(
|
|
|
|
name: uint,
|
2014-04-21 16:06:59 -05:00
|
|
|
mut color: Color,
|
|
|
|
from_rendezvous: Receiver<CreatureInfo>,
|
2014-03-09 16:58:32 -05:00
|
|
|
to_rendezvous: Sender<CreatureInfo>,
|
2014-05-12 19:56:43 -05:00
|
|
|
to_rendezvous_log: Sender<StrBuf>
|
2012-07-06 20:20:36 -05:00
|
|
|
) {
|
|
|
|
let mut creatures_met = 0;
|
|
|
|
let mut evil_clones_met = 0;
|
2014-04-21 16:06:59 -05:00
|
|
|
let mut rendezvous = from_rendezvous.iter();
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
loop {
|
|
|
|
// ask for a pairing
|
2013-01-29 01:54:39 -06:00
|
|
|
to_rendezvous.send(CreatureInfo {name: name, color: color});
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
// log and change, or quit
|
|
|
|
match rendezvous.next() {
|
|
|
|
Some(other_creature) => {
|
2012-07-06 20:20:36 -05:00
|
|
|
color = transform(color, other_creature.color);
|
|
|
|
|
|
|
|
// track some statistics
|
|
|
|
creatures_met += 1;
|
|
|
|
if other_creature.name == name {
|
|
|
|
evil_clones_met += 1;
|
|
|
|
}
|
|
|
|
}
|
2014-04-21 16:06:59 -05:00
|
|
|
None => break
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
}
|
2014-04-21 16:06:59 -05:00
|
|
|
// log creatures met and evil clones of self
|
2014-05-12 19:56:43 -05:00
|
|
|
let report = format_strbuf!("{}{}",
|
|
|
|
creatures_met,
|
|
|
|
Number(evil_clones_met));
|
2014-04-21 16:06:59 -05:00
|
|
|
to_rendezvous_log.send(report);
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
fn rendezvous(nn: uint, set: Vec<Color>) {
|
2012-07-07 23:43:12 -05:00
|
|
|
// these ports will allow us to hear from the creatures
|
2014-03-09 16:58:32 -05:00
|
|
|
let (to_rendezvous, from_creatures) = channel::<CreatureInfo>();
|
2012-07-07 23:43:12 -05:00
|
|
|
|
|
|
|
// these channels will be passed to the creatures so they can talk to us
|
2014-05-12 19:56:43 -05:00
|
|
|
let (to_rendezvous_log, from_creatures_log) = channel::<StrBuf>();
|
2012-07-07 23:43:12 -05:00
|
|
|
|
|
|
|
// these channels will allow us to talk to each creature by 'name'/index
|
2014-04-21 16:06:59 -05:00
|
|
|
let mut to_creature: Vec<Sender<CreatureInfo>> =
|
|
|
|
set.iter().enumerate().map(|(ii, &col)| {
|
2012-09-21 20:43:30 -05:00
|
|
|
// create each creature as a listener with a port, and
|
|
|
|
// give us a channel to talk to each
|
2013-01-29 01:54:39 -06:00
|
|
|
let to_rendezvous = to_rendezvous.clone();
|
|
|
|
let to_rendezvous_log = to_rendezvous_log.clone();
|
2014-03-09 16:58:32 -05:00
|
|
|
let (to_creature, from_rendezvous) = channel();
|
2014-04-21 16:06:59 -05:00
|
|
|
spawn(proc() {
|
2013-12-03 18:44:16 -06:00
|
|
|
creature(ii,
|
|
|
|
col,
|
|
|
|
from_rendezvous,
|
2014-04-21 16:06:59 -05:00
|
|
|
to_rendezvous,
|
|
|
|
to_rendezvous_log);
|
2014-01-27 17:29:50 -06:00
|
|
|
});
|
2013-01-29 01:54:39 -06:00
|
|
|
to_creature
|
2013-06-29 00:05:50 -05:00
|
|
|
}).collect();
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
let mut creatures_met = 0;
|
|
|
|
|
|
|
|
// set up meetings...
|
2013-08-05 22:43:06 -05:00
|
|
|
for _ in range(0, nn) {
|
2014-04-21 16:06:59 -05:00
|
|
|
let fst_creature = from_creatures.recv();
|
|
|
|
let snd_creature = from_creatures.recv();
|
2012-07-07 23:43:12 -05:00
|
|
|
|
|
|
|
creatures_met += 2;
|
|
|
|
|
2014-04-21 16:06:59 -05:00
|
|
|
to_creature.get_mut(fst_creature.name).send(snd_creature);
|
|
|
|
to_creature.get_mut(snd_creature.name).send(fst_creature);
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// tell each creature to stop
|
2014-04-21 16:06:59 -05:00
|
|
|
drop(to_creature);
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
// print each color in the set
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("{}", show_color_list(set));
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
// print each creature's stats
|
2014-04-21 16:06:59 -05:00
|
|
|
drop(to_rendezvous_log);
|
|
|
|
for rep in from_creatures_log.iter() {
|
|
|
|
println!("{}", rep);
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// print the total number of creatures met
|
2014-04-21 16:06:59 -05:00
|
|
|
println!("{}\n", Number(creatures_met));
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|
|
|
|
|
2012-10-03 21:16:27 -05:00
|
|
|
fn main() {
|
2014-04-21 16:06:59 -05:00
|
|
|
let nn = if std::os::getenv("RUST_BENCH").is_some() {
|
|
|
|
200000
|
2012-07-06 20:20:36 -05:00
|
|
|
} else {
|
2014-05-05 02:29:59 -05:00
|
|
|
std::os::args().as_slice().get(1).and_then(|arg| from_str(*arg)).unwrap_or(600)
|
2012-07-06 20:20:36 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
print_complements();
|
2014-01-09 04:06:55 -06:00
|
|
|
println!("");
|
2012-07-06 20:20:36 -05:00
|
|
|
|
2014-03-05 16:02:44 -06:00
|
|
|
rendezvous(nn, vec!(Blue, Red, Yellow));
|
2012-07-06 20:20:36 -05:00
|
|
|
|
|
|
|
rendezvous(nn,
|
2014-03-05 16:02:44 -06:00
|
|
|
vec!(Blue, Red, Yellow, Red, Yellow, Blue, Red, Yellow, Red, Blue));
|
2012-07-06 20:20:36 -05:00
|
|
|
}
|