Skip to content

Commit

Permalink
Rust for 2b; clean julia version
Browse files Browse the repository at this point in the history
  • Loading branch information
tjpalmer committed Dec 9, 2018
1 parent bf9d981 commit 768fe37
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 4 deletions.
7 changes: 3 additions & 4 deletions 2018/02/julia/compare.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ function main()
open("../input/input.txt") do file
for line in eachline(file)
id = map(x -> x[1], split(line, ""))
match_count = length(id) - 1
for other in ids
common = other .== id
if sum(common) == match_count
diff = other .!= id
if sum(diff) == 1
# Print what's in common.
println(join(id[common]))
println(join(id[.!diff]))
# No labeled break in Julia yet.
# See also: https://github.com/JuliaLang/julia/issues/5334
@goto done
Expand Down
4 changes: 4 additions & 0 deletions 2018/02/rust/compare/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/compare/Cargo.toml
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]
46 changes: 46 additions & 0 deletions 2018/02/rust/compare/src/main.rs
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()}
}
}

0 comments on commit 768fe37

Please sign in to comment.