From 331556e64dd4beed1223d749d9e21de8fff389c6 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Fri, 1 Dec 2017 07:12:21 +0200 Subject: Day 1: sum of characters --- src/bin/day_1.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/bin/day_1.rs') diff --git a/src/bin/day_1.rs b/src/bin/day_1.rs index 7bcfcee..5d0e431 100644 --- a/src/bin/day_1.rs +++ b/src/bin/day_1.rs @@ -3,6 +3,22 @@ use advent_of_code_2017::*; fn main() { let args = AdventArgs::init(); + + let number_chars = args.input[0].chars().collect::>(); + + let mut sum = 0; + + for i in 0..number_chars.len() { + let next = if args.part == 1 { + (i + 1) + } else { + (i + number_chars.len() / 2) + } % number_chars.len(); + if (number_chars[i] == number_chars[next]) { + let parsed: i32 = number_chars[i].to_string().parse().unwrap(); + sum += parsed; + } + } - println!("{:?} {:?}", args.part, args.input); + println!("Sum is {}", sum); } -- cgit v1.2.3