Skip to content

Instantly share code, notes, and snippets.

Created April 13, 2014 20:54
Show Gist options
  • Save anonymous/10601934 to your computer and use it in GitHub Desktop.
Save anonymous/10601934 to your computer and use it in GitHub Desktop.
use std::io::{BufferedReader, File};
/*
A-practice.in contains (from the Google CodeJam 2014 Qualification round, Problem A:
3
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
1 2 5 4
3 11 6 15
9 10 7 12
13 14 8 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
2
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
3
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
*/
fn main() {
let inputFilePath: Path = Path::new("A-practice.in");
let linesOwned = read_lines_owned(&inputFilePath);
let linesBorrowed = read_lines_borrowed(&inputFilePath);
let mut reader = BufferedReader::new(File::open(&inputFilePath));
let rawLocalOwned: ~[~str] = reader.lines().map(|l| l.unwrap()).collect();
let localOwned: ~[~str] = rawLocalOwned.iter().map(|l| l.trim().to_owned()).collect();
reader = BufferedReader::new(File::open(&inputFilePath));
let rawLocalBorrowed: ~[~str] = reader.lines().map(|l| l.unwrap()).collect();
let localBorrowed: ~[&str] = rawLocalBorrowed.iter().map(|l| l.trim()).collect();
println!("\nlinesOwned:\n{}", linesOwned);
println!("\nlinesBorrowed:\n{}", linesBorrowed); //Output is garbled here
println!("\nlocalOwned:\n{}", localOwned);
println!("\nlocalBorrowed:\n{}", localBorrowed);
}
fn read_lines_owned(inputFilePath: &Path) -> ~[~str] {
let mut reader = BufferedReader::new(File::open(inputFilePath));
let rawLines: ~[~str] = reader.lines().map(|l| l.unwrap()).collect();
let lines = rawLines.iter().map(|l| l.trim().to_owned()).collect();
println!("\nread_lines_owned:\n{}", lines);
lines
}
fn read_lines_borrowed(inputFilePath: &Path) -> ~[&str] {
let mut reader = BufferedReader::new(File::open(inputFilePath));
let rawLines: ~[~str] = reader.lines().map(|l| l.unwrap()).collect();
let lines = rawLines.iter().map(|l| l.trim()).collect();
println!("\nread_lines_borrowed:\n{}", lines);
lines
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment