Move Day 6 to its own crate

This commit is contained in:
Eryn Wells 2022-12-16 00:14:34 +00:00
parent b905d7f7fd
commit c04c7adfd2
4 changed files with 26 additions and 15 deletions

7
2022/day6/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 = "day6"
version = "0.1.0"

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

@ -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]

View file

@ -1,28 +1,26 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::fs::File; use std::env;
use std::io::{BufRead, BufReader}; use std::fs;
pub fn main(filename: &str) -> std::io::Result<()> { fn main() {
let file = File::open(filename)?; let args: Vec<String> = env::args().collect();
let reader = BufReader::new(file);
let file_contents: Vec<char> = 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<char> = file_contents
.lines() .lines()
.map(|l| { .map(|l| l.chars().collect::<Vec<char>>())
l.expect("Couldn't read line")
.chars()
.collect::<Vec<char>>()
})
.flatten() .flatten()
.collect(); .collect();
let (sop_offset, start_of_packet_marker) = file_contents let (sop_offset, start_of_packet_marker) = character_stream
.windows(4) .windows(4)
.enumerate() .enumerate()
.find(|(_, w)| HashSet::<&char>::from_iter(w.iter()).len() == 4) .find(|(_, w)| HashSet::<&char>::from_iter(w.iter()).len() == 4)
.expect("Couldn't find start-of-packet marker"); .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) .windows(14)
.enumerate() .enumerate()
.find(|(_, w)| HashSet::<&char>::from_iter(w.iter()).len() == 14) .find(|(_, w)| HashSet::<&char>::from_iter(w.iter()).len() == 14)
@ -47,6 +45,4 @@ pub fn main(filename: &str) -> std::io::Result<()> {
.join(""), .join(""),
sow_offset + 14 sow_offset + 14
); );
Ok(())
} }