From 33db46ef6dcdddeb8e9c64ca6f18a1cedfe9de36 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 2 Dec 2017 07:26:08 +0200 Subject: Day 2 --- src/bin/day_2.rs | 26 ++++++++++++++++++++++++++ src/lib.rs | 6 ++++++ 2 files changed, 32 insertions(+) create mode 100644 src/bin/day_2.rs (limited to 'src') diff --git a/src/bin/day_2.rs b/src/bin/day_2.rs new file mode 100644 index 0000000..307029a --- /dev/null +++ b/src/bin/day_2.rs @@ -0,0 +1,26 @@ +extern crate advent_of_code_2017; +use advent_of_code_2017::*; + +fn main() { + let args = AdventArgs::init(); + let sum = args.input.iter().map(|line| { + let splitline = parse_space_separated_ints(line).unwrap(); + + if args.part == 1 { + let max = splitline.iter().max().unwrap(); + let min = splitline.iter().min().unwrap(); + max-min + } else { + for i in 0..splitline.len() { + for j in 0..splitline.len() { + if i != j && splitline[i] % splitline[j] == 0 { + return splitline[i] / splitline[j]; + } + } + } + panic!("Didn't find a dividing one! {:?}", splitline) + } + }).sum::(); + + println!("Checksum is {}", sum); +} diff --git a/src/lib.rs b/src/lib.rs index c5a0f16..5d3721f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,3 +59,9 @@ impl AdventArgs { .collect() } } + +pub fn parse_space_separated_ints(line: &String) -> Result, std::num::ParseIntError> { + line.split_whitespace() + .map(|x| x.parse::()) + .collect() +} -- cgit v1.2.3