Move Day 2 to its own crate

This commit is contained in:
Eryn Wells 2022-12-15 23:55:22 +00:00
parent 2a932235cc
commit fb3fbddb1f
4 changed files with 25 additions and 10 deletions

7
2022/day2/Cargo.lock generated Normal file
View file

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "day2"
version = "0.1.0"

8
2022/day2/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "day2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

View file

@ -1,6 +1,5 @@
use std::io::Result; use std::env;
use std::fs;
use crate::file::line_reader_for_file;
const SCORE_FOR_ROCK: i32 = 1; const SCORE_FOR_ROCK: i32 = 1;
const SCORE_FOR_PAPER: i32 = 2; const SCORE_FOR_PAPER: i32 = 2;
@ -158,16 +157,19 @@ impl Round {
} }
} }
pub fn main(input_filename: &str) -> Result<()> { fn main() {
let line_reader = line_reader_for_file(input_filename)?; let args: Vec<String> = env::args().collect();
let filename = args.get(1).expect("Missing filename argument");
let file_contents = fs::read_to_string(&filename).expect("Unable to read file");
let lines = file_contents.lines();
let mut number_of_rounds: i32 = 0; let mut number_of_rounds: i32 = 0;
let mut part1_total_score: i32 = 0; let mut part1_total_score: i32 = 0;
let mut part2_total_score: i32 = 0; let mut part2_total_score: i32 = 0;
for line in line_reader { for line in lines {
let line =
line.expect(format!("Failed to read line after {} rounds!", number_of_rounds).as_str());
if let Some(round) = Round::part1_from_string(&line) { if let Some(round) = Round::part1_from_string(&line) {
part1_total_score += round.score(); part1_total_score += round.score();
} }
@ -183,6 +185,4 @@ pub fn main(input_filename: &str) -> Result<()> {
"Part 2: total score with expected outcomes: {}", "Part 2: total score with expected outcomes: {}",
part2_total_score part2_total_score
); );
Ok(())
} }