From 5070cd414f9be1b63fba61e7b2be7b47b510f92e Mon Sep 17 00:00:00 2001 From: Thomas Ruoff Date: Thu, 30 Dec 2021 00:27:30 +0100 Subject: [PATCH] day01 in rust --- .gitignore | 2 +- day01/Cargo.lock | 7 +++++++ day01/Cargo.toml | 8 ++++++++ day01/{ => data}/input | 0 day01/{ => js}/index.mjs | 16 ++++++++++----- day01/src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 6 deletions(-) create mode 100644 day01/Cargo.lock create mode 100644 day01/Cargo.toml rename day01/{ => data}/input (100%) rename day01/{ => js}/index.mjs (69%) create mode 100644 day01/src/main.rs diff --git a/.gitignore b/.gitignore index ea8c4bf..eb5a316 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/target +target diff --git a/day01/Cargo.lock b/day01/Cargo.lock new file mode 100644 index 0000000..683c0b9 --- /dev/null +++ b/day01/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "day01" +version = "0.1.0" diff --git a/day01/Cargo.toml b/day01/Cargo.toml new file mode 100644 index 0000000..5a61072 --- /dev/null +++ b/day01/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "day01" +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/day01/input b/day01/data/input similarity index 100% rename from day01/input rename to day01/data/input diff --git a/day01/index.mjs b/day01/js/index.mjs similarity index 69% rename from day01/index.mjs rename to day01/js/index.mjs index 0053038..0030cf5 100644 --- a/day01/index.mjs +++ b/day01/js/index.mjs @@ -1,18 +1,24 @@ import * as readline from 'node:readline'; import * as fs from 'node:fs'; + +const data = []; const rl = readline.createInterface({ input: fs.createReadStream('./input') }); -let last = null; -let counter = 0; - for await (const line of rl) { - const v = parseInt(line, 10); + data.push(parseInt(line, 10)); +} + + +let last = null; +let part1 = 0; + +for (const v in data) { if (last !== null && v > last) counter++; last = v; } -console.log(counter); +console.log('part1', part1); diff --git a/day01/src/main.rs b/day01/src/main.rs new file mode 100644 index 0000000..9bcb64d --- /dev/null +++ b/day01/src/main.rs @@ -0,0 +1,44 @@ +use std::fs; + +fn main() { + let input = fs::read_to_string("./data/input").expect("Unable to read file"); + let data = parse(&input); + + println!("Task 1: {:?}", count_inc(1, &data)); + println!("Task 2: {:?}", count_inc(3, &data)); +} + +fn count_inc(window_count: usize, data: &Vec) -> i32 { + let mut last: i32 = i32::MAX; + let mut counter = 0; + for (pos, _) in data.iter().enumerate() { + let e = get_last_n(pos, window_count, data) + .into_iter() + .reduce(|accum, item| accum + item) + .unwrap_or(0); + + if last < e { + counter += 1; + } + + last = e; + } + return counter; +} + +fn get_last_n(index: usize, n: usize, data: &Vec) -> Vec { + let data_len = data.len(); + if index > data_len || index + n > data_len { + return vec![]; + } + return data.get(index..(n + index)).unwrap().to_vec(); +} + +fn parse(data: &String) -> Vec { + let mut result: Vec = vec![]; + for line in data.lines() { + result.push(line.parse::().unwrap()); + } + + return result; +}