--- /dev/null
+pub fn first_marker_pos(signal: &str, n: usize) -> usize {
+ for (i, c) in signal.chars().collect::<Vec<char>>().windows(n).enumerate() {
+ if !contains_same_char(c) {
+ return i + n;
+ }
+ }
+ 0
+}
+
+// Warning: O(n^2).
+fn contains_same_char(chars: &[char]) -> bool {
+ for i in 0..chars.len() {
+ for j in i+1..chars.len() {
+ if chars[i] == chars[j] {
+ return true;
+ }
+ }
+ }
+ false
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn part1() {
+ assert_eq!(first_marker_pos("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 4), 7);
+ assert_eq!(first_marker_pos("bvwbjplbgvbhsrlpgdmjqwftvncz", 4), 5);
+ assert_eq!(first_marker_pos("nppdvjthqldpwncqszvftbrmjlhg", 4), 6);
+ assert_eq!(first_marker_pos("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 4), 10);
+ assert_eq!(first_marker_pos("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 4), 11);
+ }
+
+ #[test]
+ fn part2() {
+ assert_eq!(first_marker_pos("mjqjpqmgbljsphdztnvjfqwrcgsmlb", 14), 19);
+ assert_eq!(first_marker_pos("bvwbjplbgvbhsrlpgdmjqwftvncz", 14), 23);
+ assert_eq!(first_marker_pos("nppdvjthqldpwncqszvftbrmjlhg", 14), 23);
+ assert_eq!(first_marker_pos("nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", 14), 29);
+ assert_eq!(first_marker_pos("zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", 14), 26);
+ }
+}
\ No newline at end of file
mod day03;
mod day04;
mod day05;
+mod day06;
fn day01() -> String {
let f = fs::File::open("data/day01.input").unwrap();
format!("part1: {}, part2: {}", day05::get_top_as_string(&stacks), day05::get_top_as_string(&stacks2))
}
+fn day06() -> String {
+ let signals = fs::read_to_string("data/day06.input").unwrap();
+ format!("part1: {}, part2: {}", day06::first_marker_pos(&signals, 4), day06::first_marker_pos(&signals, 14))
+}
+
fn format_micros(t: u128) -> String {
if t < 10_000 {
format!("{} μs", t)
day03,
day04,
day05,
+ day06,
);
let args: Vec<String> = env::args().skip(1).collect();