2020-12-01 17:15:33 +01:00
|
|
|
use std::collections::HashSet;
|
2020-12-01 16:34:25 +01:00
|
|
|
use std::fmt::Write;
|
|
|
|
|
|
|
|
use aoc::{err, Result};
|
|
|
|
|
|
|
|
const INPUT: &str = include_str!("../input/day01.txt");
|
|
|
|
|
|
|
|
pub fn run() -> Result<String> {
|
|
|
|
let mut res = String::with_capacity(128);
|
|
|
|
|
|
|
|
writeln!(res, "part 1: {}", part1(INPUT)?)?;
|
2020-12-01 16:49:09 +01:00
|
|
|
writeln!(res, "part 2: {}", part2(INPUT)?)?;
|
2020-12-01 16:34:25 +01:00
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:15:33 +01:00
|
|
|
fn part1(input: &str) -> Result<i64> {
|
2020-12-01 16:34:25 +01:00
|
|
|
let entries = input
|
|
|
|
.lines()
|
2020-12-01 17:15:33 +01:00
|
|
|
.map(|line| line.parse::<i64>().map_err(|e| err!("{}", e)))
|
|
|
|
.collect::<Result<Vec<i64>>>()?;
|
2020-12-01 16:34:25 +01:00
|
|
|
|
2020-12-01 16:49:09 +01:00
|
|
|
let (a, b) = find_2020_2_sum(&entries)?;
|
2020-12-01 16:34:25 +01:00
|
|
|
|
|
|
|
Ok(a * b)
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:15:33 +01:00
|
|
|
fn part2(input: &str) -> Result<i64> {
|
2020-12-01 16:49:09 +01:00
|
|
|
let entries = input
|
|
|
|
.lines()
|
2020-12-01 17:15:33 +01:00
|
|
|
.map(|line| line.parse::<i64>().map_err(|e| err!("{}", e)))
|
|
|
|
.collect::<Result<Vec<i64>>>()?;
|
2020-12-01 16:49:09 +01:00
|
|
|
|
|
|
|
let (a, b, c) = find_2020_3_sum(&entries)?;
|
|
|
|
|
|
|
|
Ok(a * b * c)
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:15:33 +01:00
|
|
|
fn find_2020_2_sum(entries: &[i64]) -> Result<(i64, i64)> {
|
2020-12-01 17:54:44 +01:00
|
|
|
let set: HashSet<i64> = entries.iter().copied().collect();
|
|
|
|
|
2020-12-02 12:46:30 +01:00
|
|
|
for a in entries {
|
2020-12-01 17:54:44 +01:00
|
|
|
let b = 2020 - a;
|
|
|
|
|
|
|
|
if set.contains(&b) {
|
2020-12-02 12:46:30 +01:00
|
|
|
return Ok((*a, b));
|
2020-12-01 16:34:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(err!("couldn't find 2 elements that sum to 2020"))
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:15:33 +01:00
|
|
|
fn find_2020_3_sum(entries: &[i64]) -> Result<(i64, i64, i64)> {
|
|
|
|
let set: HashSet<i64> = entries.iter().copied().collect();
|
|
|
|
|
2020-12-01 16:49:09 +01:00
|
|
|
for i in 0..entries.len() {
|
|
|
|
for j in 0..entries.len() {
|
2020-12-01 17:15:33 +01:00
|
|
|
if i == j {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let a = entries[i];
|
|
|
|
let b = entries[j];
|
|
|
|
|
|
|
|
let c = 2020 - a - b;
|
|
|
|
|
|
|
|
if set.contains(&c) {
|
|
|
|
return Ok((a, b, c));
|
2020-12-01 16:49:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(err!("couldn't find 2 elements that sum to 2020"))
|
|
|
|
}
|
|
|
|
|
2020-12-01 16:34:25 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2020-12-01 17:15:33 +01:00
|
|
|
static PROVIDED: &[i64] = &[1721, 979, 366, 299, 675, 1456];
|
2020-12-01 16:49:09 +01:00
|
|
|
|
2020-12-01 16:34:25 +01:00
|
|
|
#[test]
|
|
|
|
fn part1_provided() {
|
2020-12-01 16:49:09 +01:00
|
|
|
let (a, b) = find_2020_2_sum(PROVIDED).unwrap();
|
|
|
|
assert_eq!(a + b, 2020);
|
2020-12-01 16:34:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part1_real() {
|
|
|
|
assert_eq!(part1(INPUT).unwrap(), 1014171);
|
|
|
|
}
|
2020-12-01 16:49:09 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part2_provided() {
|
|
|
|
let (a, b, c) = find_2020_3_sum(PROVIDED).unwrap();
|
|
|
|
assert_eq!(a + b + c, 2020);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn part2_real() {
|
|
|
|
assert_eq!(part2(INPUT).unwrap(), 46584630);
|
|
|
|
}
|
2020-12-01 16:34:25 +01:00
|
|
|
}
|