Second huge commit

This commit is contained in:
Russell Champoux committed 2016-12-05 17:37:26 -05:00
1 parent ef848440e5
commit e91f81a898
10 files changed
+387

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
[root]
name = "day01"
version = "0.1.0"
+4
View File
@@ -0,0 +1,4 @@
[root]
name = "day03"
version = "0.1.0"
+6
View File
@@ -0,0 +1,6 @@
[package]
name = "day03"
version = "0.1.0"
authors = ["~krowbar"]
[dependencies]
+27
View File
@@ -0,0 +1,27 @@
use std::io::Read;
use std::fs::File;
fn part_1(input: &String) {
let total = input.lines()
.map(|l| l
.trim()
.split_whitespace()
.map(|v| v.parse::<i32>().unwrap())
.collect::<Vec<_>>())
.filter(|v| v.iter().fold(0, |sum, x| sum + x) > 2 * v.iter().max().unwrap() )
.collect::<Vec<_>>()
.len();
println!("Part 1: Total was {}", total);
}
fn part_2(input: &String) {
;
}
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);
}
+98
View File
@@ -0,0 +1,98 @@
[root]
name = "day04"
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 = "day04"
version = "0.1.0"
authors = ["~krowbar"]
[dependencies]
regex = "0.1"
+74
View File
@@ -0,0 +1,74 @@
extern crate regex;
use std::io::Read;
use std::fs::File;
use regex::Regex;
use std::collections::HashMap;
///// THIS IS NOT WORKING RIGHT. :(
// fn get_lines(input: &String) {
// let lines: Vec<(&String, i32, &String)> = vec![];
// lines.push(("Foo", 42, "bar"));
// return lines;
// }
// fn part_1(input: &Vec<(String, i32, String)>) {
// println!("Started part 1");
// }
#[derive(Debug)]
struct Room {
name: String,
sector: i32,
check: String,
}
fn part_1(input: &String) {
let re = Regex::new(r"([a-z\\-]+)-([:digit:]+)\[([:lower:]+)\]").unwrap();
let sum = 0;
for cap in re.captures_iter(input) {
let room = Room { name: String::from(cap.at(1).unwrap_or("")),
sector: cap.at(2).unwrap_or("0").parse().unwrap_or(0),
check: String::from(cap.at(3).unwrap_or("")),
};
let mut charMap: HashMap<char, i32> = HashMap::new();
for ch in room.name.replace("-", "").chars() {
let l = charMap.entry(ch).or_insert(0);
*l += 1;
}
println!("{:?}", charMap);
//println!("{:?}", room);
//let mut letters: Vec<char> = .collect();
//println!("{:?}", letters.sort());
}
println!("Part 1: Total sum of valid rooms is {}", sum);
// let sum = input
// .lines()
// .map(|l|
// re.captures(l).unwrap()
// //.split(&re)
// //.map(|&v| Room { name: v[0] + v[1] + v[2], sector: v[3].parse::<i32>().unwrap(), checksum: v[4] })
// //.collect::<Vec<_>>()
// //.turn into collection of items
// //.collect
// )
// .map(|c| c.at(0))
// .collect::<Vec<_>>()
// // //.filter out decoy rooms
// // //.collect
// // //.fold and sum the sectors
// // //.unwrap?
// .len();
// println!("Part 1: Sum was {}", sum);
}
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);
}
+81
View File
@@ -0,0 +1,81 @@
[root]
name = "day05"
version = "0.1.0"
dependencies = [
"rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "gcc"
version = "0.3.39"
source = "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 = "rand"
version = "0.3.15"
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 = "rust-crypto"
version = "0.2.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.39 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rustc-serialize"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "time"
version = "0.1.35"
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)",
"winapi 0.2.8 (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 gcc 0.3.39 (registry+https://github.com/rust-lang/crates.io-index)" = "771e4a97ff6f237cf0f7d5f5102f6e28bb9743814b6198d684da5c58b76c11e0"
"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 rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
"checksum rust-crypto 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a"
"checksum rustc-serialize 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)" = "bff9fc1c79f2dec76b253273d07682e94a978bd8f132ded071188122b2af9818"
"checksum time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)" = "3c7ec6d62a20df54e07ab3b78b9a3932972f4b7981de295563686849eb3989af"
"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 = "day05"
version = "0.1.0"
authors = ["~krowbar"]
[dependencies]
rust-crypto = "^0.2"
+79
View File
@@ -0,0 +1,79 @@
extern crate crypto;
use crypto::md5::Md5;
use crypto::digest::Digest;
//This was mostly stolen from a github gist that I found while looking for a Rust MD5 library
fn part_1() {
let mut hasher = Md5::new();
let key = "reyedfim".as_bytes();
let mut answer: Vec<u8> = vec![];
let mut count = 8;
for i in 0..std::u64::MAX {
hasher.input(key);
hasher.input(i.to_string().as_bytes());
let mut output = [0; 16]; //represents MD5 string
hasher.result(&mut output);
//check the first two and a half bytes of the output
let first_five = output[0] as i32 + output[1] as i32 + (output[2] >> 4) as i32;
if first_five == 0 {
let output_str: String = output.iter().map(|&c| format!("{:x}{:x}", c >> 4, c)).collect();
println!("hashed {} to get {}", i, output_str);
answer.push(output[2]);
match count > 1 {
true => count -= 1,
false => break,
}
}
hasher.reset();
}
let answer_str: String = answer.iter().map(|&c| format!("{:x}", c)).collect();
println!("Part 1: Answer is {}", answer_str);
}
fn part_2() {
let mut hasher = Md5::new();
let key = "reyedfim".as_bytes();
let mut answer = [0; 8];
let mut answer_mask = [true; 8];
for i in 0..std::u64::MAX {
hasher.input(key);
hasher.input(i.to_string().as_bytes());
let mut output = [0; 16]; //represents MD5 string
hasher.result(&mut output);
//check the first two and a half bytes of the output
let first_five = output[0] as i32 + output[1] as i32 + (output[2] >> 4) as i32;
if first_five == 0 {
let output_str: String = output.iter().map(|&c| format!("{:x}{:x}", c >> 4, c)).collect();
println!("hashed {} to get {}", i, output_str);
//answer.push(output[2]);
let idx = output[2] as usize;
let val = output[3] >> 4;
if idx < 8 && answer_mask[idx] {
answer[idx] = val;
answer_mask[idx] = false; //we filled this slot
println!("Values are {} and {}", idx, val);
match answer_mask.iter().fold(false, |f, &i| f || i) {
true => {}, //keep going
false => break, //we're all done here
}
}
}
hasher.reset();
}
let answer_str: String = answer.iter().map(|&c| format!("{:x}", c)).collect();
println!("Part 1: Answer is {}", answer_str);
}
fn main() {
//part_1();
part_2();
}