Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 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?
|
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 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 {
|
struct Entries {
|
||||||
for i in list {
|
list: Vec<i32>,
|
||||||
let product = find_pair(target - i, list, set);
|
set: HashSet<i32>,
|
||||||
if product > 0 {
|
|
||||||
return product * i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_pair(target: i32, list: &Vec<i32>, set: &HashSet<i32>) -> i32 {
|
impl Entries {
|
||||||
for i in list {
|
fn find_triple(&self, target: i32) -> i32 {
|
||||||
if i > &target {
|
for i in self.list.iter().copied() {
|
||||||
break
|
let product = self.find_pair(target - i);
|
||||||
}
|
if product > 0 {
|
||||||
let val = target - i;
|
return product * i;
|
||||||
if set.contains(&val) {
|
}
|
||||||
return i * val;
|
|
||||||
}
|
}
|
||||||
|
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<()> {
|
fn main() -> Result<()> {
|
||||||
//let contents = fs::read_to_string("1.input").expect("Couldn't read \"1.input\"");
|
//let contents = fs::read_to_string("1.input").expect("Couldn't read \"1.input\"");
|
||||||
let file = File::open("1.input")?;
|
let entries: Entries = BufReader::new(File::open("1.input")?)
|
||||||
let reader = BufReader::new(file);
|
.lines()
|
||||||
|
.filter_map(|line| line.ok()?.parse().ok())
|
||||||
|
.collect();
|
||||||
|
|
||||||
let mut list: Vec<i32> = Vec::new();
|
println!("Pair is {}", entries.find_pair(2020));
|
||||||
let mut set: HashSet<i32> = HashSet::new();
|
println!("Triple is {}", entries.find_triple(2020));
|
||||||
|
|
||||||
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));
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user