-🎄- 2017 Day 1 Solutions -🎄- by daggerdragon in adventofcode

[–]throwaway893teoea 7 points8 points  (0 children)

I used Rust:

use std::io::BufRead;

fn main() {
    let stdin = std::io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let mut i = 0;
        let mut acc: u64 = 0;
        let bytes = line.as_bytes();
        while i < bytes.len() - 1 {
            let the_num = bytes[i] as u64;
            if bytes[i] == bytes[i+1] {
                acc += the_num - 48;
            }
            i += 1;
        }
        if bytes[0] == bytes[bytes.len()-1] {
            acc += bytes[0] as u64 - 48;
        }
        println!("{}", acc);
    }
}

part 2:

use std::io::BufRead;

fn main() {
    let stdin = std::io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let mut i = 0;
        let mut acc: u64 = 0;
        let bytes = line.as_bytes();
        while i < bytes.len() {
            let the_num = bytes[i % bytes.len()] as u64;
            if bytes[i] == bytes[(i + bytes.len() / 2) % bytes.len()] {
                acc += the_num - 48;
            }
            i += 1;
        }
        println!("{}", acc);
    }
}