[2017][rust][5.x]

sorbet
Alpha Chen 7 years ago
parent 27051203b9
commit 6901d71abb

@ -1,6 +1,7 @@
use std::collections::HashSet; use std::collections::HashSet;
use failure::Error; use failure::Error;
#[allow(dead_code)]
pub fn solve(input: &str) -> Result<String, Error> { pub fn solve(input: &str) -> Result<String, Error> {
Ok(input Ok(input
.trim() .trim()
@ -11,11 +12,13 @@ pub fn solve(input: &str) -> Result<String, Error> {
.to_string()) .to_string())
} }
#[allow(dead_code)]
struct Passphrase { struct Passphrase {
words: String, words: String,
} }
impl Passphrase { impl Passphrase {
#[allow(dead_code)]
fn is_valid(&self) -> bool { fn is_valid(&self) -> bool {
let mut words = HashSet::new(); let mut words = HashSet::new();
!self.words !self.words

@ -0,0 +1,25 @@
use std::collections::HashMap;
use std::collections::hash_map::Entry;
use failure::*;
pub fn solve(input: &str) -> Result<String, Error> {
let mut jumps: HashMap<isize, isize> = input
.trim()
.lines()
.enumerate()
.map(|(i,x)| x.parse().map(|x| (i as isize, x)))
.collect::<Result<_,_>>()?;
let mut pc = 0;
let mut steps = 0;
while let Entry::Occupied(mut entry) = jumps.entry(pc) {
let offset = entry.get_mut();
pc += *offset;
if *offset >= 3 {
*offset -= 1;
} else {
*offset += 1;
}
steps += 1;
}
Ok(steps.to_string())
}

@ -12,6 +12,7 @@ mod day_01;
mod day_02; mod day_02;
mod day_03; mod day_03;
mod day_04; mod day_04;
mod day_05;
fn main() { fn main() {
if let Err(e) = run() { if let Err(e) = run() {
@ -23,7 +24,7 @@ fn run() -> Result<(), Error> {
let mut input = String::new(); let mut input = String::new();
io::stdin().read_to_string(&mut input)?; io::stdin().read_to_string(&mut input)?;
let solution = day_04::solve(&input)?; let solution = day_05::solve(&input)?;
println!("{}", solution); println!("{}", solution);
Ok(()) Ok(())

Loading…
Cancel
Save