Day 01 - Report Repair

searchtwo-sum

Language: Rust

Problem https://adventofcode.com/2020/day/1


Part 1: A list of numbers. Find two that sum to 2020 and return their product. The input is small so a double loop is fine:

for i in &numbers {
    for j in &numbers {
        if i + j == 2020 {
            return (i * j).to_string();
        }
    }
}

Part 2: Same thing with three numbers. Just add one more loop:

for i in &numbers {
    for j in &numbers {
        for h in &numbers {
            if i + j + h == 2020 {
                return (i * j * h).to_string();
            }
        }
    }
}

Cubic time, but with an input this small it doesn’t matter at all.


Solution: https://github.com/Elyrial/AdventOfCode/blob/main/src/solutions/year2020/day01.rs

No C writeup yet.