Added day09 solutions

This commit is contained in:
Russell Champoux committed 2016-12-09 12:01:55 -05:00
1 parent a8b1f29c3a
commit 38454dfa97
3 files changed
+192

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
[root]
name = "day09"
version = "0.1.0"
dependencies = [
"regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "aho-corasick"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "libc"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "memchr"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex"
version = "0.1.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
"regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
"utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "regex-syntax"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "thread-id"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "thread_local"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "utf8-ranges"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum aho-corasick 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ca972c2ea5f742bfce5687b9aef75506a764f61d37f8f649047846a9686ddb66"
"checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
"checksum libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "a51822fc847e7a8101514d1d44e354ba2ffa7d4c194dcab48870740e327cac70"
"checksum memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d8b629fb514376c675b98c1421e80b151d3817ac42d7c667717d282761418d20"
"checksum regex 0.1.80 (registry+https://github.com/rust-lang/crates.io-index)" = "4fd4ace6a8cf7860714a2c2280d6c1f7e6a413486c13298bbc86fd3da019402f"
"checksum regex-syntax 0.3.9 (registry+https://github.com/rust-lang/crates.io-index)" = "f9ec002c35e86791825ed294b50008eea9ddfc8def4420124fbc6b08db834957"
"checksum thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a9539db560102d1cef46b8b78ce737ff0bb64e7e18d35b2a5688f7d097d0ff03"
"checksum thread_local 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "8576dbbfcaef9641452d5cf0df9b0e7eeab7694956dd33bb61515fb8f18cfdd5"
"checksum utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a1ca13c08c41c9c3e04224ed9ff80461d97e121589ff27c753a16cb10830ae0f"
"checksum winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
+7
View File
@@ -0,0 +1,7 @@
[package]
name = "day09"
version = "0.1.0"
authors = ["Russell Champoux <russell.champoux@thomsonreuters.com>"]
[dependencies]
regex = "0.1"
+87
View File
@@ -0,0 +1,87 @@
extern crate regex;
use regex::Regex;
use std::io::Read;
use std::fs::File;
fn part_1(input: &String) {
let set: Vec<char> = input.chars().collect();
let mut idx: usize = 0;
let mut output = String::new();
let re = Regex::new(r"\((\d+)x(\d+)\)").unwrap();
//step by single characters
//when you find an open paren, look for the next closing paren and match to a regex like (x,y)
//if it does not match, add contents to output and continue
//if it matches, add the next 'x' characters to the output 'y' times and increase idx by 'x'
//if not an open paren, add to output
while idx < input.len() {
let c = set[idx];
match c {
'(' => {
let old_idx: usize = idx; //for when we have to colleect the slice
loop {
idx = idx + 1;
match set[idx] {
')' => break,
_ => {},
}
}
let cap_slice = &input[old_idx..idx+1];
let cap = re.captures(cap_slice).unwrap();
let char_count: usize = cap.at(1).unwrap().parse().unwrap();
let times = cap.at(2).unwrap().parse().unwrap();
print!("({}x{}) ", char_count, times);
//println!("({}x{}) adding slice from {} to {}", char_count, times, idx, idx+char_count);
for time in 0..times {
output = output + &input[idx+1..idx+char_count+1]; //copy the next char_count characters
}
idx = idx + char_count; //skip over all the chars we added
},
_ => {
output.push(c);
//println!("{} input is len {}", idx, input.len());
},
}
idx = idx + 1; //increment idx in every case
}
println!("Final size is {}", output.len());
}
fn get_count(input: &String) -> usize {
let re_letters = Regex::new(r"^[^\(\)]*$").unwrap(); //I'd prefer to make this static but don't know how
if re_letters.is_match(&input) { //if there are no parens
return input.len(); //return the length
}
let re = Regex::new(r"^([:alpha:]*)\((\d+)x(\d+)\)(.*)$").unwrap();
match re.captures(input) {
None => println!("Found nothing!"),
Some(cap) => {
let letters = cap.at(1).unwrap().to_string();
let count: usize = cap.at(2).unwrap().parse().unwrap();
let times: usize = cap.at(3).unwrap().parse().unwrap();
let subslice = &cap.at(4).unwrap()[0..count].to_string();
let therest = &cap.at(4).unwrap()[count..].to_string();
//println!("Found: {} - {},{}", cap.at(1).unwrap(), cap.at(2).unwrap(), cap.at(3).unwrap()),
println!("({},{}) - {}x {}", count, times, times, subslice);
return get_count(&letters) + times * get_count(subslice) + get_count(therest);
}
}
0
}
fn part_2(input: &String) {
println!("Part 2: Length is {}", get_count(input));
}
fn main() {
let mut file = File::open("./data").expect("could not open file");
let mut input = String::new();
file.read_to_string(&mut input).expect("could not read file");
//part_1(&input);
part_2(&input);
}