mirror of
https://github.com/tomru/adventofcode.git
synced 2026-03-02 22:17:19 +01:00
day01 in rust
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1 @@
|
|||||||
/target
|
target
|
||||||
|
|||||||
7
day01/Cargo.lock
generated
Normal file
7
day01/Cargo.lock
generated
Normal file
@@ -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"
|
||||||
8
day01/Cargo.toml
Normal file
8
day01/Cargo.toml
Normal file
@@ -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]
|
||||||
@@ -1,18 +1,24 @@
|
|||||||
import * as readline from 'node:readline';
|
import * as readline from 'node:readline';
|
||||||
import * as fs from 'node:fs';
|
import * as fs from 'node:fs';
|
||||||
|
|
||||||
|
|
||||||
|
const data = [];
|
||||||
const rl = readline.createInterface({
|
const rl = readline.createInterface({
|
||||||
input: fs.createReadStream('./input')
|
input: fs.createReadStream('./input')
|
||||||
});
|
});
|
||||||
|
|
||||||
let last = null;
|
|
||||||
let counter = 0;
|
|
||||||
|
|
||||||
for await (const line of rl) {
|
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++;
|
if (last !== null && v > last) counter++;
|
||||||
last = v;
|
last = v;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(counter);
|
console.log('part1', part1);
|
||||||
|
|
||||||
44
day01/src/main.rs
Normal file
44
day01/src/main.rs
Normal file
@@ -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>) -> 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<i32>) -> Vec<i32> {
|
||||||
|
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<i32> {
|
||||||
|
let mut result: Vec<i32> = vec![];
|
||||||
|
for line in data.lines() {
|
||||||
|
result.push(line.parse::<i32>().unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user