feat: solve day 2 part 1
This commit is contained in:
parent
85dd429a36
commit
28223a1b34
1000
input2.txt
Normal file
1000
input2.txt
Normal file
File diff suppressed because it is too large
Load Diff
74
src/day2.rs
Normal file
74
src/day2.rs
Normal file
@ -0,0 +1,74 @@
|
||||
use std::fs::File;
|
||||
use std::io::{prelude::*, self};
|
||||
use std::num::ParseIntError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InputFromFileError {
|
||||
ParseCommandError,
|
||||
ParseIntError(ParseIntError),
|
||||
IoError(io::Error),
|
||||
}
|
||||
|
||||
pub enum Command {
|
||||
Forward,
|
||||
Down,
|
||||
Up
|
||||
}
|
||||
|
||||
pub struct CommandLine(Command, u32);
|
||||
|
||||
fn parse_line(line: &str) -> Result<CommandLine, InputFromFileError> {
|
||||
let parts: Vec<&str> = line.split(' ').collect();
|
||||
let command = match parts[0] {
|
||||
"up" => Ok(Command::Up),
|
||||
"down" => Ok(Command::Down),
|
||||
"forward" => Ok(Command::Forward),
|
||||
_ => Err(InputFromFileError::ParseCommandError)
|
||||
}?;
|
||||
let amount = parts[1].parse().map_err(InputFromFileError::ParseIntError)?;
|
||||
Ok(CommandLine(command, amount))
|
||||
}
|
||||
|
||||
pub fn input_from_file(filename: &str) -> Result<Vec<CommandLine>, InputFromFileError> {
|
||||
let mut file = File::open(filename).map_err(InputFromFileError::IoError)?;
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).map_err(InputFromFileError::IoError)?;
|
||||
|
||||
contents.split_terminator('\n')
|
||||
.map(parse_line)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn part1(commands: &[CommandLine]) -> u32 {
|
||||
let mut depth = 0;
|
||||
let mut horizontal = 0;
|
||||
for command in commands {
|
||||
match command.0 {
|
||||
Command::Up => depth -= command.1,
|
||||
Command::Down => depth += command.1,
|
||||
Command::Forward => horizontal += command.1,
|
||||
}
|
||||
}
|
||||
return depth * horizontal;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn part1_example() {
|
||||
let commands = [
|
||||
CommandLine(Command::Forward, 5),
|
||||
CommandLine(Command::Down, 5),
|
||||
CommandLine(Command::Forward, 8),
|
||||
CommandLine(Command::Up, 3),
|
||||
CommandLine(Command::Down, 8),
|
||||
CommandLine(Command::Forward, 2)
|
||||
];
|
||||
let result = part1(&commands);
|
||||
assert_eq!(result, 150);
|
||||
}
|
||||
}
|
||||
|
18
src/main.rs
18
src/main.rs
@ -1,12 +1,18 @@
|
||||
mod day1;
|
||||
mod day2;
|
||||
|
||||
fn main() {
|
||||
let input_filename = "input.txt";
|
||||
let input = day1::input_from_file(input_filename)
|
||||
.expect("Failed to read input.txt");
|
||||
let result1 = day1::part1(&input);
|
||||
let result2 = day1::part2(&input);
|
||||
let input_filename = "input2.txt";
|
||||
let input = day2::input_from_file(input_filename)
|
||||
.expect("Failed to read input2.txt");
|
||||
|
||||
// let result1 = day1::part1(&input);
|
||||
// let result2 = day1::part2(&input);
|
||||
|
||||
let result1 = day2::part1(&input);
|
||||
// let result2 = day2::part2(&input);
|
||||
|
||||
println!("Part 1 result: {}", result1);
|
||||
println!("Part 2 result: {}", result2);
|
||||
// println!("Part 2 result: {}", result2);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user