-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()} | ||
} | ||
} |