forked from akramer/advent
alternative
This commit is contained in:
parent
338920c64f
commit
b228e7f80a
84
src/bin/1.rs
84
src/bin/1.rs
@ -36,51 +36,65 @@ Of course, your expense report is much larger. Find the two entries that sum to
|
||||
do you get if you multiply them together?
|
||||
*/
|
||||
|
||||
use std::fs::{File};
|
||||
use std::collections::HashSet;
|
||||
use std::io::{BufRead, BufReader};
|
||||
use anyhow::Result;
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader};
|
||||
|
||||
fn find_triple(target: i32, list: &Vec<i32>, set: &HashSet<i32>) -> i32 {
|
||||
for i in list {
|
||||
let product = find_pair(target - i, list, set);
|
||||
if product > 0 {
|
||||
return product * i;
|
||||
}
|
||||
}
|
||||
0
|
||||
struct Entries {
|
||||
list: Vec<i32>,
|
||||
set: HashSet<i32>,
|
||||
}
|
||||
|
||||
fn find_pair(target: i32, list: &Vec<i32>, set: &HashSet<i32>) -> i32 {
|
||||
for i in list {
|
||||
if i > &target {
|
||||
break
|
||||
}
|
||||
let val = target - i;
|
||||
if set.contains(&val) {
|
||||
return i * val;
|
||||
impl Entries {
|
||||
fn find_triple(&self, target: i32) -> i32 {
|
||||
for i in self.list.iter().copied() {
|
||||
let product = self.find_pair(target - i);
|
||||
if product > 0 {
|
||||
return product * i;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
fn find_pair(&self, target: i32) -> i32 {
|
||||
for i in self.list.iter().copied() {
|
||||
if i > target {
|
||||
break;
|
||||
}
|
||||
let val = target - i;
|
||||
if self.set.contains(&val) {
|
||||
return i * val;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
}
|
||||
|
||||
impl std::iter::FromIterator<i32> for Entries {
|
||||
fn from_iter<T>(iter: T) -> Self
|
||||
where
|
||||
T: IntoIterator<Item = i32>,
|
||||
{
|
||||
let mut list = Vec::new();
|
||||
let mut set = HashSet::new();
|
||||
for value in iter {
|
||||
list.push(value);
|
||||
set.insert(value);
|
||||
}
|
||||
list.sort();
|
||||
Entries { list, set }
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
//let contents = fs::read_to_string("1.input").expect("Couldn't read \"1.input\"");
|
||||
let file = File::open("1.input")?;
|
||||
let reader = BufReader::new(file);
|
||||
let entries: Entries = BufReader::new(File::open("1.input")?)
|
||||
.lines()
|
||||
.filter_map(|line| line.ok()?.parse().ok())
|
||||
.collect();
|
||||
|
||||
let mut list: Vec<i32> = Vec::new();
|
||||
let mut set: HashSet<i32> = HashSet::new();
|
||||
|
||||
for line in reader.lines() {
|
||||
let value = line?.parse::<i32>()?;
|
||||
list.push(value);
|
||||
set.insert(value);
|
||||
}
|
||||
|
||||
list.sort();
|
||||
|
||||
println!("Pair is {}", find_pair(2020, &list, &set));
|
||||
println!("Triple is {}", find_triple(2020, &list, &set));
|
||||
println!("Pair is {}", entries.find_pair(2020));
|
||||
println!("Triple is {}", entries.find_triple(2020));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user