1
0

refactor: move input.txt reading into main.rs

This commit is contained in:
Rokas Puzonas 2021-12-03 11:57:08 +02:00
parent b36eb44b3c
commit 71cd3daec3
3 changed files with 51 additions and 67 deletions

View File

@ -1,21 +1,8 @@
use std::fs::File;
use std::io::{prelude::*, self};
use std::num::ParseIntError; use std::num::ParseIntError;
#[derive(Debug)] pub fn parse_input(input: &str) -> Result<Vec<u32>, ParseIntError> {
pub enum InputFromFileError { input.split_whitespace()
ParseIntError(ParseIntError), .map(|s| s.parse())
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))
.collect() .collect()
} }

View File

@ -1,67 +1,59 @@
use std::fs::File;
use std::io::{prelude::*, self};
use std::num::ParseIntError; use std::num::ParseIntError;
#[derive(Debug)] #[derive(Debug)]
pub enum InputFromFileError { pub enum ParseCommandError {
ParseCommandError, ParseEnumError,
ParseIntError(ParseIntError), ParseIntError(ParseIntError),
IoError(io::Error),
} }
pub enum Command { pub enum CommandType {
Forward, Forward,
Down, Down,
Up 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 parts: Vec<&str> = line.split(' ').collect();
let command = match parts[0] { let command = match parts[0] {
"up" => Ok(Command::Up), "up" => Ok(CommandType::Up),
"down" => Ok(Command::Down), "down" => Ok(CommandType::Down),
"forward" => Ok(Command::Forward), "forward" => Ok(CommandType::Forward),
_ => Err(InputFromFileError::ParseCommandError) _ => Err(ParseCommandError::ParseEnumError)
}?; }?;
let amount = parts[1].parse().map_err(InputFromFileError::ParseIntError)?; let amount = parts[1].parse().map_err(ParseCommandError::ParseIntError)?;
Ok(CommandLine(command, amount)) Ok(Command(command, amount))
} }
pub fn input_from_file(filename: &str) -> Result<Vec<CommandLine>, InputFromFileError> { pub fn parse_input(input: &str) -> Result<Vec<Command>, ParseCommandError> {
let mut file = File::open(filename).map_err(InputFromFileError::IoError)?; input.split_terminator('\n')
let mut contents = String::new();
file.read_to_string(&mut contents).map_err(InputFromFileError::IoError)?;
contents.split_terminator('\n')
.map(parse_line) .map(parse_line)
.collect() .collect()
} }
pub fn part1(commands: &[CommandLine]) -> u32 { pub fn part1(commands: &[Command]) -> u32 {
let mut depth = 0; let mut depth = 0;
let mut horizontal = 0; let mut horizontal = 0;
for command in commands { for command in commands {
match command.0 { match command.0 {
Command::Up => depth -= command.1, CommandType::Up => depth -= command.1,
Command::Down => depth += command.1, CommandType::Down => depth += command.1,
Command::Forward => horizontal += command.1, CommandType::Forward => horizontal += command.1,
} }
} }
return depth * horizontal; return depth * horizontal;
} }
pub fn part2(commands: &[CommandLine]) -> u32 { pub fn part2(commands: &[Command]) -> u32 {
let mut depth = 0; let mut depth = 0;
let mut horizontal = 0; let mut horizontal = 0;
let mut aim = 0; let mut aim = 0;
for command in commands { for command in commands {
match command.0 { match command.0 {
Command::Up => aim -= command.1, CommandType::Up => aim -= command.1,
Command::Down => aim += command.1, CommandType::Down => aim += command.1,
Command::Forward => { CommandType::Forward => {
horizontal += command.1; horizontal += command.1;
depth += aim * command.1; depth += aim * command.1;
} }
@ -77,12 +69,12 @@ mod tests {
#[test] #[test]
fn part1_example() { fn part1_example() {
let commands = [ let commands = [
CommandLine(Command::Forward, 5), Command(CommandType::Forward, 5),
CommandLine(Command::Down, 5), Command(CommandType::Down, 5),
CommandLine(Command::Forward, 8), Command(CommandType::Forward, 8),
CommandLine(Command::Up, 3), Command(CommandType::Up, 3),
CommandLine(Command::Down, 8), Command(CommandType::Down, 8),
CommandLine(Command::Forward, 2) Command(CommandType::Forward, 2)
]; ];
let result = part1(&commands); let result = part1(&commands);
assert_eq!(result, 150); assert_eq!(result, 150);
@ -91,12 +83,12 @@ mod tests {
#[test] #[test]
fn part2_example() { fn part2_example() {
let commands = [ let commands = [
CommandLine(Command::Forward, 5), Command(CommandType::Forward, 5),
CommandLine(Command::Down, 5), Command(CommandType::Down, 5),
CommandLine(Command::Forward, 8), Command(CommandType::Forward, 8),
CommandLine(Command::Up, 3), Command(CommandType::Up, 3),
CommandLine(Command::Down, 8), Command(CommandType::Down, 8),
CommandLine(Command::Forward, 2) Command(CommandType::Forward, 2)
]; ];
let result = part2(&commands); let result = part2(&commands);
assert_eq!(result, 900) assert_eq!(result, 900)

View File

@ -1,18 +1,23 @@
mod day1; mod day1;
mod day2; mod day2;
use std::fs::File;
use std::io::prelude::*;
fn main() { fn main() {
let input_filename = "input2.txt"; let input_filename = "input1.txt";
let input = day2::input_from_file(input_filename)
.expect("Failed to read input2.txt");
// let result1 = day1::part1(&input); let mut input_file = File::open(input_filename)
// let result2 = day1::part2(&input); .expect("Input file not found");
let result1 = day2::part1(&input); let mut contents = String::new();
let result2 = day2::part2(&input); input_file.read_to_string(&mut contents)
.expect("Could not read input file");
println!("Part 1 result: {}", result1); let input = day2::parse_input(input_filename)
println!("Part 2 result: {}", result2); .expect("Failed to parse input");
println!("Part 1 result: {}", day2::part1(&input));
println!("Part 2 result: {}", day2::part2(&input));
} }