refactor: move input.txt reading into main.rs
This commit is contained in:
parent
b36eb44b3c
commit
71cd3daec3
19
src/day1.rs
19
src/day1.rs
@ -1,21 +1,8 @@
|
||||
use std::fs::File;
|
||||
use std::io::{prelude::*, self};
|
||||
use std::num::ParseIntError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InputFromFileError {
|
||||
ParseIntError(ParseIntError),
|
||||
IoError(io::Error),
|
||||
}
|
||||
|
||||
pub fn input_from_file(filename: &str) -> Result<Vec<u32>, 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_whitespace()
|
||||
.map(|s| s.parse().map_err(InputFromFileError::ParseIntError))
|
||||
pub fn parse_input(input: &str) -> Result<Vec<u32>, ParseIntError> {
|
||||
input.split_whitespace()
|
||||
.map(|s| s.parse())
|
||||
.collect()
|
||||
}
|
||||
|
||||
|
74
src/day2.rs
74
src/day2.rs
@ -1,67 +1,59 @@
|
||||
use std::fs::File;
|
||||
use std::io::{prelude::*, self};
|
||||
use std::num::ParseIntError;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum InputFromFileError {
|
||||
ParseCommandError,
|
||||
pub enum ParseCommandError {
|
||||
ParseEnumError,
|
||||
ParseIntError(ParseIntError),
|
||||
IoError(io::Error),
|
||||
}
|
||||
|
||||
pub enum Command {
|
||||
pub enum CommandType {
|
||||
Forward,
|
||||
Down,
|
||||
Up
|
||||
}
|
||||
|
||||
pub struct CommandLine(Command, u32);
|
||||
pub struct Command(CommandType, u32);
|
||||
|
||||
fn parse_line(line: &str) -> Result<CommandLine, InputFromFileError> {
|
||||
fn parse_line(line: &str) -> Result<Command, ParseCommandError> {
|
||||
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)
|
||||
"up" => Ok(CommandType::Up),
|
||||
"down" => Ok(CommandType::Down),
|
||||
"forward" => Ok(CommandType::Forward),
|
||||
_ => Err(ParseCommandError::ParseEnumError)
|
||||
}?;
|
||||
let amount = parts[1].parse().map_err(InputFromFileError::ParseIntError)?;
|
||||
Ok(CommandLine(command, amount))
|
||||
let amount = parts[1].parse().map_err(ParseCommandError::ParseIntError)?;
|
||||
Ok(Command(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')
|
||||
pub fn parse_input(input: &str) -> Result<Vec<Command>, ParseCommandError> {
|
||||
input.split_terminator('\n')
|
||||
.map(parse_line)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn part1(commands: &[CommandLine]) -> u32 {
|
||||
pub fn part1(commands: &[Command]) -> 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,
|
||||
CommandType::Up => depth -= command.1,
|
||||
CommandType::Down => depth += command.1,
|
||||
CommandType::Forward => horizontal += command.1,
|
||||
}
|
||||
}
|
||||
return depth * horizontal;
|
||||
}
|
||||
|
||||
pub fn part2(commands: &[CommandLine]) -> u32 {
|
||||
pub fn part2(commands: &[Command]) -> u32 {
|
||||
let mut depth = 0;
|
||||
let mut horizontal = 0;
|
||||
let mut aim = 0;
|
||||
for command in commands {
|
||||
match command.0 {
|
||||
Command::Up => aim -= command.1,
|
||||
Command::Down => aim += command.1,
|
||||
Command::Forward => {
|
||||
CommandType::Up => aim -= command.1,
|
||||
CommandType::Down => aim += command.1,
|
||||
CommandType::Forward => {
|
||||
horizontal += command.1;
|
||||
depth += aim * command.1;
|
||||
}
|
||||
@ -77,12 +69,12 @@ mod tests {
|
||||
#[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)
|
||||
Command(CommandType::Forward, 5),
|
||||
Command(CommandType::Down, 5),
|
||||
Command(CommandType::Forward, 8),
|
||||
Command(CommandType::Up, 3),
|
||||
Command(CommandType::Down, 8),
|
||||
Command(CommandType::Forward, 2)
|
||||
];
|
||||
let result = part1(&commands);
|
||||
assert_eq!(result, 150);
|
||||
@ -91,12 +83,12 @@ mod tests {
|
||||
#[test]
|
||||
fn part2_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)
|
||||
Command(CommandType::Forward, 5),
|
||||
Command(CommandType::Down, 5),
|
||||
Command(CommandType::Forward, 8),
|
||||
Command(CommandType::Up, 3),
|
||||
Command(CommandType::Down, 8),
|
||||
Command(CommandType::Forward, 2)
|
||||
];
|
||||
let result = part2(&commands);
|
||||
assert_eq!(result, 900)
|
||||
|
25
src/main.rs
25
src/main.rs
@ -1,18 +1,23 @@
|
||||
mod day1;
|
||||
mod day2;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
|
||||
fn main() {
|
||||
let input_filename = "input2.txt";
|
||||
let input = day2::input_from_file(input_filename)
|
||||
.expect("Failed to read input2.txt");
|
||||
let input_filename = "input1.txt";
|
||||
|
||||
// let result1 = day1::part1(&input);
|
||||
// let result2 = day1::part2(&input);
|
||||
|
||||
let result1 = day2::part1(&input);
|
||||
let result2 = day2::part2(&input);
|
||||
let mut input_file = File::open(input_filename)
|
||||
.expect("Input file not found");
|
||||
|
||||
println!("Part 1 result: {}", result1);
|
||||
println!("Part 2 result: {}", result2);
|
||||
let mut contents = String::new();
|
||||
input_file.read_to_string(&mut contents)
|
||||
.expect("Could not read input file");
|
||||
|
||||
let input = day2::parse_input(input_filename)
|
||||
.expect("Failed to parse input");
|
||||
|
||||
println!("Part 1 result: {}", day2::part1(&input));
|
||||
println!("Part 2 result: {}", day2::part2(&input));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user