feat: day 1 and 2 code in rust
This commit is contained in:
parent
71a7dba50c
commit
057acf8456
3 changed files with 136 additions and 0 deletions
50
src/day1.rs
Normal file
50
src/day1.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use std::fs;
|
||||
fn parse_input(input_string: &str, column1: &mut Vec<i32>, column2: &mut Vec<i32>) {
|
||||
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::<i32>().unwrap();
|
||||
let second = values.1.trim().parse::<i32>().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<i32> = Vec::new();
|
||||
let mut loc2: Vec<i32> = 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<i32> = Vec::new();
|
||||
let mut list2: Vec<i32> = 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);
|
||||
}
|
74
src/day2.rs
Normal file
74
src/day2.rs
Normal file
|
@ -0,0 +1,74 @@
|
|||
use std::fs;
|
||||
fn parse_input(input_file_path: &str, data: &mut Vec<Vec<i32>>) {
|
||||
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::<i32>().unwrap());
|
||||
let mut tmp: Vec<i32> = 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);
|
||||
}
|
12
src/main.rs
Normal file
12
src/main.rs
Normal file
|
@ -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");
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue