You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.7 KiB
Rust
68 lines
1.7 KiB
Rust
#![crate_name = "advent_of_code_day6"]
|
|
|
|
use std::fs;
|
|
use std::path::{Path};
|
|
//use regex::Regex;
|
|
//use lazy_static::lazy_static;
|
|
use std::collections::VecDeque;
|
|
|
|
|
|
fn parse_file(input: &str) -> Vec<Vec<char>> {
|
|
let lines: Vec<&str> = input.split("\n").collect();
|
|
|
|
let mut vec_chars: Vec<Vec<char>> = Vec::new();
|
|
for line in &lines {
|
|
let mut chars: Vec<char> = Vec::new();
|
|
for s in line.chars() {
|
|
//println!("Parts: {:?}", s);
|
|
chars.push(s as char);
|
|
}
|
|
vec_chars.push(chars);
|
|
}
|
|
return vec_chars;
|
|
}
|
|
|
|
fn contains_unique_sequence(seq: &VecDeque<char>) -> bool {
|
|
let mut checked: Vec<char> = Vec::new();
|
|
for c in seq {
|
|
if checked.contains(c) {
|
|
return false;
|
|
}
|
|
else {
|
|
checked.push(*c);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
fn find_starter_marker(transmission: &Vec<char>) -> u32 {
|
|
let mut last_seen: VecDeque<char> = VecDeque::new();
|
|
|
|
for i in 0..transmission.len() {
|
|
last_seen.push_back(transmission[i]);
|
|
|
|
if last_seen.len() >= 4 {
|
|
println!("{:?} is unique: {}", last_seen, contains_unique_sequence(&last_seen));
|
|
if contains_unique_sequence(&last_seen) {
|
|
return i as u32;
|
|
}
|
|
last_seen.pop_front();
|
|
}
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
fn main() {
|
|
let input_path = Path::new("./input/input.lst");
|
|
|
|
let contents = fs::read_to_string(input_path).unwrap();
|
|
println!("My given file: \n{}\n\n", contents);
|
|
|
|
let vec_chars: Vec<Vec<char>> = parse_file(&contents);
|
|
for chars in &vec_chars {
|
|
let start_pos: u32 = find_starter_marker(&chars);
|
|
println!("Found starter packet at position: {}", start_pos+1);
|
|
}
|
|
} |