Last active
December 4, 2015 17:19
-
-
Save shssoichiro/dbf5211e57eebf3664e2 to your computer and use it in GitHub Desktop.
Daily Challenge 243 Hard (Part 1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate petgraph; | |
use std::env; | |
use std::fs::File; | |
use std::io::prelude::*; | |
use std::io::BufReader; | |
use std::collections::HashMap; | |
use petgraph::graph; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
let filename = args[1].clone(); | |
let file = File::open(filename).ok().expect("File not found"); | |
let mut reader = BufReader::new(file); | |
let mut buffer = String::new(); | |
let mut input_graph: Vec<String> = vec![]; | |
let mut graph = graph::Graph::<char, u8>::new(); | |
let mut nodes: HashMap<char, graph::NodeIndex> = HashMap::new(); | |
let mut line_index = 0; | |
let mut starting_point: char; | |
while reader.read_line(&mut buffer).unwrap() > 0 { | |
match line_index { | |
0 => { | |
// We don't need the expected size | |
}, | |
1 => { // Read the starting point | |
starting_point = buffer.chars().next().unwrap(); | |
}, | |
_ => { // Put the graph in a vector of strings | |
input_graph.push(buffer.clone()); | |
} | |
}; | |
line_index += 1; | |
buffer.clear(); | |
} | |
// Iterate once to get the nodes | |
line_index = 0; | |
for line in input_graph.iter() { | |
if line_index % 2 == 0 { // This line contains nodes and edges | |
let mut i = 0; | |
for graph_char in line.split_whitespace() { | |
if i % 2 == 0 { // node | |
let node = graph_char.chars().next().unwrap(); | |
nodes.insert(node, graph.add_node(node)); | |
} | |
i += 1; | |
} | |
}; | |
line_index += 1; | |
} | |
// Iterate again to parse the edges | |
line_index = 0; | |
for line in input_graph.iter() { | |
let mut i = 0; | |
let mut cur_line: Vec<char> = vec![]; | |
let mut up_line: Vec<char> = vec![]; | |
let mut down_line: Vec<char> = vec![]; | |
if line_index % 2 == 0 { | |
cur_line = line.split_whitespace().map(|x| x.chars().next().unwrap()).collect(); | |
} else { | |
let mut j = 0; | |
up_line = input_graph[line_index-1].split_whitespace().filter(|_| {j += 1; j % 2 == 1}).map(|x| x.chars().next().unwrap()).collect(); | |
j = 0; | |
down_line = input_graph[line_index+1].split_whitespace().filter(|_| {j += 1; j % 2 == 1}).map(|x| x.chars().next().unwrap()).collect(); | |
} | |
for graph_char in line.split_whitespace() { | |
if line_index % 2 == 0 { // This line contains nodes and edges | |
if i % 2 == 1 { // edge | |
let a = *nodes.get(&cur_line[i-1]).unwrap(); | |
let b = *nodes.get(&cur_line[i+1]).unwrap(); | |
match graph_char { | |
"-" => { | |
graph.add_edge(a, b, 1); | |
graph.add_edge(b, a, 1); | |
}, | |
">" => { | |
graph.add_edge(a, b, 1); | |
}, | |
"<" => { | |
graph.add_edge(b, a, 1); | |
}, | |
_ => panic!("Invalid character found when creating edges") | |
}; | |
} | |
} else { // This line contains only edges | |
let a = *nodes.get(&up_line[i]).unwrap(); | |
let b = *nodes.get(&down_line[i]).unwrap(); | |
match graph_char { | |
"|" => { | |
graph.add_edge(a, b, 1); | |
graph.add_edge(b, a, 1); | |
}, | |
"v" => { | |
graph.add_edge(a, b, 1); | |
}, | |
"^" => { | |
graph.add_edge(b, a, 1); | |
}, | |
_ => panic!("Invalid character found when creating edges") | |
}; | |
} | |
i += 1; | |
}; | |
line_index += 1; | |
} | |
println!("{:?}", graph); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment