[rust] Don't use a Day trait

wip
Alpha Chen 9 years ago
parent e402732959
commit 7afc3ba714

@ -1,20 +1,8 @@
use std::io;
use std::str::Chars; use std::str::Chars;
use day::Day; pub fn solve(input: &str) -> i32 {
let elevator = Elevator::new(input.to_string());
pub struct Day01 { 1 + elevator.run().position(|f| f == -1).unwrap_or(0) as i32
elevator: Elevator,
}
impl Day for Day01 {
fn new(input: String) -> Day01 {
Day01 { elevator: Elevator::new(input) }
}
fn solve(&self) -> io::Result<i32> {
Ok(1 + self.elevator.run().position(|f| f == -1).unwrap_or(0) as i32)
}
} }
struct Elevator { struct Elevator {

@ -1,20 +1,6 @@
use std::io; pub fn solve(input: &str) -> i32 {
let presents = input.split("\n").map(|line| Present::new(line)).collect::<Vec<Present>>();
use day::Day; presents.iter().fold(0u32, |acc, present| acc + present.ribbon()) as i32
pub struct Day02 {
presents: Vec<Present>,
}
impl Day for Day02 {
fn new(input: String) -> Day02 {
let presents = input.split("\n").map(|line| Present::new(line));
Day02 { presents: presents.collect::<Vec<Present>>() }
}
fn solve(&self) -> io::Result<i32> {
Ok(self.presents.iter().fold(0u32, |acc, present| acc + present.ribbon()) as i32)
}
} }
struct Present { struct Present {

@ -1,25 +1,13 @@
use std::cmp::Eq; use std::cmp::Eq;
use std::collections::HashMap; use std::collections::HashMap;
use std::fmt; use std::fmt;
use std::io;
use std::ops::Add; use std::ops::Add;
use std::str::Chars; use std::str::Chars;
use day::Day; pub fn solve(input: &str) -> i32 {
pub struct Day03 {
directions: String,
}
impl Day for Day03 {
fn new(input: String) -> Day03 {
Day03 { directions: input }
}
fn solve(&self) -> io::Result<i32> {
let mut santa = String::new(); let mut santa = String::new();
let mut robo_santa = String::new(); let mut robo_santa = String::new();
let mut iter = self.directions.chars(); let mut iter = input.chars();
loop { loop {
match iter.next() { match iter.next() {
@ -35,8 +23,7 @@ impl Day for Day03 {
let mut houses: HashMap<Point, u32> = HashMap::new(); let mut houses: HashMap<Point, u32> = HashMap::new();
Santa::houses_visited(&santa, &mut houses); Santa::houses_visited(&santa, &mut houses);
Santa::houses_visited(&robo_santa, &mut houses); Santa::houses_visited(&robo_santa, &mut houses);
Ok(houses.len() as i32) houses.len() as i32
}
} }
struct Santa<'a> { struct Santa<'a> {

@ -1,22 +1,9 @@
use std::io;
use crypto::md5::Md5; use crypto::md5::Md5;
use crypto::digest::Digest; use crypto::digest::Digest;
use day::Day; pub fn solve(input: &str) -> i32 {
pub struct Day04 {
secret: String,
}
impl Day for Day04 {
fn new(input: String) -> Day04 {
Day04 { secret: input }
}
fn solve(&self) -> io::Result<i32> {
let mut md5 = Md5::new(); let mut md5 = Md5::new();
let key = self.secret.as_bytes(); let key = input.as_bytes();
let mut i = 0; let mut i = 0;
let mut out = [0; 16]; let mut out = [0; 16];
loop { loop {
@ -30,13 +17,11 @@ impl Day for Day04 {
} }
md5.reset(); md5.reset();
} }
Ok(i) i
}
} }
#[test] #[test]
#[ignore] #[ignore]
fn test_day04() { fn test_day04() {
let day = Day04::new("abcdef".to_string()); assert_eq!(609043, solve("abcdef"));
assert_eq!(609043, day.solve().unwrap());
} }

@ -1,18 +1,14 @@
use std::io; #![allow(dead_code)]
use regex::Regex; use regex::Regex;
use day::Day; pub fn solve(input: &str) -> i32 {
input.lines().filter(|&s| is_nice(&s.to_string())).count() as i32
pub struct Day05 {
input: String,
} }
#[allow(dead_code)]
impl Day05 {
fn is_nice(string: &String) -> bool { fn is_nice(string: &String) -> bool {
Day05::has_pair_of_two_letters(string) && has_pair_of_two_letters(string) &&
Day05::has_letter_sandwich(string) has_letter_sandwich(string)
} }
fn has_three_vowels(string: &String) -> bool { fn has_three_vowels(string: &String) -> bool {
@ -38,22 +34,11 @@ impl Day05 {
fn has_letter_sandwich(string: &String) -> bool { fn has_letter_sandwich(string: &String) -> bool {
string.as_bytes().windows(3).any(|win| win[0] == win[2]) string.as_bytes().windows(3).any(|win| win[0] == win[2])
} }
}
impl Day for Day05 {
fn new(input: String) -> Day05 {
Day05 { input: input }
}
fn solve(&self) -> io::Result<i32> {
Ok(self.input.lines().filter(|&s| Day05::is_nice(&s.to_string())).count() as i32)
}
}
#[test] #[test]
fn test_nice() { fn test_nice() {
assert!( Day05::is_nice(&"qjhvhtzxzqqjkmpb".to_string())); assert!( is_nice(&"qjhvhtzxzqqjkmpb".to_string()));
assert!( Day05::is_nice(&"xxyxx ".to_string())); assert!( is_nice(&"xxyxx ".to_string()));
assert!(!Day05::is_nice(&"uurcxstgmygtbstg ".to_string())); assert!(!is_nice(&"uurcxstgmygtbstg ".to_string()));
assert!(!Day05::is_nice(&"ieodomkazucvgmuy ".to_string())); assert!(!is_nice(&"ieodomkazucvgmuy ".to_string()));
} }

@ -1,23 +1,6 @@
use std::io;
use regex::Regex; use regex::Regex;
use day::Day; pub fn solve(input: &str) -> i32 {
pub struct Day06 {
input: String,
}
impl Day for Day06 {
fn new(input: String) -> Day06 {
Day06 { input: input }
}
fn solve(&self) -> io::Result<i32> {
Ok(solve(&self.input))
}
}
fn solve(input: &str) -> i32 {
let mut lg = LightGrid::new(); let mut lg = LightGrid::new();
for line in input.lines() { for line in input.lines() {

@ -1,17 +1,15 @@
extern crate crypto; extern crate crypto;
extern crate regex; extern crate regex;
macro_rules! import_day { macro_rules! pub_mod {
( $( $d:ident ),* ) => { ( $( $d:ident ),* ) => {
$( $(
pub use $d::*; pub mod $d;
mod $d;
)* )*
} }
} }
import_day! { pub_mod! {
day,
day_01, day_01,
day_02, day_02,
day_03, day_03,

@ -7,6 +7,5 @@ use advent_of_code::*;
fn main() { fn main() {
let mut input = String::new(); let mut input = String::new();
io::stdin().read_to_string(&mut input).ok(); io::stdin().read_to_string(&mut input).ok();
let day = Day06::new(input); println!("{}", day_06::solve(&input));
println!("{}", day.solve().unwrap());
} }

Loading…
Cancel
Save