Actually parse the boat from the string describing it now
This commit is contained in:
parent
308ddd0de4
commit
4236ff8a61
|
@ -91,14 +91,17 @@ impl fmt::Display for Boat {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vec<&str>> for Boat {
|
impl FromStr for Boat {
|
||||||
fn from(lines: Vec<&str>) -> Self {
|
type Err = anyhow::Error;
|
||||||
|
|
||||||
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref RE: Regex = Regex::new(r"\[[A-Z]\]").unwrap();
|
static ref RE: Regex = Regex::new(r"\[[A-Z]\]").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut boat = Self{stacks: HashMap::new()};
|
let mut boat = Self{stacks: HashMap::new()};
|
||||||
for line in lines {
|
|
||||||
|
s.lines().rev().for_each(|line| {
|
||||||
for mat in RE.find_iter(line) {
|
for mat in RE.find_iter(line) {
|
||||||
let index = mat.start() / 4 + 1;
|
let index = mat.start() / 4 + 1;
|
||||||
// based on the pattern [.] we get the second char
|
// based on the pattern [.] we get the second char
|
||||||
|
@ -106,14 +109,9 @@ impl From<Vec<&str>> for Boat {
|
||||||
|
|
||||||
boat.push(index, Crate{letter});
|
boat.push(index, Crate{letter});
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
// Because of how we load the data, each stack is upside down
|
Ok(boat)
|
||||||
for stack in boat.stacks.iter_mut() {
|
|
||||||
stack.1.reverse()
|
|
||||||
}
|
|
||||||
|
|
||||||
return boat;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,15 +172,10 @@ impl FromStr for Instruction {
|
||||||
fn solution(input: &str, part1: bool) -> String {
|
fn solution(input: &str, part1: bool) -> String {
|
||||||
let (boat, moves) = input.split_once("\n\n").expect("Input is invalid");
|
let (boat, moves) = input.split_once("\n\n").expect("Input is invalid");
|
||||||
|
|
||||||
// The current layout description ends with an empty line
|
let mut boat: Boat = boat.parse().expect("Invalid input");
|
||||||
let mut boat: Boat = boat.lines()
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.into();
|
|
||||||
|
|
||||||
// Each instruction starts with an 'm'
|
moves.lines()
|
||||||
moves
|
.map(|line| line.parse().expect("Invalid input"))
|
||||||
.lines()
|
|
||||||
.map(|line| line.parse().expect("Input is invalid"))
|
|
||||||
.for_each(|i: Instruction| {
|
.for_each(|i: Instruction| {
|
||||||
let mut taken = boat.take(i.from, i.amount);
|
let mut taken = boat.take(i.from, i.amount);
|
||||||
if part1 {
|
if part1 {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user