Move Day 3 to its own crate
This commit is contained in:
parent
c993f95278
commit
937530e605
4 changed files with 28 additions and 11 deletions
7
2022/day3/Cargo.lock
generated
Normal file
7
2022/day3/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day3"
|
||||
version = "0.1.0"
|
8
2022/day3/Cargo.toml
Normal file
8
2022/day3/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day3"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
|
@ -1,9 +1,8 @@
|
|||
use std::collections::HashSet;
|
||||
use std::io::Result;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::iter::Iterator;
|
||||
|
||||
use crate::file::line_reader_for_file;
|
||||
|
||||
fn priority_items_in_knapsack(knapsack: &str) -> Vec<char> {
|
||||
assert!(knapsack.len() % 2 == 0);
|
||||
|
||||
|
@ -28,16 +27,21 @@ fn badge_for_group(group: (&str, &str, &str)) -> Vec<char> {
|
|||
ab_intersection.intersection(&c_set).map(|c| *c).collect()
|
||||
}
|
||||
|
||||
pub fn main(filename: &str) -> Result<()> {
|
||||
let mut line_reader = line_reader_for_file(filename)?.peekable();
|
||||
fn main() {
|
||||
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 mut lines = file_contents.lines().peekable();
|
||||
|
||||
let mut priority_letters: Vec<char> = Vec::new();
|
||||
let mut badges: Vec<char> = Vec::new();
|
||||
|
||||
while line_reader.peek().is_some() {
|
||||
let a = line_reader.next().unwrap()?;
|
||||
let b = line_reader.next().unwrap()?;
|
||||
let c = line_reader.next().unwrap()?;
|
||||
while lines.peek().is_some() {
|
||||
let a = lines.next().unwrap();
|
||||
let b = lines.next().unwrap();
|
||||
let c = lines.next().unwrap();
|
||||
|
||||
priority_letters.extend(priority_items_in_knapsack(&a));
|
||||
priority_letters.extend(priority_items_in_knapsack(&b));
|
||||
|
@ -79,6 +83,4 @@ pub fn main(filename: &str) -> Result<()> {
|
|||
.sum();
|
||||
|
||||
println!("Part 2: sum of badges: {}", sum_of_badges);
|
||||
|
||||
Ok(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue