Skip to content

Commit

Permalink
day_3: rust solution
Browse files Browse the repository at this point in the history
  • Loading branch information
MellKam committed Dec 4, 2022
1 parent 5f1e5c8 commit 234d190
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
8 changes: 8 additions & 0 deletions day_3/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "day_3_rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
88 changes: 88 additions & 0 deletions day_3/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const CHARS_COUNT: usize = 52;
type AppearCharArray = [bool; CHARS_COUNT];

fn convert_ascii(ascii_char: u8) -> usize {
if ascii_char > 96 && ascii_char < 123 {
return (ascii_char - 96) as usize;
}

if ascii_char > 64 && ascii_char < 91 {
return (ascii_char - 38) as usize;
}

panic!("Cannot parse '{ascii_char};");
}

fn get_appear_arr(string: &str) -> AppearCharArray {
let mut store: AppearCharArray = [false; CHARS_COUNT];

for byte in string.bytes() {
let parsed_char = convert_ascii(byte);

if !store[parsed_char - 1] {
store[parsed_char - 1] = true;
}
}

return store;
}

fn get_appear_sum(unique_chars_arrays: Vec<AppearCharArray>) -> [u8; CHARS_COUNT] {
let mut result: [u8; CHARS_COUNT] = [0; 52];

for index in 0..CHARS_COUNT {
let mut sum: u8 = 0;

for array_index in 0..unique_chars_arrays.len() {
sum += unique_chars_arrays[array_index][index] as u8;
}

result[index] = sum;
}

return result;
}

fn get_appear_chars_sum(array: [u8; CHARS_COUNT], number: u8) -> i32 {
let mut sum: i32 = 0;

for (index, num) in array.into_iter().enumerate() {
if num == number {
sum += index as i32 + 1;
}
}

return sum;
}

fn main() {
let data = include_str!("../../input.txt");

let mut iter = data.lines();
let mut sum: i32 = 0;

loop {
let lines = match (iter.next(), iter.next(), iter.next()) {
(Some(a), Some(b), Some(c)) => [a, b, c],
_ => break,
};

let appears = Vec::from(lines.map(|line| get_appear_arr(line)));

let chars_sum = get_appear_chars_sum(get_appear_sum(appears), lines.len() as u8);
sum += chars_sum;
}

// let sum: i32 = data
// .lines()
// .map(|line| {
// let (left, right) = line.split_at(line.len() / 2);

// let appears = vec![get_appear_arr(left), get_appear_arr(right)];

// return get_appear_chars_sum(get_appear_sum(appears), 2);
// })
// .sum();

println!("{sum}");
}

0 comments on commit 234d190

Please sign in to comment.