Skip to content

Instantly share code, notes, and snippets.

@pdonald
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pdonald/85a0877d691e48de01bb to your computer and use it in GitHub Desktop.
Save pdonald/85a0877d691e48de01bb to your computer and use it in GitHub Desktop.
use std::char;
use std::rand::{Rng, task_rng};
// identisks tam pašam, kas C#
fn main() {
let mut people: Vec<String> = range(0, 15).map(|i| char::from_u32(('A' as u32) + i).unwrap().to_string()).collect();
let blacklist: Vec<Vec<&str>> = "A/B B/C D/E".split(' ').map(|pair| pair.split('/').collect()).collect();
task_rng().shuffle(people.as_mut_slice());
while people.len() > 0 {
let giver = people.pop().unwrap();
let receiver = people.iter().position(|candidate|
!blacklist.iter().any(|ref pair| *pair[0] == **candidate && *pair[1] == *giver) &&
!blacklist.iter().any(|ref pair| *pair[1] == **candidate && *pair[0] == *giver));
match receiver {
Some(index) => {
println!("{} dāvina dāvanu {}", giver, people.index(&index));
people.remove(index);
},
None => {
println!("{} nav pārīša", giver);
}
}
}
}
use std::io;
use std::collections::HashSet;
use std::rand::{Rng, task_rng};
// tas pats primitīvais algoritms, kas iepriekš, bet ātrs
// kā arī cilvēki un pārīši (un ģimenes) tiek ņemtas no vieniem ievaddatiem
fn main() {
// ievaddati:
// A (viens cilvēks)
// B C (pārītis)
// D E F (ģimene - pārīši D E, D F un E F)
let lines: Vec<String> = io::stdin().lines().map(|line| line.unwrap()).collect();
let mut people = HashSet::new();
let mut blacklist = HashSet::new();
for line in lines.iter() {
let group: Vec<&str> = line.trim().split(' ').collect();
for i in range(0, group.len()) {
people.insert(group[i]);
for j in range(i + 1, group.len()) {
people.insert(group[j]);
blacklist.insert((group[i], group[j]));
}
}
}
let mut people: Vec<&str> = people.into_iter().collect();
task_rng().shuffle(people.as_mut_slice());
while !people.is_empty() {
let giver = people.pop().unwrap();
let receiver = people.iter().position(|&candidate|
!blacklist.contains(&(giver, candidate)) &&
!blacklist.contains(&(candidate, giver)));
match receiver {
Some(index) => {
println!("{} dāvina dāvanu {}", giver, people.index(&index));
people.swap_remove(index);
},
None => {
println!("{} nav pārīša", giver);
}
}
}
}
// vienu miljonu cilvēku izlozē vienā sekundē:
// seq 1 1000000 > 1m.txt
// time secret_santa2 < 1m.txt > /dev/null
// real 0m0.991s
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
// algoritms tik elementārs, cik vien iespējams, kā arī neefektīvs
// netiek ņemti vērā speciāl/robežgadījumi
class SecretSanta
{
static void Main()
{
// string[] people = File.ReadLines("people.txt");
// string[][] blacklist = File.ReadLines("blacklist.txt").Select(line => line.Split('\t')).ToArray();
string[] people = Enumerable.Range(0, 15).Select(i => ((char)('A' + i)).ToString()).ToArray();
string[][] blacklist = "A/B B/C D/E".Split(' ').Select(pair => pair.Split('/')).ToArray();
Random random = new Random();
List<string> list = people.Distinct().OrderBy(person => random.Next()).ToList();
while (list.Count > 0)
{
string giver = list.First();
string receiver = (from candidate in list
where candidate != giver
where !blacklist.Any(pair => pair[0] == candidate && pair[1] == giver)
where !blacklist.Any(pair => pair[1] == candidate && pair[0] == giver)
select candidate).FirstOrDefault();
if (receiver != null)
Console.WriteLine("{0} dāvina dāvanu {1}", giver, receiver);
else
Console.WriteLine("{0} nav pārīša", giver);
list.Remove(giver);
list.Remove(receiver);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment