2020: day01: quadratic part 2

This commit is contained in:
Antoine Martin 2020-12-01 17:15:33 +01:00
parent a948437488
commit 14756db40a

View file

@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::fmt::Write; use std::fmt::Write;
use aoc::{err, Result}; use aoc::{err, Result};
@ -13,29 +14,29 @@ pub fn run() -> Result<String> {
Ok(res) Ok(res)
} }
fn part1(input: &str) -> Result<u64> { fn part1(input: &str) -> Result<i64> {
let entries = input let entries = input
.lines() .lines()
.map(|line| line.parse::<u64>().map_err(|e| err!("{}", e))) .map(|line| line.parse::<i64>().map_err(|e| err!("{}", e)))
.collect::<Result<Vec<u64>>>()?; .collect::<Result<Vec<i64>>>()?;
let (a, b) = find_2020_2_sum(&entries)?; let (a, b) = find_2020_2_sum(&entries)?;
Ok(a * b) Ok(a * b)
} }
fn part2(input: &str) -> Result<u64> { fn part2(input: &str) -> Result<i64> {
let entries = input let entries = input
.lines() .lines()
.map(|line| line.parse::<u64>().map_err(|e| err!("{}", e))) .map(|line| line.parse::<i64>().map_err(|e| err!("{}", e)))
.collect::<Result<Vec<u64>>>()?; .collect::<Result<Vec<i64>>>()?;
let (a, b, c) = find_2020_3_sum(&entries)?; let (a, b, c) = find_2020_3_sum(&entries)?;
Ok(a * b * c) Ok(a * b * c)
} }
fn find_2020_2_sum(entries: &[u64]) -> Result<(u64, u64)> { fn find_2020_2_sum(entries: &[i64]) -> Result<(i64, i64)> {
for i in 0..entries.len() { for i in 0..entries.len() {
for j in 0..entries.len() { for j in 0..entries.len() {
if i == j { if i == j {
@ -50,24 +51,25 @@ fn find_2020_2_sum(entries: &[u64]) -> Result<(u64, u64)> {
Err(err!("couldn't find 2 elements that sum to 2020")) Err(err!("couldn't find 2 elements that sum to 2020"))
} }
fn find_2020_3_sum(entries: &[u64]) -> Result<(u64, u64, u64)> { fn find_2020_3_sum(entries: &[i64]) -> Result<(i64, i64, i64)> {
let set: HashSet<i64> = entries.iter().copied().collect();
for i in 0..entries.len() { for i in 0..entries.len() {
for j in 0..entries.len() { for j in 0..entries.len() {
for k in 0..entries.len() { if i == j {
if i == j || j == k || k == i {
continue; continue;
} }
let a = entries[i]; let a = entries[i];
let b = entries[j]; let b = entries[j];
let c = entries[k];
if a + b + c == 2020 { let c = 2020 - a - b;
if set.contains(&c) {
return Ok((a, b, c)); return Ok((a, b, c));
} }
} }
} }
}
Err(err!("couldn't find 2 elements that sum to 2020")) Err(err!("couldn't find 2 elements that sum to 2020"))
} }
@ -76,7 +78,7 @@ fn find_2020_3_sum(entries: &[u64]) -> Result<(u64, u64, u64)> {
mod tests { mod tests {
use super::*; use super::*;
static PROVIDED: &[u64] = &[1721, 979, 366, 299, 675, 1456]; static PROVIDED: &[i64] = &[1721, 979, 366, 299, 675, 1456];
#[test] #[test]
fn part1_provided() { fn part1_provided() {