From 2b85cd754b113a76608b5e33c5666114790b4fe8 Mon Sep 17 00:00:00 2001 From: Nickiel12 Date: Fri, 1 Dec 2023 09:50:15 -0800 Subject: [PATCH] fixed wrong day2 problem --- day1/src/main.rs | 37 +++------------------- day1/src/part1.rs | 37 ++++++++++++++++++++++ day1/src/part2.rs | 80 ++++++++++++++++++++++++++++++++++++++++++++++ day2/src/main.rs | 81 ----------------------------------------------- 4 files changed, 122 insertions(+), 113 deletions(-) create mode 100644 day1/src/part1.rs create mode 100644 day1/src/part2.rs diff --git a/day1/src/main.rs b/day1/src/main.rs index 5a912d6..cadb96b 100644 --- a/day1/src/main.rs +++ b/day1/src/main.rs @@ -1,38 +1,11 @@ -use std::fs; + +mod part1; +mod part2; fn main() { - let file_contents = fs::read_to_string("./input.txt").unwrap(); - - let sum: i64 = file_contents - .lines() - .map(|x| sum_first_last(x)) - .sum::(); - println!("Answer is: {}", sum); + part1::part1(); + part2::part2(); } -fn sum_first_last(input: &str) -> i64 { - let mut first: i64 = -64; - let mut last: i64 = -64; - let _: Vec<_> = input - .chars() - .map(|x| { - if char::is_digit(x, 10) { - if first == -64 { - first = char::to_digit(x, 10).unwrap() as i64; - } else { - last = char::to_digit(x, 10).unwrap() as i64; - } - } - }).collect(); - - if first == -64 { - first = 0; - } - if last == -64 { - last = first.clone(); - } - - return first * 10 + last; -} diff --git a/day1/src/part1.rs b/day1/src/part1.rs new file mode 100644 index 0000000..e85b3e1 --- /dev/null +++ b/day1/src/part1.rs @@ -0,0 +1,37 @@ +use std::fs; + +pub fn part1() { + let file_contents = fs::read_to_string("./input.txt").unwrap(); + + let sum: i64 = file_contents + .lines() + .map(|x| sum_first_last(x)) + .sum::(); + println!("Answer is: {}", sum); + +} + +fn sum_first_last(input: &str) -> i64 { + let mut first: i64 = -64; + let mut last: i64 = -64; + let _: Vec<_> = input + .chars() + .map(|x| { + if char::is_digit(x, 10) { + if first == -64 { + first = char::to_digit(x, 10).unwrap() as i64; + } else { + last = char::to_digit(x, 10).unwrap() as i64; + } + } + }).collect(); + + if first == -64 { + first = 0; + } + if last == -64 { + last = first.clone(); + } + + return first * 10 + last; +} diff --git a/day1/src/part2.rs b/day1/src/part2.rs new file mode 100644 index 0000000..06c6441 --- /dev/null +++ b/day1/src/part2.rs @@ -0,0 +1,80 @@ +use std::fs; + +pub fn part2() { + + let file_contents = fs::read_to_string("./input.txt").unwrap(); + + let sum: i64 = file_contents + .lines() + .map(|x| sum_first_last(x)) + .sum::(); + println!("Answer is: {}", sum); + +} + +fn sum_first_last(input: &str) -> i64 { + let mut first: i64 = -64; + let mut last: i64 = -64; + + + let _: Vec<_> = input + .lines() + .map(|line| { + // if !line.contains(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]){ + for n in 0..line.len() { + if char::is_digit(line.chars().nth(n).unwrap(), 10){ + if first == -64 { + first = char::to_digit(line.chars().nth(n).unwrap(), 10).unwrap() as i64; + } else { + last = char::to_digit(line.chars().nth(n).unwrap(), 10).unwrap() as i64; + } + } else { + let val = get_str_num(line.get(n..).unwrap()); + match val { + Some(val) => { + if first == -64 { + first = val; + } else { + last = val; + } + }, + None => {} + }; + } + } + }).collect(); + + if first == -64 { + first = 0; + } + if last == -64 { + last = first.clone(); + } + + return first * 10 + last; +} + +fn get_str_num(x: &str) -> Option { + // check timing of branch pre-check version + if x.starts_with("one") { + Some(1) + } else if x.starts_with("two") { + Some(2) + } else if x.starts_with("three") { + Some(3) + } else if x.starts_with("four") { + Some(4) + } else if x.starts_with("five") { + Some(5) + } else if x.starts_with("six") { + Some(6) + } else if x.starts_with("seven") { + Some(7) + } else if x.starts_with("eight") { + Some(8) + } else if x.starts_with("nine") { + Some(9) + } else { + None + } +} diff --git a/day2/src/main.rs b/day2/src/main.rs index d959ea2..e69de29 100644 --- a/day2/src/main.rs +++ b/day2/src/main.rs @@ -1,81 +0,0 @@ -use std::fs; - - -fn main() { - - let file_contents = fs::read_to_string("./input.txt").unwrap(); - - let sum: i64 = file_contents - .lines() - .map(|x| sum_first_last(x)) - .sum::(); - println!("Answer is: {}", sum); - -} - -fn sum_first_last(input: &str) -> i64 { - let mut first: i64 = -64; - let mut last: i64 = -64; - - - let _: Vec<_> = input - .lines() - .map(|line| { - // if !line.contains(["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]){ - for n in 0..line.len() { - if char::is_digit(line.chars().nth(n).unwrap(), 10){ - if first == -64 { - first = char::to_digit(line.chars().nth(n).unwrap(), 10).unwrap() as i64; - } else { - last = char::to_digit(line.chars().nth(n).unwrap(), 10).unwrap() as i64; - } - } else { - let val = get_str_num(line.get(n..).unwrap()); - match val { - Some(val) => { - if first == -64 { - first = val; - } else { - last = val; - } - }, - None => {} - }; - } - } - }).collect(); - - if first == -64 { - first = 0; - } - if last == -64 { - last = first.clone(); - } - - return first * 10 + last; -} - -fn get_str_num(x: &str) -> Option { - // check timing of branch pre-check version - if x.starts_with("one") { - Some(1) - } else if x.starts_with("two") { - Some(2) - } else if x.starts_with("three") { - Some(3) - } else if x.starts_with("four") { - Some(4) - } else if x.starts_with("five") { - Some(5) - } else if x.starts_with("six") { - Some(6) - } else if x.starts_with("seven") { - Some(7) - } else if x.starts_with("eight") { - Some(8) - } else if x.starts_with("nine") { - Some(9) - } else { - None - } -}