1
0

refactor: launch of days with given input file

fixup
This commit is contained in:
Rokas Puzonas 2021-12-06 00:49:09 +02:00
parent a6c9462d91
commit 34dc7889b0

View File

@ -1,22 +1,59 @@
mod day1;
mod day2;
mod day3;
mod day4;
mod day5; mod day5;
use std::{env, process};
use std::fs::File; use std::fs::File;
use std::io::prelude::*; use std::io::prelude::*;
fn main() { fn run(day: i32, part: i32, input_filename: &str) {
let input_filename = "input5.txt";
let mut input_file = File::open(input_filename) let mut input_file = File::open(input_filename)
.expect("Input file not found"); .expect(format!("Input file '{}' not found", input_filename).as_str());
let mut contents = String::new(); let mut contents = String::new();
input_file.read_to_string(&mut contents) input_file.read_to_string(&mut contents)
.expect("Could not read input file"); .expect("Failed to read input file");
let input = day5::parse_input(&contents) // Janky solution so that I could specify what I want to run
.expect("Failed to parse input"); let parse_error_msg = "Failed to parse input";
match format!("{}.{}", day, part).as_str() {
println!("Part 1 result: {}", day5::part1(&input)); "1.1" => println!("{}", day1::part1(&day1::parse_input(&contents).expect(parse_error_msg))),
println!("Part 2 result: {}", day5::part2(&input)); "1.2" => println!("{}", day1::part2(&day1::parse_input(&contents).expect(parse_error_msg))),
"2.1" => println!("{}", day2::part1(&day2::parse_input(&contents).expect(parse_error_msg))),
"2.2" => println!("{}", day2::part2(&day2::parse_input(&contents).expect(parse_error_msg))),
"3.1" => println!("{}", day3::part1(&day3::parse_input(&contents).expect(parse_error_msg))),
"3.2" => println!("{}", day3::part2(&day3::parse_input(&contents).expect(parse_error_msg))),
"4.1" => println!("{}", day4::part1(&day4::parse_input(&contents).expect(parse_error_msg))),
"4.2" => println!("{}", day4::part2(&day4::parse_input(&contents).expect(parse_error_msg))),
"5.1" => println!("{}", day5::part1(&day5::parse_input(&contents).expect(parse_error_msg))),
"5.2" => println!("{}", day5::part2(&day5::parse_input(&contents).expect(parse_error_msg))),
"6.1" => println!("{}", day6::part1(&day6::parse_input(&contents).expect(parse_error_msg))),
"6.2" => println!("{}", day6::part2(&day6::parse_input(&contents).expect(parse_error_msg))),
_ => println!("Day {} part {} not found", day, part)
}
}
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() < 3 {
println!("Usage: {} <day> <part> [input-file]", args[0]);
process::exit(0);
}
let day = args[1].parse::<i32>()
.expect("Failed to parse day");
let part = args[2].parse::<i32>()
.expect("Failed to parse part");
let input_filename;
if args.len() > 3 {
input_filename = args[3].clone();
} else {
input_filename = format!("input{}.txt", day);
}
run(day, part, &input_filename);
} }