aboutsummaryrefslogtreecommitdiff
path: root/2022/rust/day03/src/main.rs
blob: db5b83e763504bb2eeaceac96658aedfc32a443b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#![feature(iter_array_chunks)]
use std::env;
use std::fs;

fn main() {
    let args = env::args().nth(1).expect("missing input");
    let input = fs::read_to_string(args).unwrap();
    let rucksacks = input.trim().split("\n");

    let compartments: Vec<(&str, &str)> = rucksacks.clone()
        .map(|x| x.split_at(x.len() / 2)).collect();
    let mut sum = 0;
    for (a, b) in compartments.iter() {
        for c in a.chars() {
            if b.contains(c) {
                sum += deascii(c);
                break;
            }
        }
        // println!("{:?}, {:?}", a, b);
    }
    println!("{}", sum);

    sum = 0;
    // yoo this is sick
    for a in rucksacks.array_chunks::<3>() {
        for c in a[0].chars() {
            if a[1].contains(c) && a[2].contains(c) {
                sum += deascii(c);
                break;
            }
        }
    }
    println!("{}", sum);
}

fn deascii(x: char) -> i64 {
    if x as i64 > 96 {
        return x as i64 - 96;
    } else {
        return x as i64 - 38;
    }
}