Skip to content

Instantly share code, notes, and snippets.

@tbelaire
Created December 16, 2017 18:22
Show Gist options
  • Save tbelaire/0d3f2b85161679f6fa77910b8c4d6378 to your computer and use it in GitHub Desktop.
Save tbelaire/0d3f2b85161679f6fa77910b8c4d6378 to your computer and use it in GitHub Desktop.
fn main() {
println!("Hello, world!");
}
fn splitit(s:&str) -> Vec<i32> {
s.chars().filter_map(|c| c.to_digit(10).map(|x| x as i32)).collect()
}
fn doit(v:Vec<i32>) -> i32 {
assert!(v.len() > 2);
let first = v.iter().cycle();
let second = v.iter().cycle().skip(1);
let combined = first.zip(second).take(v.len());
let filtered = combined.filter( |&(a,b)| a == b );
filtered.map(|(a, _)| a).sum()
}
fn doit2(v:Vec<i32>) -> i32 {
assert!(v.len() > 2);
let first = v.iter().cycle();
let second = v.iter().cycle().skip(v.len()/2);
let combined = first.zip(second).take(v.len());
let filtered = combined.filter( |&(a,b)| a == b );
filtered.map(|(a, _)| a).sum()
}
#[test]
fn test_splitit () {
assert_eq!(splitit("1111"), vec![1,1,1,1]);
}
#[test]
fn test_doit() {
assert_eq!(doit(vec![1,1,1,1]), 4);
assert_eq!(doit(vec![1,1,2,2]), 3);
assert_eq!(doit(vec![1,2,3,4]), 0);
assert_eq!(doit(vec![9,1,2,1,2,1,2,9]), 9);
}
#[test]
fn test_real() {
assert_eq!(doit(splitit(include_str!("input.txt"))), 1141);
}
#[test]
fn test_doit2() {
assert_eq!(doit2(vec![1,1,1,1]), 4);
assert_eq!(doit2(vec![1,2,1,2]), 6);
assert_eq!(doit2(vec![1,2,3,4]), 0);
assert_eq!(doit2(vec![1,2,3,4,2,5]), 4);
assert_eq!(doit2(vec![1,2,3,1,2,3]), 12);
}
#[test]
fn test_real2() {
assert_eq!(doit2(splitit(include_str!("input.txt"))), 950);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment