From 724ee2d0d5ea7c4df67c590a8184be40e7af2bde Mon Sep 17 00:00:00 2001 From: Muhammad Nauman Raza Date: Wed, 23 Oct 2024 11:43:05 +0100 Subject: [PATCH] feat: basic program function --- Cargo.lock | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 40 ++++++++++++++++++++-- 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..3fc62a0 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,96 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "proc-macro2" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "qalc-rs" +version = "0.1.0" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + +[[package]] +name = "serde" +version = "1.0.213" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ea7893ff5e2466df8d720bb615088341b295f849602c6956047f8f80f0e9bc1" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.213" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e85ad2009c50b58e87caa8cd6dac16bdf511bbfb7af6c33df902396aa480fa5" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.132" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d726bfaff4b320266d395898905d0eba0345aae23b54aee3a737e260fd46db03" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "2.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83540f837a8afc019423a8edb95b52a8effe46957ee402287f4292fae35be021" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" diff --git a/Cargo.toml b/Cargo.toml index d288cd5..08250b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,3 +4,5 @@ version = "0.1.0" edition = "2021" [dependencies] +serde = { version = "1.0.213", features = ["derive"] } +serde_json = "1.0.132" diff --git a/src/main.rs b/src/main.rs index e7a11a9..02bfbeb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,39 @@ -fn main() { - println!("Hello, world!"); +use std::collections::HashMap; +use std::fs; +use std::io; + +fn memorised_words(word_counts: Vec>, ranges: Vec<&str>) -> i32 { + let mut total_memorised: i32 = 0; + + for word_count in word_counts { + for &range in &ranges { + match word_count.get(range) { + Some(length) => { + total_memorised += length + }, + None => {}, + } + } + } + + total_memorised +} + +fn main() { + let file_data = fs::read_to_string("data.json").expect("Unable to read file"); + let word_counts: Vec> = serde_json::from_str(&file_data).expect("Could not parse JSON data"); + + + // Read input from standard input + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to take in ranges"); + + input.pop(); + let ranges: Vec<&str> = input.split(",").collect(); + + let total_memorised = memorised_words(word_counts, ranges); + + println!("{}", total_memorised); }