diff --git a/2022/day6/Cargo.lock b/2022/day6/Cargo.lock new file mode 100644 index 0000000..a16c0bf --- /dev/null +++ b/2022/day6/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day6" +version = "0.1.0" diff --git a/2022/day6/Cargo.toml b/2022/day6/Cargo.toml new file mode 100644 index 0000000..89d04ae --- /dev/null +++ b/2022/day6/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day6" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2022/aoc2022/data/day6-input.txt b/2022/day6/input.txt similarity index 100% rename from 2022/aoc2022/data/day6-input.txt rename to 2022/day6/input.txt diff --git a/2022/aoc2022/src/day6.rs b/2022/day6/src/main.rs similarity index 66% rename from 2022/aoc2022/src/day6.rs rename to 2022/day6/src/main.rs index c4ac066..9bb078d 100644 --- a/2022/aoc2022/src/day6.rs +++ b/2022/day6/src/main.rs @@ -1,28 +1,26 @@ use std::collections::HashSet; -use std::fs::File; -use std::io::{BufRead, BufReader}; +use std::env; +use std::fs; -pub fn main(filename: &str) -> std::io::Result<()> { - let file = File::open(filename)?; - let reader = BufReader::new(file); +fn main() { + let args: Vec = env::args().collect(); - let file_contents: Vec = reader + let filename = args.get(1).expect("Missing filename argument"); + + let file_contents = fs::read_to_string(&filename).expect("Unable to read file"); + let character_stream: Vec = file_contents .lines() - .map(|l| { - l.expect("Couldn't read line") - .chars() - .collect::>() - }) + .map(|l| l.chars().collect::>()) .flatten() .collect(); - let (sop_offset, start_of_packet_marker) = file_contents + let (sop_offset, start_of_packet_marker) = character_stream .windows(4) .enumerate() .find(|(_, w)| HashSet::<&char>::from_iter(w.iter()).len() == 4) .expect("Couldn't find start-of-packet marker"); - let (sow_offset, start_of_window_marker) = file_contents + let (sow_offset, start_of_window_marker) = character_stream .windows(14) .enumerate() .find(|(_, w)| HashSet::<&char>::from_iter(w.iter()).len() == 14) @@ -47,6 +45,4 @@ pub fn main(filename: &str) -> std::io::Result<()> { .join(""), sow_offset + 14 ); - - Ok(()) }