From 216616ab804a9b8df86511f356f692db5406d654 Mon Sep 17 00:00:00 2001 From: j-james Date: Mon, 5 Dec 2022 22:54:35 -0800 Subject: Day Six --- 2022/nim/day06/src/main.nim | 25 +++++++++++++++++++++++++ 2022/rust/day06/Cargo.lock | 7 +++++++ 2022/rust/day06/Cargo.toml | 8 ++++++++ 2022/rust/day06/src/main.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 2022/nim/day06/src/main.nim create mode 100644 2022/rust/day06/Cargo.lock create mode 100644 2022/rust/day06/Cargo.toml create mode 100644 2022/rust/day06/src/main.rs diff --git a/2022/nim/day06/src/main.nim b/2022/nim/day06/src/main.nim new file mode 100644 index 0000000..cd8eb0a --- /dev/null +++ b/2022/nim/day06/src/main.nim @@ -0,0 +1,25 @@ +# Day 6: Tuning Trouble +import std/[os, strutils, sets] + +let input = paramStr(1).readFile().strip().split("\n")[0] + +iterator slide(input: string, num: int): string = + for i in 0 ..< input.len - num: + var result: string + for j in 0 ..< num: + result &= input[i+j] + yield result + +var i = 4 +for window in input.slide(4): + if window.toHashSet.len == 4: + break + inc i +echo i + +i = 14 +for window in input.slide(14): + if window.toHashSet.len == 14: + break + inc i +echo i diff --git a/2022/rust/day06/Cargo.lock b/2022/rust/day06/Cargo.lock new file mode 100644 index 0000000..0c65426 --- /dev/null +++ b/2022/rust/day06/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day06" +version = "0.1.0" diff --git a/2022/rust/day06/Cargo.toml b/2022/rust/day06/Cargo.toml new file mode 100644 index 0000000..2f276f3 --- /dev/null +++ b/2022/rust/day06/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day06" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/2022/rust/day06/src/main.rs b/2022/rust/day06/src/main.rs new file mode 100644 index 0000000..b5f42cc --- /dev/null +++ b/2022/rust/day06/src/main.rs @@ -0,0 +1,27 @@ +use std::collections::HashSet; +use std::env; +use std::fs; + +fn main() { + let args = env::args().nth(1).expect("missing input"); + let input = fs::read_to_string(args).unwrap(); + let signal: Vec = input.trim().chars().collect(); + + let depth = 4; + for (i, c) in signal.windows(depth).enumerate() { + // why can rust not infer the type of HashSet? + // https://stackoverflow.com/questions/62949404/ + if HashSet::<_>::from_iter(c).len() == depth { + println!("{}", i + depth); + break; + } + } + + let depth = 14; + for (i, c) in signal.windows(depth).enumerate() { + if HashSet::<_>::from_iter(c).len() == depth { + println!("{}", i + depth); + break; + } + } +} -- cgit v1.2.3-70-g09d2