d5
This commit is contained in:
62
day5/src/main.rs
Normal file
62
day5/src/main.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
fn main() {
|
||||
let (mut stacks, moves) = read_input(&env::args().nth(1).unwrap());
|
||||
println!("stack = {:?}", stacks);
|
||||
println!("moves = {:?}", moves);
|
||||
|
||||
for (n, from, to) in moves {
|
||||
let mut crane: Vec<char> = Vec::new();
|
||||
for _i in 1..=n {
|
||||
crane.push(stacks[from].pop_front().unwrap());
|
||||
}
|
||||
for _i in 1..=n {
|
||||
stacks[to].push_front(crane.pop().unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
let res: String = stacks.iter()
|
||||
.map(|stack| stack.front().unwrap())
|
||||
.collect();
|
||||
println!("res = {:?}", res);
|
||||
}
|
||||
|
||||
fn read_input(file: &str) -> (Vec<VecDeque<char>>, Vec<(usize, usize, usize)>) {
|
||||
let input = fs::read_to_string(file).unwrap();
|
||||
|
||||
let n_stacks = (input.lines().next().unwrap().len() + 1) / 4;
|
||||
let mut stacks: Vec<VecDeque<char>> = vec![VecDeque::new(); n_stacks];
|
||||
let mut moves: Vec<(usize, usize, usize)> = Vec::new();
|
||||
let mut reading_crates = true;
|
||||
|
||||
for line in input.lines() {
|
||||
if line.is_empty() || (reading_crates && has_digit(line)) {
|
||||
reading_crates = false;
|
||||
continue;
|
||||
|
||||
} else if reading_crates {
|
||||
for (i, c) in line.char_indices() {
|
||||
if i % 4 == 1 && c != ' '{
|
||||
stacks[i/4].push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
let nums: Vec<usize> = line
|
||||
.split(|c: char| !c.is_numeric())
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|num| num.parse::<usize>().unwrap())
|
||||
.collect();
|
||||
moves.push((nums[0], nums[1] - 1, nums[2] - 1));
|
||||
|
||||
}
|
||||
}
|
||||
(stacks, moves)
|
||||
}
|
||||
|
||||
fn has_digit(line: &str) -> bool {
|
||||
!line.chars().filter(|c| c.is_digit(10)).collect::<Vec<char>>().is_empty()
|
||||
}
|
||||
|
59
day5/src/main1.rs
Normal file
59
day5/src/main1.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
fn main() {
|
||||
let (mut stacks, moves) = read_input(&env::args().nth(1).unwrap());
|
||||
println!("stack = {:?}", stacks);
|
||||
println!("moves = {:?}", moves);
|
||||
|
||||
for (n, from, to) in moves {
|
||||
for _i in 1..=n {
|
||||
let item = stacks[from].pop_front().unwrap();
|
||||
stacks[to].push_front(item);
|
||||
}
|
||||
}
|
||||
|
||||
let res: String = stacks.iter()
|
||||
.map(|stack| stack.front().unwrap())
|
||||
.collect();
|
||||
println!("res = {:?}", res);
|
||||
}
|
||||
|
||||
fn read_input(file: &str) -> (Vec<VecDeque<char>>, Vec<(usize, usize, usize)>) {
|
||||
let input = fs::read_to_string(file).unwrap();
|
||||
|
||||
let n_stacks = (input.lines().next().unwrap().len() + 1) / 4;
|
||||
let mut stacks: Vec<VecDeque<char>> = vec![VecDeque::new(); n_stacks];
|
||||
let mut moves: Vec<(usize, usize, usize)> = Vec::new();
|
||||
let mut reading_crates = true;
|
||||
|
||||
for line in input.lines() {
|
||||
if line.is_empty() || (reading_crates && has_digit(line)) {
|
||||
reading_crates = false;
|
||||
continue;
|
||||
|
||||
} else if reading_crates {
|
||||
for (i, c) in line.char_indices() {
|
||||
if i % 4 == 1 && c != ' '{
|
||||
stacks[i/4].push_back(c);
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
let nums: Vec<usize> = line
|
||||
.split(|c: char| !c.is_numeric())
|
||||
.filter(|s| !s.is_empty())
|
||||
.map(|num| num.parse::<usize>().unwrap())
|
||||
.collect();
|
||||
moves.push((nums[0], nums[1] - 1, nums[2] - 1));
|
||||
|
||||
}
|
||||
}
|
||||
(stacks, moves)
|
||||
}
|
||||
|
||||
fn has_digit(line: &str) -> bool {
|
||||
!line.chars().filter(|c| c.is_digit(10)).collect::<Vec<char>>().is_empty()
|
||||
}
|
||||
|
Reference in New Issue
Block a user