Skip to content

Commit

Permalink
Rust for 2a
Browse files Browse the repository at this point in the history
  • Loading branch information
tjpalmer committed Dec 8, 2018
1 parent cf0dd1b commit d575d52
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 2018/02/rust/checksum/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions 2018/02/rust/checksum/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "checksum"
version = "0.1.0"
authors = ["Tom <[email protected]>"]
edition = "2018"

[dependencies]
39 changes: 39 additions & 0 deletions 2018/02/rust/checksum/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::io::{self, BufRead, BufReader};
use std::fs::File;

fn main() -> Try<()> {
let file = File::open("../../input/input.txt")?;
let mut count2 = 0;
let mut count3 = 0;
for line in BufReader::new(file).lines() {
// Count chars.
let mut counts = [0; 256];
for c in line?.bytes() {
counts[c as usize] += 1;
}
// Find 2s and 3s.
count2 += counts.iter().any(|count| *count == 2) as i32;
count3 += counts.iter().any(|count| *count == 3) as i32;
}
println!("{}", count2 * count3);
Ok(())
}

#[derive(Debug)]
pub struct Err {
pub message: String,
}

type Try<Value> = Result<Value, Err>;

impl From<io::Error> for Err {
fn from(_: io::Error) -> Err {
Err{message: "io error".to_string()}
}
}

impl From<std::num::ParseIntError> for Err {
fn from(_: std::num::ParseIntError) -> Err {
Err{message: "parse int error".to_string()}
}
}

0 comments on commit d575d52

Please sign in to comment.