[rust] Update Rust, run Clippy

wip
Alpha Chen 8 years ago
parent 1697560761
commit d9c69654e9

@ -1,4 +1,4 @@
require "letters" # require "letters"
distances = Hash.new {|h,k| h[k] = {} } distances = Hash.new {|h,k| h[k] = {} }
DATA.read.scan(/^(.*) to (.*) = (.*)$/).each do |from, to, distance| DATA.read.scan(/^(.*) to (.*) = (.*)$/).each do |from, to, distance|
distance = distance.to_i distance = distance.to_i
@ -8,31 +8,31 @@ end
locations = distances.keys locations = distances.keys
puts locations.permutation.map {|p| p.each_cons(2).map {|a,b| distances[a][b] }.inject(&:+) }.max puts locations.permutation.map {|p| p.each_cons(2).map {|a,b| distances[a][b] }.inject(&:+) }.max
__END__ __END__
Faerun to Tristram = 65 Faerun to Norrath = 129
Faerun to Tambi = 129 Faerun to Tristram = 58
Faerun to Norrath = 144 Faerun to AlphaCentauri = 13
Faerun to Snowdin = 71 Faerun to Arbre = 24
Faerun to Straylight = 137 Faerun to Snowdin = 60
Faerun to AlphaCentauri = 3 Faerun to Tambi = 71
Faerun to Arbre = 149 Faerun to Straylight = 67
Tristram to Tambi = 63 Norrath to Tristram = 142
Tristram to Norrath = 4 Norrath to AlphaCentauri = 15
Tristram to Snowdin = 105 Norrath to Arbre = 135
Tristram to Straylight = 125 Norrath to Snowdin = 75
Tristram to AlphaCentauri = 55 Norrath to Tambi = 82
Tristram to Arbre = 14 Norrath to Straylight = 54
Tambi to Norrath = 68 Tristram to AlphaCentauri = 118
Tambi to Snowdin = 52 Tristram to Arbre = 122
Tambi to Straylight = 65 Tristram to Snowdin = 103
Tambi to AlphaCentauri = 22 Tristram to Tambi = 49
Tambi to Arbre = 143 Tristram to Straylight = 97
Norrath to Snowdin = 8 AlphaCentauri to Arbre = 116
Norrath to Straylight = 23 AlphaCentauri to Snowdin = 12
Norrath to AlphaCentauri = 136 AlphaCentauri to Tambi = 18
Norrath to Arbre = 115 AlphaCentauri to Straylight = 91
Snowdin to Straylight = 101 Arbre to Snowdin = 129
Snowdin to AlphaCentauri = 84 Arbre to Tambi = 53
Snowdin to Arbre = 96 Arbre to Straylight = 40
Straylight to AlphaCentauri = 107 Snowdin to Tambi = 15
Straylight to Arbre = 14 Snowdin to Straylight = 99
AlphaCentauri to Arbre = 46 Tambi to Straylight = 70

17
rust/Cargo.lock generated

@ -127,3 +127,20 @@ name = "winapi-build"
version = "0.1.1" version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum advapi32-sys 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "307c92332867e586720c0222ee9d890bbe8431711efed8a1b06bc5b40fc66bd7"
"checksum aho-corasick 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8f36f237c490deb976b38aca6369182dceb5a7af249aabf41c0ba5a964bac5ed"
"checksum gcc 0.3.21 (registry+https://github.com/rust-lang/crates.io-index)" = "ca10e3e1f1c8278047da19b94dc17c4397861150d5fbcea052eedb1d9847d356"
"checksum kernel32-sys 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b5b5e7edf375e6d26243bde172f1d5ed1446f4a766fc9b7006e1fd27258243f1"
"checksum libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "e32a70cf75e5846d53a673923498228bbec6a8624708a9ea5645f075d6276122"
"checksum libc 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "10569e57695cc2c91ca4214357907649c9e242dc822c9ae623d0e0b0d68aa4d9"
"checksum memchr 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "dc66b0957bf6ae6590681ceac49b0df16823d43037d49aaf2ee658d483af30ab"
"checksum permutohedron 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "abf78a1e8b52782de92fc4f361362a62bcf5fd5718b5432b48cb381485740b83"
"checksum rand 0.3.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5eee40bdf3d293e1648490ab47e5471d9ab3e455e6b0bd48e558c454be4a015e"
"checksum regex 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "e0940ad6bf8abf79e3210809a6a49e199fc8e00b7deafc0d9394157f56f5401e"
"checksum regex-syntax 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "fcf1e805b0a23c845be2a303136d840a1511284727bc1f1fc32d079552ef901f"
"checksum rust-crypto 0.2.34 (registry+https://github.com/rust-lang/crates.io-index)" = "b8672a8eb8db93d0938972e391159ba66912b415285ee5cf0ebe732df9e53b70"
"checksum rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "1a48546a64cae47d06885e9bccadb99d0547d877a94c5167fa451ea33a484456"
"checksum time 0.1.34 (registry+https://github.com/rust-lang/crates.io-index)" = "8c4aeaa1c95974f5763c3a5ac0db95a19793589bcea5d22e161b5587e3aad029"
"checksum winapi 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "dc3583688b861fcd83c2823d37cf2cd2446c233dd7ba3f97884d1a7302817537"
"checksum winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"

@ -1,28 +1,28 @@
Faerun to Tristram = 65 Faerun to Norrath = 129
Faerun to Tambi = 129 Faerun to Tristram = 58
Faerun to Norrath = 144 Faerun to AlphaCentauri = 13
Faerun to Snowdin = 71 Faerun to Arbre = 24
Faerun to Straylight = 137 Faerun to Snowdin = 60
Faerun to AlphaCentauri = 3 Faerun to Tambi = 71
Faerun to Arbre = 149 Faerun to Straylight = 67
Tristram to Tambi = 63 Norrath to Tristram = 142
Tristram to Norrath = 4 Norrath to AlphaCentauri = 15
Tristram to Snowdin = 105 Norrath to Arbre = 135
Tristram to Straylight = 125 Norrath to Snowdin = 75
Tristram to AlphaCentauri = 55 Norrath to Tambi = 82
Tristram to Arbre = 14 Norrath to Straylight = 54
Tambi to Norrath = 68 Tristram to AlphaCentauri = 118
Tambi to Snowdin = 52 Tristram to Arbre = 122
Tambi to Straylight = 65 Tristram to Snowdin = 103
Tambi to AlphaCentauri = 22 Tristram to Tambi = 49
Tambi to Arbre = 143 Tristram to Straylight = 97
Norrath to Snowdin = 8 AlphaCentauri to Arbre = 116
Norrath to Straylight = 23 AlphaCentauri to Snowdin = 12
Norrath to AlphaCentauri = 136 AlphaCentauri to Tambi = 18
Norrath to Arbre = 115 AlphaCentauri to Straylight = 91
Snowdin to Straylight = 101 Arbre to Snowdin = 129
Snowdin to AlphaCentauri = 84 Arbre to Tambi = 53
Snowdin to Arbre = 96 Arbre to Straylight = 40
Straylight to AlphaCentauri = 107 Snowdin to Tambi = 15
Straylight to Arbre = 14 Snowdin to Straylight = 99
AlphaCentauri to Arbre = 46 Tambi to Straylight = 70

@ -1,5 +1,6 @@
pub fn solve(input: &str) -> i32 { pub fn solve(input: &str) -> i32 {
let presents = input.split("\n").map(|line| Present::new(line)).collect::<Vec<Present>>(); let presents =
input.split('\n').map(|line| Present::new(line)).collect::<Vec<Present>>();
presents.iter().fold(0u32, |acc, present| acc + present.ribbon()) as i32 presents.iter().fold(0u32, |acc, present| acc + present.ribbon()) as i32
} }
@ -12,10 +13,14 @@ struct Present {
#[allow(dead_code)] #[allow(dead_code)]
impl Present { impl Present {
fn new(input: &str) -> Self { fn new(input: &str) -> Self {
let dimensions = input.split("x") let dimensions = input.split('x')
.map(|d| d.parse::<u32>().unwrap()) .map(|d| d.parse::<u32>().unwrap())
.collect::<Vec<u32>>(); .collect::<Vec<u32>>();
Present { length: dimensions[0], width: dimensions[1], height: dimensions[2] } Present {
length: dimensions[0],
width: dimensions[1],
height: dimensions[2],
}
} }
fn wrapping_paper(&self) -> u32 { fn wrapping_paper(&self) -> u32 {
@ -37,7 +42,10 @@ impl Present {
fn perimeters(&self) -> Vec<u32> { fn perimeters(&self) -> Vec<u32> {
vec![self.length + self.width, vec![self.length + self.width,
self.width + self.height, self.width + self.height,
self.height + self.length ].iter().map(|&x| 2 * x).collect() self.height + self.length]
.iter()
.map(|&x| 2 * x)
.collect()
} }
fn volume(&self) -> u32 { fn volume(&self) -> u32 {
@ -45,7 +53,9 @@ impl Present {
} }
fn side_areas(&self) -> Vec<u32> { fn side_areas(&self) -> Vec<u32> {
vec![self.length * self.width, self.width * self.height, self.height * self.length] vec![self.length * self.width,
self.width * self.height,
self.height * self.length]
} }
} }

@ -1,4 +1,3 @@
use std::cmp::Eq;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::ops::Add; use std::ops::Add;
@ -11,11 +10,11 @@ pub fn solve(input: &str) -> i32 {
loop { loop {
match iter.next() { match iter.next() {
Some(c) => { santa.push(c) }, Some(c) => santa.push(c),
None => break, None => break,
} }
match iter.next() { match iter.next() {
Some(c) => { robo_santa.push(c) }, Some(c) => robo_santa.push(c),
None => break, None => break,
} }
} }
@ -103,7 +102,7 @@ fn test_three() {
assert_eq!(&5, houses.get(&Point { x: 0, y: 1 }).unwrap()); assert_eq!(&5, houses.get(&Point { x: 0, y: 1 }).unwrap());
} }
#[derive(Clone,Copy,Debug,Eq,Hash)] #[derive(Clone,Copy,Debug,Eq,Hash,PartialEq)]
struct Point { struct Point {
x: i32, x: i32,
y: i32, y: i32,
@ -132,12 +131,6 @@ impl fmt::Display for Point {
} }
} }
impl PartialEq for Point {
fn eq(&self, other: &Self) -> bool {
self.x == other.x && self.y == other.y
}
}
#[test] #[test]
fn test_point() { fn test_point() {
let point = Point { x: 10, y: -10 }; let point = Point { x: 10, y: -10 };

@ -6,32 +6,31 @@ pub fn solve(input: &str) -> i32 {
input.lines().filter(|&s| is_nice(&s.to_string())).count() as i32 input.lines().filter(|&s| is_nice(&s.to_string())).count() as i32
} }
fn is_nice(string: &String) -> bool { fn is_nice(string: &str) -> bool {
has_pair_of_two_letters(string) && has_pair_of_two_letters(string) && has_letter_sandwich(string)
has_letter_sandwich(string)
} }
fn has_three_vowels(string: &String) -> bool { fn has_three_vowels(string: &str) -> bool {
let three_vowels = Regex::new(r"[aeiou].*[aeiou].*[aeiou]").unwrap(); let three_vowels = Regex::new(r"[aeiou].*[aeiou].*[aeiou]").unwrap();
three_vowels.is_match(string) three_vowels.is_match(string)
} }
fn has_double_letters(string: &String) -> bool { fn has_double_letters(string: &str) -> bool {
string.as_bytes().windows(2).any(|win| win[0] == win[1]) string.as_bytes().windows(2).any(|win| win[0] == win[1])
} }
fn has_no_substrings(string: &String) -> bool { fn has_no_substrings(string: &str) -> bool {
!vec!["ab", "cd", "pq", "xy"].iter().any(|&s| string.contains(s)) !vec!["ab", "cd", "pq", "xy"].iter().any(|&s| string.contains(s))
} }
fn has_pair_of_two_letters(string: &String) -> bool { fn has_pair_of_two_letters(string: &str) -> bool {
string.as_bytes().windows(2).any(|win| { string.as_bytes().windows(2).any(|win| {
let s = String::from_utf8(win.to_vec()).unwrap(); let s = String::from_utf8(win.to_vec()).unwrap();
string.split(&s).count() > 2 string.split(&s).count() > 2
}) })
} }
fn has_letter_sandwich(string: &String) -> bool { fn has_letter_sandwich(string: &str) -> bool {
string.as_bytes().windows(3).any(|win| win[0] == win[2]) string.as_bytes().windows(3).any(|win| win[0] == win[2])
} }

@ -7,7 +7,8 @@ fn test_day_07() {
x AND y -> d x AND y -> d
x OR y -> e x OR y -> e
x LSHIFT 2 -> f x LSHIFT 2 -> f
y RSHIFT 2 -> g y \
RSHIFT 2 -> g
NOT x -> h NOT x -> h
NOT y -> i"; NOT y -> i";
@ -33,7 +34,8 @@ fn test_circuit() {
assert_eq!(Signal::Value(123), circuit.connections[&Wire("x")]); assert_eq!(Signal::Value(123), circuit.connections[&Wire("x")]);
assert_eq!(Signal::Wire(Wire("x")), circuit.connections[&Wire("y")]); assert_eq!(Signal::Wire(Wire("x")), circuit.connections[&Wire("y")]);
assert_eq!(Signal::Gate(Gate::Not(Wire("x"))), circuit.connections[&Wire("h")]); assert_eq!(Signal::Gate(Gate::Not(Wire("x"))),
circuit.connections[&Wire("h")]);
assert_eq!(123, circuit.signal_on("x")); assert_eq!(123, circuit.signal_on("x"));
assert_eq!(123, circuit.signal_on("y")); assert_eq!(123, circuit.signal_on("y"));
@ -51,7 +53,8 @@ impl<'a> Circuit<'a> {
for line in input.lines() for line in input.lines()
.map(|line| line.split(" -> ").collect::<Vec<_>>()) { .map(|line| line.split(" -> ").collect::<Vec<_>>()) {
let wire = Wire(line.last().expect("wire")); let wire = Wire(line.last().expect("wire"));
let signal = Signal::new(line.first().expect("line").split(" ").collect()); let signal =
Signal::new(line.first().expect("line").split(' ').collect());
connections.insert(wire, signal); connections.insert(wire, signal);
} }
@ -62,14 +65,20 @@ impl<'a> Circuit<'a> {
// TODO Is there a way for this to not be mutable? // TODO Is there a way for this to not be mutable?
fn signal_on(&mut self, wire_id: &'a str) -> u16 { fn signal_on(&mut self, wire_id: &'a str) -> u16 {
let wire = Wire(wire_id); let wire = Wire(wire_id);
let value = match self.connections.get(&wire).cloned() let value = match self.connections
.get(&wire)
.cloned()
.unwrap_or_else(|| Signal::Value(wire_id.parse::<u16>().unwrap())) { .unwrap_or_else(|| Signal::Value(wire_id.parse::<u16>().unwrap())) {
Signal::Value(value) => return value, Signal::Value(value) => return value,
Signal::Wire(Wire(input_wire_id)) => self.signal_on(input_wire_id), Signal::Wire(Wire(input_wire_id)) => self.signal_on(input_wire_id),
Signal::Gate(Gate::And(Wire(a), Wire(b))) => self.signal_on(a) & self.signal_on(b), Signal::Gate(Gate::And(Wire(a), Wire(b))) => {
self.signal_on(a) & self.signal_on(b)
}
Signal::Gate(Gate::LeftShift(Wire(a), i)) => self.signal_on(a) << i, Signal::Gate(Gate::LeftShift(Wire(a), i)) => self.signal_on(a) << i,
Signal::Gate(Gate::Not(Wire(a))) => !self.signal_on(a), Signal::Gate(Gate::Not(Wire(a))) => !self.signal_on(a),
Signal::Gate(Gate::Or(Wire(a), Wire(b))) => self.signal_on(a) | self.signal_on(b), Signal::Gate(Gate::Or(Wire(a), Wire(b))) => {
self.signal_on(a) | self.signal_on(b)
}
Signal::Gate(Gate::RightShift(Wire(a), i)) => self.signal_on(a) >> i, Signal::Gate(Gate::RightShift(Wire(a), i)) => self.signal_on(a) >> i,
}; };
self.connections.insert(wire, Signal::Value(value)); self.connections.insert(wire, Signal::Value(value));
@ -89,12 +98,18 @@ enum Signal<'a> {
impl<'a> Signal<'a> { impl<'a> Signal<'a> {
fn new(input: Vec<&str>) -> Signal { fn new(input: Vec<&str>) -> Signal {
match &input[..] { match input[..] {
[a, "AND", b] => Signal::Gate(Gate::And(Wire(a), Wire(b))), [a, "AND", b] => Signal::Gate(Gate::And(Wire(a), Wire(b))),
["NOT", a] => Signal::Gate(Gate::Not(Wire(a))), ["NOT", a] => Signal::Gate(Gate::Not(Wire(a))),
[a, "OR", b] => Signal::Gate(Gate::Or(Wire(a), Wire(b))), [a, "OR", b] => Signal::Gate(Gate::Or(Wire(a), Wire(b))),
[a, "LSHIFT", b] => Signal::Gate(Gate::LeftShift(Wire(a), b.parse::<u16>().expect("lshift"))), [a, "LSHIFT", b] => {
[a, "RSHIFT", b] => Signal::Gate(Gate::RightShift(Wire(a), b.parse::<u16>().expect("rshift"))), Signal::Gate(Gate::LeftShift(Wire(a),
b.parse::<u16>().expect("lshift")))
}
[a, "RSHIFT", b] => {
Signal::Gate(Gate::RightShift(Wire(a),
b.parse::<u16>().expect("rshift")))
}
[value] => { [value] => {
match value.parse::<u16>() { match value.parse::<u16>() {
Ok(value) => Signal::Value(value), Ok(value) => Signal::Value(value),

@ -24,21 +24,32 @@ fn test_decode() {
fn decode(string: &str) -> String { fn decode(string: &str) -> String {
let mut out = "".to_owned(); let mut out = "".to_owned();
let mut chars = string.chars().map(|c| Some(c)).collect::<Vec<_>>(); let mut chars = string.chars().map(Some).collect::<Vec<_>>();
chars.append(&mut vec![None, None, None]); chars.append(&mut vec![None, None, None]);
let mut it = chars.windows(4); let mut it = chars.windows(4);
loop { loop {
match it.next() { match it.next() {
Some([Some('"'), None, None, None]) => break, Some(&[Some('"'), None, None, None]) => break,
Some([Some('"'), _, _, _]) => {}, Some(&[Some('"'), _, _, _]) => {}
Some([Some('\\'), Some('\\'), _, _]) => { it.next(); out.push('\\'); }, Some(&[Some('\\'), Some('\\'), _, _]) => {
Some([Some('\\'), Some('"'), _, _]) => { it.next(); out.push('"'); }, it.next();
Some([Some('\\'), Some('x'), Some(a), Some(b)]) => { out.push('\\');
it.next(); it.next(); it.next(); }
let c = (a.to_digit(16).unwrap_or(0) << 4) + (b.to_digit(16).unwrap_or(0)); Some(&[Some('\\'), Some('"'), _, _]) => {
it.next();
out.push('"');
}
Some(&[Some('\\'), Some('x'), Some(a), Some(b)]) => {
it.next();
it.next();
it.next();
let c = (a.to_digit(16).unwrap_or(0) << 4) +
(b.to_digit(16).unwrap_or(0));
out.push(c as u8 as char); out.push(c as u8 as char);
}, }
Some([Some(c), _, _, _]) => { out.push(c); }, Some(&[Some(c), _, _, _]) => {
out.push(c);
}
Some(x) => panic!("{:?}", x), Some(x) => panic!("{:?}", x),
None => panic!(""), None => panic!(""),
}; };
@ -56,5 +67,6 @@ fn test_encode() {
} }
fn encode(string: &str) -> String { fn encode(string: &str) -> String {
format!(r#""{}""#, string.replace(r#"\"#, r#"\\"#).replace(r#"""#, r#"\""#)) format!(r#""{}""#,
string.replace(r#"\"#, r#"\\"#).replace(r#"""#, r#"\""#))
} }

@ -7,7 +7,8 @@ use permutohedron::Heap;
fn test_day_09() { fn test_day_09() {
let input = "London to Dublin = 464 let input = "London to Dublin = 464
London to Belfast = 518 London to Belfast = 518
Dublin to Belfast = 141"; Dublin to \
Belfast = 141";
let day = Day09::new(input); let day = Day09::new(input);
assert_eq!(464, assert_eq!(464,
@ -54,11 +55,15 @@ impl<'a> Day09<'a> {
distances.insert(LocationPair(a, b), distance); distances.insert(LocationPair(a, b), distance);
} }
Day09 { locations: locations, distances: distances } Day09 {
locations: locations,
distances: distances,
}
} }
fn routes(&self) -> Vec<Route> { fn routes(&self) -> Vec<Route> {
let mut locations = self.locations.iter() let mut locations = self.locations
.iter()
.cloned() .cloned()
.collect::<Vec<Location>>(); .collect::<Vec<Location>>();
let heap = Heap::new(&mut locations); let heap = Heap::new(&mut locations);
@ -69,7 +74,10 @@ impl<'a> Day09<'a> {
let distance = locations.windows(2) let distance = locations.windows(2)
.map(|w| self.distances[&LocationPair(w[0], w[1])]) .map(|w| self.distances[&LocationPair(w[0], w[1])])
.fold(0, |sum, dist| sum + dist); .fold(0, |sum, dist| sum + dist);
Route { locations: locations, distance: distance } Route {
locations: locations,
distance: distance,
}
} }
} }
@ -79,15 +87,9 @@ struct Route<'a> {
distance: usize, distance: usize,
} }
#[derive(Clone,Copy,Debug,Eq,PartialEq,PartialOrd,Ord)] #[derive(Clone,Copy,Debug,Eq,Hash,PartialEq,PartialOrd,Ord)]
struct Location<'a>(&'a str); struct Location<'a>(&'a str);
impl<'a> Hash for Location<'a> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
#[derive(Debug,Eq)] #[derive(Debug,Eq)]
struct LocationPair<'a>(Location<'a>, Location<'a>); struct LocationPair<'a>(Location<'a>, Location<'a>);

Loading…
Cancel
Save