Avoid unneeded heap allocation in day 2

master
Nick Krichevsky 2021-12-02 00:48:01 -05:00
parent 445d09cbd7
commit 33b3d303a0
1 changed files with 11 additions and 8 deletions

View File

@ -113,18 +113,21 @@ fn parse_line(line: &str) -> IResult<&str, Direction> {
fn main() {
let input_file_name = env::args().nth(1).expect("No input filename specified");
let input_file = File::open(input_file_name).expect("Could not open input file");
let lines = BufReader::new(input_file)
let directions = BufReader::new(input_file)
.lines()
.map(|res| res.expect("Failed to read line"))
.collect::<Vec<_>>();
let directions = lines
.iter()
.map(|line| {
parse_line(line)
.unwrap_or_else(|err| panic!("Failed to parse line '{}': {}", line, err))
let (remaining, direction) = parse_line(&line)
.unwrap_or_else(|err| panic!("Failed to parse line '{}': {}", line, err));
assert!(
remaining.is_empty(),
"Input remained after parsing: {}",
remaining
);
direction
})
.map(|(_, direction)| direction)
.collect::<Vec<_>>();
println!("Part 1: {}", simulate(&directions, &Part::Part1));