day 6
This commit is contained in:
Generated
+7
@@ -0,0 +1,7 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "d6"
|
||||||
|
version = "0.1.0"
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "d6"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
@@ -0,0 +1,55 @@
|
|||||||
|
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(())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user