|
@@ -58,28 +58,41 @@ fn parse_reveal(s: &str) -> Reveal {
|
|
|
reveal
|
|
reveal
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-// fn parse_reveals(line: &str) -> Vec<Reveal> {}
|
|
|
|
|
|
|
+fn parse_reveals(s: &str) -> Vec<Reveal> {
|
|
|
|
|
+ // example: "1 green, 4 blue; 1 blue, 2 green, 1 red; 1 red, 1 green, 2 blue; 1 green, 1 red; 1 green; 1 green, 1 blue, 1 red"
|
|
|
|
|
+
|
|
|
|
|
+ let tokens: Vec<&str> = s.split("; ").collect();
|
|
|
|
|
+
|
|
|
|
|
+ let mut reveals: Vec<Reveal> = Vec::new();
|
|
|
|
|
+
|
|
|
|
|
+ for t in tokens {
|
|
|
|
|
+ let reveal: Reveal = parse_reveal(t);
|
|
|
|
|
+ reveals.push(reveal);
|
|
|
|
|
+ }
|
|
|
|
|
+ reveals
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
// fn parse_game(line: &str) -> Game {
|
|
// fn parse_game(line: &str) -> Game {
|
|
|
-// let tokens: Vec<&str> = line.split(":").collect();
|
|
|
|
|
-// let id_str: &str = tokens[0];
|
|
|
|
|
-// let id: i32 = id_str.parse::<i32>().unwrap();
|
|
|
|
|
-
|
|
|
|
|
-// let reveal: Reveal = Reveal {
|
|
|
|
|
-// red: 3,
|
|
|
|
|
-// green: 3,
|
|
|
|
|
-// blue: 3,
|
|
|
|
|
-// };
|
|
|
|
|
-
|
|
|
|
|
-// let mut reveals: Vec<Reveal> = Vec::new();
|
|
|
|
|
-// reveals.push(reveal);
|
|
|
|
|
-
|
|
|
|
|
-// let game: Game = Game {
|
|
|
|
|
-// id: id,
|
|
|
|
|
-// reveals: reveals,
|
|
|
|
|
-// };
|
|
|
|
|
-// game
|
|
|
|
|
-// }
|
|
|
|
|
|
|
+fn parse_game(line: &str) {
|
|
|
|
|
+ // let tokens: Vec<&str> = line.split(":").collect();
|
|
|
|
|
+ // let id_str: &str = tokens[0];
|
|
|
|
|
+ // let id: i32 = id_str.parse::<i32>().unwrap();
|
|
|
|
|
+
|
|
|
|
|
+ // let reveal: Reveal = Reveal {
|
|
|
|
|
+ // red: 3,
|
|
|
|
|
+ // green: 3,
|
|
|
|
|
+ // blue: 3,
|
|
|
|
|
+ // };
|
|
|
|
|
+
|
|
|
|
|
+ // let mut reveals: Vec<Reveal> = Vec::new();
|
|
|
|
|
+ // reveals.push(reveal);
|
|
|
|
|
+
|
|
|
|
|
+ // let game: Game = Game {
|
|
|
|
|
+ // id: id,
|
|
|
|
|
+ // reveals: reveals,
|
|
|
|
|
+ // };
|
|
|
|
|
+ // game
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
// fn parse_games(lines: Vec<String>) -> Vec<Game> {
|
|
// fn parse_games(lines: Vec<String>) -> Vec<Game> {
|
|
|
// let mut games: Vec<Game> = Vec::new();
|
|
// let mut games: Vec<Game> = Vec::new();
|
|
@@ -105,7 +118,7 @@ fn main() {
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
#[cfg(test)]
|
|
|
mod tests {
|
|
mod tests {
|
|
|
- use crate::parse_reveal;
|
|
|
|
|
|
|
+ use crate::{parse_game, parse_reveal, parse_reveals};
|
|
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
|
fn test_parse_reveal_1() {
|
|
fn test_parse_reveal_1() {
|
|
@@ -120,6 +133,7 @@ mod tests {
|
|
|
assert_eq!(reveal.blue, 4);
|
|
assert_eq!(reveal.blue, 4);
|
|
|
assert_eq!(reveal.red, 0);
|
|
assert_eq!(reveal.red, 0);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
#[test]
|
|
#[test]
|
|
|
fn test_parse_reveal_2() {
|
|
fn test_parse_reveal_2() {
|
|
|
// arrange
|
|
// arrange
|
|
@@ -133,6 +147,7 @@ mod tests {
|
|
|
assert_eq!(reveal.blue, 1);
|
|
assert_eq!(reveal.blue, 1);
|
|
|
assert_eq!(reveal.red, 15);
|
|
assert_eq!(reveal.red, 15);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
#[test]
|
|
#[test]
|
|
|
fn test_parse_reveal_3() {
|
|
fn test_parse_reveal_3() {
|
|
|
// arrange
|
|
// arrange
|
|
@@ -146,4 +161,31 @@ mod tests {
|
|
|
assert_eq!(reveal.blue, 0);
|
|
assert_eq!(reveal.blue, 0);
|
|
|
assert_eq!(reveal.red, 0);
|
|
assert_eq!(reveal.red, 0);
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ #[test]
|
|
|
|
|
+ fn test_parse_reveals() {
|
|
|
|
|
+ // arrange
|
|
|
|
|
+ let s: &str = "1 green, 4 blue; 1 blue, 2 green, 1 red";
|
|
|
|
|
+
|
|
|
|
|
+ // act
|
|
|
|
|
+ let reveals = parse_reveals(s);
|
|
|
|
|
+
|
|
|
|
|
+ // assert
|
|
|
|
|
+ assert_eq!(reveals.len(), 2);
|
|
|
|
|
+ assert_eq!(reveals[0].green, 1);
|
|
|
|
|
+ assert_eq!(reveals[0].blue, 4);
|
|
|
|
|
+ assert_eq!(reveals[1].blue, 1);
|
|
|
|
|
+ assert_eq!(reveals[1].green, 2);
|
|
|
|
|
+ assert_eq!(reveals[1].red, 1);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // #[test]
|
|
|
|
|
+ // fn test_parse_game(){
|
|
|
|
|
+ // //arrange
|
|
|
|
|
+ // let s: &str = ""
|
|
|
|
|
+
|
|
|
|
|
+ // //act
|
|
|
|
|
+
|
|
|
|
|
+ // //assert
|
|
|
|
|
+ // }
|
|
|
}
|
|
}
|