Created
December 2, 2020 12:06
-
-
Save thomasaarholt/39d0b21df6bae00cbad17f1d9e0e0b73 to your computer and use it in GitHub Desktop.
Beginner-rust solution to Advent of Code 2020 Day 01 - Feedback very welcome!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Uses the cargo-aoc crate to help take care of inputs and benchmarks | |
use aoc_runner_derive::{aoc, aoc_generator}; | |
use std::num::ParseIntError; | |
#[aoc_generator(day1)] | |
fn parse_input_day1(input: &str) -> Result<Vec<i32>, ParseIntError> { | |
input.lines().map(|l| l.parse()).collect() | |
} | |
#[aoc(day1, part1)] | |
fn part1(numbers: &[i32]) -> i32 { | |
let mut numbers = numbers.to_vec(); | |
numbers.sort(); | |
let mut sum; | |
let mut iter = numbers.iter(); | |
let mut low = iter.next(); | |
let mut high = iter.next_back(); | |
let result = loop { | |
let lowval = low.unwrap(); | |
let highval = high.unwrap(); | |
sum = lowval + highval; | |
if sum > 2020 { | |
high = iter.next_back(); | |
} | |
else if sum < 2020 { | |
low = iter.next(); | |
} | |
else { | |
break lowval * highval; | |
} | |
}; | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment