Skip to content

Instantly share code, notes, and snippets.

@burnsauce
Created December 16, 2020 22:52
Show Gist options
  • Select an option

  • Save burnsauce/d1c1277db8592713e70ff0fb4370ec11 to your computer and use it in GitHub Desktop.

Select an option

Save burnsauce/d1c1277db8592713e70ff0fb4370ec11 to your computer and use it in GitHub Desktop.
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
use std::fs::File;
fn main() -> io::Result<()> {
let f = File::open("input.txt")?;
let reader = BufReader::new(f);
let mut adp = Vec::<usize>::new();
for line in reader.lines() {
let n: usize = line?.parse().unwrap();
adp.push(n);
}
adp.sort();
let part = 1;
if part == 0 {
let mut last = 0;
let mut three = 1;
let mut one = 0;
for i in adp {
//if count > 0 {
match i - last {
1 => one += 1,
2 => { },
3 => three += 1,
_ => break,
}
//}
last = i;
}
println!("{}", one * three);
} else {
println!("{}", validate(adp.iter()));
}
Ok(())
}
fn validate<'a, T: Clone + Iterator<Item = &'a usize>>(s: T) -> usize {
if valid(s.clone()) {
let mut ret = 1;
for r in s.clone() {
ret += validate(s.clone()
.enumerate()
.filter(|&(i, _x)| i != *r)
.map(|(_i, x)| x));
}
ret
} else {
0
}
}
fn valid<'a, T: Iterator<Item = &'a usize>>(s: T) -> bool {
let mut last = 0;
for i in s {
if *i - last > 3 { return false; }
last = *i;
}
true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment