summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2023-12-09 23:28:08 +0100
committerkdx <kikoodx@paranoici.org>2023-12-09 23:28:08 +0100
commitbeea2fbfecb400991c467354272c1e99d12a2540 (patch)
tree1fb4c27dbda791de1c865f9fc4a3cd460b28acc1
parentfbdb6b2e99b25b900add6f47e22b921a923d059c (diff)
downloadaoc23-beea2fbfecb400991c467354272c1e99d12a2540.tar.gz
09 rust
-rw-r--r--09/rust/Cargo.lock34
-rw-r--r--09/rust/Cargo.toml9
-rw-r--r--09/rust/src/main.rs46
3 files changed, 89 insertions, 0 deletions
diff --git a/09/rust/Cargo.lock b/09/rust/Cargo.lock
new file mode 100644
index 0000000..39fd4de
--- /dev/null
+++ b/09/rust/Cargo.lock
@@ -0,0 +1,34 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "atoi"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f28d99ec8bfea296261ca1af174f24225171fea9664ba9003cbebee704810528"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
+[[package]]
+name = "num-traits"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "rust"
+version = "0.1.0"
+dependencies = [
+ "atoi",
+]
diff --git a/09/rust/Cargo.toml b/09/rust/Cargo.toml
new file mode 100644
index 0000000..c2e223f
--- /dev/null
+++ b/09/rust/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "rust"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+atoi = "2.0.0"
diff --git a/09/rust/src/main.rs b/09/rust/src/main.rs
new file mode 100644
index 0000000..537d38a
--- /dev/null
+++ b/09/rust/src/main.rs
@@ -0,0 +1,46 @@
+use std::io::BufRead;
+
+fn main() {
+ let mut sum = 0;
+ for line in std::io::stdin().lock().lines() {
+ let mut history: Vec<Vec<i32>> = vec![vec![]];
+
+ let line = line.unwrap();
+ let line: Vec<&str> = line.split(" ").collect();
+ for e in line {
+ history[0].push(atoi::atoi(e.as_bytes()).unwrap());
+ }
+
+ loop {
+ history.push(vec![]);
+ let previous_len = history[history.len() - 2].len();
+ for i in 0..previous_len - 1 {
+ let prev_idx = history.len() - 2;
+ let v = history[prev_idx][i + 1] - history[prev_idx][i];
+ history.last_mut().unwrap().push(v);
+ }
+
+ let mut all_zeroes = true;
+ for e in history.last().unwrap() {
+ if *e != 0 {
+ all_zeroes = false;
+ break;
+ }
+ }
+ if all_zeroes {
+ break;
+ }
+ }
+
+ history.last_mut().unwrap().push(0);
+
+ for i in 1..history.len() {
+ let i = history.len() - 1 - i;
+ let idx = history[i].len() - 1;
+ let v = history[i][idx] + history[i + 1][idx];
+ history[i].push(v);
+ }
+ sum += history[0].last().unwrap();
+ }
+ println!("{sum}");
+}