diff --git a/examples/day04_1.rs b/examples/day04_1.rs index 197f9b6..51f5c21 100644 --- a/examples/day04_1.rs +++ b/examples/day04_1.rs @@ -1,5 +1,3 @@ -use std::collections::*; - fn main() { let lines: Vec = std::io::stdin().lines().map(|x| x.unwrap()).collect(); let mut result = 0; diff --git a/examples/day04_2.rs b/examples/day04_2.rs index f257a7a..d26e87a 100644 --- a/examples/day04_2.rs +++ b/examples/day04_2.rs @@ -1,5 +1,3 @@ -use std::collections::*; - fn main() { let lines: Vec = std::io::stdin().lines().map(|x| x.unwrap()).collect(); let mut result = 0; diff --git a/examples/day05_1.rs b/examples/day05_1.rs new file mode 100644 index 0000000..47b3143 --- /dev/null +++ b/examples/day05_1.rs @@ -0,0 +1,38 @@ +fn main() { + let mut lines = std::io::stdin().lines(); + let mut crates = Vec::new(); + let mut line = lines.next().unwrap().unwrap(); + let crate_count = line.len()/4 + 1; + for _ in 0..crate_count { + crates.push(vec![]); + } + loop { + if line.chars().nth(1).unwrap().is_numeric() { + break; + } + for i in 0..crate_count { + let c = line.chars().nth(4*i+1).unwrap(); + if c != ' ' { + crates[i].insert(0, c); + } + } + line = lines.next().unwrap().unwrap(); + } + lines.next().unwrap().unwrap(); + while let Some(Ok(line)) = lines.next() { + let mut parts = line.split(" "); + let n: usize = parts.nth(1).unwrap().parse().unwrap(); + let from: usize = parts.nth(1).unwrap().parse().unwrap(); + let to: usize = parts.nth(1).unwrap().parse().unwrap(); + for _ in 0..n { + let c = crates[from-1].pop().unwrap(); + crates[to-1].push(c); + } + } + + for c in crates.iter_mut() { + print!("{}", c.pop().unwrap()); + } + println!(); + +} diff --git a/examples/day05_2.rs b/examples/day05_2.rs new file mode 100644 index 0000000..dc47922 --- /dev/null +++ b/examples/day05_2.rs @@ -0,0 +1,41 @@ +fn main() { + let mut lines = std::io::stdin().lines(); + let mut crates = Vec::new(); + let mut line = lines.next().unwrap().unwrap(); + let crate_count = line.len()/4 + 1; + for _ in 0..crate_count { + crates.push(vec![]); + } + loop { + if line.chars().nth(1).unwrap().is_numeric() { + break; + } + for i in 0..crate_count { + let c = line.chars().nth(4*i+1).unwrap(); + if c != ' ' { + crates[i].insert(0, c); + } + } + line = lines.next().unwrap().unwrap(); + } + lines.next().unwrap().unwrap(); + while let Some(Ok(line)) = lines.next() { + let mut parts = line.split(" "); + let n: usize = parts.nth(1).unwrap().parse().unwrap(); + let from: usize = parts.nth(1).unwrap().parse().unwrap(); + let to: usize = parts.nth(1).unwrap().parse().unwrap(); + let mut crane = Vec::new(); + for _ in 0..n { + crane.push(crates[from-1].pop().unwrap()); + } + for _ in 0..n { + crates[to-1].push(crane.pop().unwrap()); + } + } + + for c in crates.iter_mut() { + print!("{}", c.pop().unwrap()); + } + println!(); + +}