From 057acf84565cc22921f7344920416cb8d180381d Mon Sep 17 00:00:00 2001 From: Zhongheng Liu Date: Wed, 11 Dec 2024 21:05:59 +0200 Subject: [PATCH] feat: day 1 and 2 code in rust --- src/day1.rs | 50 ++++++++++++++++++++++++++++++++++++ src/day2.rs | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 12 +++++++++ 3 files changed, 136 insertions(+) create mode 100644 src/day1.rs create mode 100644 src/day2.rs create mode 100644 src/main.rs diff --git a/src/day1.rs b/src/day1.rs new file mode 100644 index 0000000..137d7f4 --- /dev/null +++ b/src/day1.rs @@ -0,0 +1,50 @@ +use std::fs; +fn parse_input(input_string: &str, column1: &mut Vec, column2: &mut Vec) { + let mut counter = 0; + input_string.split('\n').for_each(|line| { + if line.is_empty() { + return; + } + let values = line + .split_once(' ') + .expect("Expected line to split successfully"); + // println!("{}, {}", values.0, values.1); + let first = values.0.trim().parse::().unwrap(); + let second = values.1.trim().parse::().unwrap(); + column1.insert(counter, first); + column2.insert(counter, second); + counter += 1; + }); +} +pub fn part1(input_path: &str) { + println!("Part 1, Day 1"); + let input = fs::read_to_string(input_path).expect("Expected to read the file"); + let mut loc1: Vec = Vec::new(); + let mut loc2: Vec = Vec::new(); + parse_input(&input, &mut loc1, &mut loc2); + let mut sum = 0; + for i in 0..loc1.len() { + let diff = (loc1[i] - loc2[i]).abs(); + // println!("1: {}, 2: {}, d: {}", loc1[i], loc2[i], diff); + sum += diff; + } + println!("Solution: {}", sum); +} +pub fn part2(input_path: &str) { + println!("Part 2, Day 1"); + let input = fs::read_to_string(input_path).expect("Expected to read file normally"); + let mut list1: Vec = Vec::new(); + let mut list2: Vec = Vec::new(); + parse_input(&input, &mut list1, &mut list2); + let mut similarity_score_sum = 0; + for elem in &list1 { + let mut occurences = 0; + for i in &list2 { + if elem == i { + occurences += 1; + } + } + similarity_score_sum += elem * occurences; + } + println!("Solution: {}", similarity_score_sum); +} diff --git a/src/day2.rs b/src/day2.rs new file mode 100644 index 0000000..81e1ac0 --- /dev/null +++ b/src/day2.rs @@ -0,0 +1,74 @@ +use std::fs; +fn parse_input(input_file_path: &str, data: &mut Vec>) { + let input = + fs::read_to_string(input_file_path).expect("Expected to parse string successfully!"); + let mut i = 0; + for line in input.split('\n') { + if line.is_empty() { + return; + } + // println!("line: {}", line); + let items = line + .split(' ') + .map(|item| return item.trim().parse::().unwrap()); + let mut tmp: Vec = vec![]; + for (j, item) in items.enumerate() { + tmp.insert(j, item); + } + data.insert(i, tmp); + i += 1; + } +} +fn is_safe(arr: &[i32]) -> bool { + let mut is_increasing = true; + let mut is_decreasing = true; + for i in 0..arr.len() - 1 { + let diff = arr[i] - arr[i + 1]; + if diff.abs() < 1 || diff.abs() > 3 { + return false; + } + if arr[i] - arr[i + 1] > 0 { + is_increasing = false; + } + if arr[i] - arr[i + 1] < 0 { + is_decreasing = false; + } + } + is_increasing || is_decreasing +} +fn is_also_safe(arr: &[i32]) -> bool { + let safe = is_safe(arr); + let mut also_safe = false; + if !safe { + for (i, _item) in arr.iter().enumerate() { + let mut test = arr.to_vec(); + test.remove(i); + also_safe = if is_safe(&test) {true} else {also_safe}; + } + } + safe || also_safe +} +pub fn part1(input_file_path: &str) { + println!("Part 1, Day 2"); + let mut data = Vec::new(); + parse_input(input_file_path, &mut data); + let mut c = 0; + for line in data { + if is_safe(&line) { + c += 1; + } + } + println!("Safe lines: {}", c); +} +pub fn part2(input_file_path: &str) { + println!("Part 2, Day 2"); + let mut data = Vec::new(); + parse_input(input_file_path, &mut data); + let mut c = 0; + for line in data { + if is_also_safe(&line) { + c += 1; + } + } + println!("Safe lines: {}", c); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..c6bd8d8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,12 @@ +use std::{ + env, + fs, +}; +mod day1; +mod day2; +fn main() { + day1::part1("./day1.input"); + day1::part2("./day1.input"); + day2::part1("./day2.input"); + day2::part2("./day2.input"); +}