-
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
4 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
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
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 = "compare" | ||
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,46 @@ | ||
use std::io::{self, BufRead, BufReader}; | ||
use std::fs::File; | ||
|
||
fn main() -> Try<()> { | ||
let file = File::open("../../input/input.txt")?; | ||
let mut ids: Vec<String> = Vec::new(); | ||
'lines: for line in BufReader::new(file).lines() { | ||
// Count chars. | ||
let id = line?; | ||
'others: for other in ids.iter() { | ||
let mut diff_index = -1; | ||
for (index, (c0, c1)) in id.chars().zip(other.chars()).enumerate() { | ||
if c0 != c1 { | ||
if diff_index >= 0 { | ||
// More than one different. | ||
continue 'others; | ||
} else { | ||
diff_index = index as i32; | ||
} | ||
} | ||
} | ||
if diff_index >= 0 { | ||
// If we get here, it's because we had only one different. | ||
let (a, b) = id.split_at(diff_index as usize); | ||
let c: String = b.chars().skip(1).collect(); | ||
println!("{}{}", a, c); | ||
break 'lines; | ||
} | ||
} | ||
ids.push(id); | ||
} | ||
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()} | ||
} | ||
} |