My solutions for the Advent of Code 2022 - spoilers ahead
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
aoc2022/d6/src/main.rs

55 lines
1.2 KiB

use std::{fs::read_to_string, io::Result};
fn all_diff<T: PartialEq>(list: &[T]) -> bool {
for i in 0..list.len() {
let a = &list[i];
for j in 0..i {
let b = &list[j];
if a == b {
return false;
}
}
}
true
}
fn main() -> Result<()> {
let mut p1_ring_buf = [None; 4];
let mut p1_pos = 0;
let mut p2_ring_buf = [None; 14];
let mut p2_pos = 0;
let mut p1_result = 0;
let mut p2_result = 0;
for (i, c) in read_to_string("input.txt")?.chars().enumerate() {
p1_ring_buf[p1_pos] = Some(c);
p1_pos += 1;
if p1_pos == p1_ring_buf.len() {
p1_pos = 0
}
p2_ring_buf[p2_pos] = Some(c);
p2_pos += 1;
if p2_pos == p2_ring_buf.len() {
p2_pos = 0
}
if p1_result == 0 {
if i >= p1_ring_buf.len() && all_diff(&p1_ring_buf) {
p1_result = i + 1;
}
}
if p2_result == 0 {
if i >= p2_ring_buf.len() && all_diff(&p2_ring_buf) {
p2_result = i + 1;
}
}
}
println!("Part 1: {}", p1_result);
println!("Part 2: {}", p2_result);
Ok(())
}