Created
December 2, 2017 21:21
-
-
Save chutten/f876d2bf5e8dc59f145490074f9ff329 to your computer and use it in GitHub Desktop.
AoC Day 2
This file contains 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
use std::env; | |
use std::fs::File; | |
use std::io::prelude::*; | |
use std::io::BufReader; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
let filename = &args[1]; | |
println!("Using file {}", filename); | |
let f = BufReader::new(File::open(filename) | |
.expect("File not found")); | |
let mut checksum = 0; | |
let mut divs = 0; | |
for line in f.lines() { | |
let line = line.unwrap(); | |
let values = line.split_whitespace().map(|x| x.parse::<u32>().unwrap()); | |
let mut values: Vec<u32> = values.collect(); | |
values.sort(); | |
checksum += part1(&values); | |
divs += part2(&values); | |
} | |
println!("Checksum: {}", checksum); | |
println!("Divs: {}", divs); | |
} | |
fn part1(values: &Vec<u32>) -> u32 { | |
values[values.len() - 1] - values[0] | |
} | |
fn part2(values: &Vec<u32>) -> u32 { | |
for i in 0..values.len() { | |
let candidate = values[values.len() - 1 - i]; | |
for j in i+1..values.len() { | |
let divisor = values[values.len() - 1 - j]; | |
if candidate % divisor == 0 { | |
return candidate / divisor; | |
} | |
} | |
} | |
println!("OH NOES"); | |
0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment