Skip to content

Commit

Permalink
made 10 days
Browse files Browse the repository at this point in the history
  • Loading branch information
Groni3000 committed Jan 1, 2023
0 parents commit 4168a1f
Show file tree
Hide file tree
Showing 26 changed files with 10,667 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/target
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "aoc_rust"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
71 changes: 71 additions & 0 deletions src/days/day_1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// --- Day 1: Calorie Counting ---
#[allow(unused_variables)]
use std::fs;
use std::vec::Vec;


fn read_input() -> String{
let file = fs::read_to_string("src/days/inputs/day_1.txt").unwrap();

return file
}

fn split_input<'a>(input: &'a str) -> Vec<&'a str>{
return input.split("\r\n").collect::<Vec<&'a str>>()
}

#[derive(Debug)]
pub struct Elf{
total_calories: u32,
}

pub fn solve_part_1() -> Vec<Elf>{
let input = read_input();
let splitted_input = split_input(&input);

let mut elves_population: Vec<Elf> = vec![];
let mut tmp_vec: Vec<u32> = vec![];

for number_string in splitted_input {
match number_string.parse::<u32>(){
Ok(x) => tmp_vec.push(x),
Err(e) => {
let elf_total_calories = tmp_vec.to_owned().into_iter().sum();
let new_elf = Elf{total_calories: elf_total_calories};
elves_population.push(new_elf);
tmp_vec.clear();
},
}
}

elves_population.sort_by(|a, b| a.total_calories.partial_cmp(&b.total_calories).unwrap());
println!("max_calories = {:?}", elves_population[elves_population.len() - 1].total_calories);

return elves_population
}

pub fn solve_part_2 (elves_population:Vec<Elf>, top_n: u32) {
println!("Sum of top_{top_n} max_calories = {:?}", elves_population.iter().rev().take(top_n as usize).map(|x| x.total_calories).sum::<u32>());

}

pub fn solve_v2 () {
let input = include_str!(r"D:\rust\exercises\AOC_rust\src\days\inputs\day_1.txt");

let mut result: Vec<u32> = input.split("\r\n\r\n")
.map(|x| {
return x
// .split_ascii_whitespace()
.lines()
// .map(|x| str::parse::<u32>(x).unwrap())
.flat_map(str::parse::<u32>)
.sum();
})
.collect();

result.sort_unstable_by(|a, b| b.cmp(a));

// println!("solution: {:?}", result);
println!("First part solution: {:?}", result.first().unwrap());
println!("Second part solution: {:?}", result.into_iter().take(3).sum::<u32>());
}
103 changes: 103 additions & 0 deletions src/days/day_10.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
fn read_input<'a>() -> Vec<&'a str> {
let input = include_str!(r"D:\rust\exercises\AOC_rust\src\days\inputs\day_10.txt");
input.lines().collect::<Vec<_>>()
}

fn draw_pixel(crt_line: &mut Vec<char>, sprite: &Vec<i32>, register_value: &i32) {
let a: i32 = crt_line.len() as i32 % 40;
crt_line.push(match sprite.contains(&a) {
true => '#',
false => '.',
})
}

pub fn solve_v1() {
let lines = read_input();

let mut cycle_n = 0 as u32;
let mut register_value: i32 = 1;
let mut signal_strengths = vec![];
let mut stop_indeces = (20u32..221 as u32).step_by(40).rev().collect::<Vec<_>>();

for line in lines.iter() {
let splitted_line = line.split(" ").collect::<Vec<_>>();

if cycle_n == *stop_indeces.last().unwrap() {
signal_strengths.push((stop_indeces.pop().unwrap() as i32) * register_value);
if stop_indeces.len() == 0 {
break;
}
}

if splitted_line[0] == "noop" {
cycle_n += 1;
if cycle_n == *stop_indeces.last().unwrap() {
signal_strengths.push((stop_indeces.pop().unwrap() as i32) * register_value);
if stop_indeces.len() == 0 {
break;
}
}
} else if splitted_line[0] == "addx" {
cycle_n += 1;
if cycle_n == *stop_indeces.last().unwrap() {
signal_strengths.push((stop_indeces.pop().unwrap() as i32) * register_value);
if stop_indeces.len() == 0 {
break;
}
};
cycle_n += 1;
if cycle_n == *stop_indeces.last().unwrap() {
signal_strengths.push((stop_indeces.pop().unwrap() as i32) * register_value);
if stop_indeces.len() == 0 {
break;
}
};
register_value += splitted_line[1].parse::<i32>().unwrap();
}
}

println!(
"First part solution: {:?}",
signal_strengths.into_iter().sum::<i32>()
);

let mut sprite: Vec<i32> = vec![0, 1, 2];
let mut register_value: i32 = 1;
let mut cycle_n = 0 as u32;
let mut crt_line: Vec<char> = vec![];

for line in lines {
let splitted_line = line.split(" ").collect::<Vec<_>>();

if cycle_n == 241 {
break;
}

if splitted_line[0] == "noop" {
cycle_n += 1;
if cycle_n == 241 {
break;
}
draw_pixel(&mut crt_line, &sprite, &register_value);
} else if splitted_line[0] == "addx" {
cycle_n += 1;
if cycle_n == 241 {
break;
}
draw_pixel(&mut crt_line, &sprite, &register_value);
cycle_n += 1;
if cycle_n == 241 {
break;
}
draw_pixel(&mut crt_line, &sprite, &register_value);
let value_to_add = splitted_line[1].parse::<i32>().unwrap();
register_value += value_to_add;
sprite.iter_mut().for_each(|x| *x += value_to_add);
}
}

println!("Second part solution:");
for crt in crt_line.chunks(40).collect::<Vec<_>>() {
println!("{:?}", crt);
}
}
39 changes: 39 additions & 0 deletions src/days/day_2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::{collections::HashMap};


pub fn solve(){
//! I dont want to continue this version.
let input = include_str!(r"D:\rust\exercises\AOC_rust\src\days\inputs\day_2.txt");
let commands:Vec<&str> = input.lines().collect();
// println!("{:?}", commands);
let game_points:HashMap<char, u8> = HashMap::from([
('A', 1), ('X', 1), // Rock
('B', 2), ('Y', 2), // Paper
('C', 3), ('Z', 3), // Scissors
]);

}

pub fn solve_v1(){
//? Honestly, I did this because I think that 1 HashMap is the most efficient way to solve this pazzle

let input = include_str!(r"D:\rust\exercises\AOC_rust\src\days\inputs\day_2.txt");
let commands:Vec<&str> = input.lines().collect();

let game_outcomes:HashMap<&str, u32> = HashMap::from([
("A X", 3+1), ("A Y", 6+2), ("A Z", 0+3),
("B X", 0+1), ("B Y", 3+2), ("B Z", 6+3),
("C X", 6+1), ("C Y", 0+2), ("C Z", 3+3),
]);
let solution:u32 = commands.iter().map(|x| game_outcomes.get(x).unwrap()).sum();
println!("First part solution: {:?}", solution);

let game_outcomes_2:HashMap<&str, u32> = HashMap::from([
("A X", 0+3), ("A Y", 3+1), ("A Z", 6+2),
("B X", 0+1), ("B Y", 3+2), ("B Z", 6+3),
("C X", 0+2), ("C Y", 3+3), ("C Z", 6+1),
]);
let solution:u32 = commands.into_iter().map(|x| game_outcomes_2.get(x).unwrap()).sum();
println!("Second part solution: {:?}", solution);
}
50 changes: 50 additions & 0 deletions src/days/day_3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::collections::HashSet;

fn apply_me_1(x:&str) -> u32{
let chars_iter = x.chars();
let string_length = chars_iter.to_owned().count();
let half_length = string_length/2;

let first_part: HashSet<char> = chars_iter.to_owned().take(half_length).collect();
let second_part: HashSet<char> = chars_iter.rev().take(half_length).collect();

// Intersection
let binding = first_part.intersection(&second_part).collect::<Vec<&char>>();
let intersection = binding.first().unwrap().to_owned().to_owned(); // <--- WTF IS THAT

// Get points
let points = match intersection.is_lowercase() {
true => intersection as u8 - 96,
false => intersection as u8 - 38,
};

return points as u32
}

fn apply_me_2(x:&[&str]) -> u32{
let mut first_el:HashSet<char> = HashSet::from_iter(x[0].chars());

for str_chunk in &x[1..]{
first_el.retain(|item| str_chunk.contains(*item));
}

// Get points
let intersection:Vec<char> = first_el.into_iter().collect();
let points = match intersection[0].is_lowercase() {
true => intersection[0] as u8 - 96,
false => intersection[0] as u8 - 38,
};

return points as u32
}

pub fn solve_v1(){
let input = include_str!(r"D:\rust\exercises\AOC_rust\src\days\inputs\day_3.txt");
let commands:Vec<&str> = input.lines().collect();
let solution = commands.iter().map(|x| apply_me_1(x)).sum::<u32>();
println!("First part solution: {:?}", solution);

let reshaped_commands = commands.chunks(3);
let solution: u32 = reshaped_commands.map(|x| apply_me_2(x)).sum();
println!("Second part solution: {:?}", solution);
}
45 changes: 45 additions & 0 deletions src/days/day_4.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
pub fn solve_v1() {
let input = include_str!(r"D:\rust\exercises\AOC_rust\src\days\inputs\day_4.txt");
let commands: Vec<Vec<Vec<u32>>> = input
.lines()
.into_iter()
// .take(20)
.map(|line| {
line.split(",")
.map(|inner_str| {
inner_str
.split("-")
.collect::<Vec<&str>>()
.into_iter()
.map(|numeric_str| numeric_str.parse::<u32>().unwrap()).collect()
})
.collect()
})
.collect();

let mut counter: u32 = 0;

for pair in commands.to_owned(){
if pair[0][0] <= pair[1][0] && pair[0][1] >= pair[1][1]{
counter += 1;
} else if pair[0][0] >= pair[1][0] && pair[0][1] <= pair[1][1] {
counter += 1;
}
};
println!("First part solution: {:?}", counter);

let mut counter: u32 = 0;

for pair in commands{
let val_10_inside = pair[1][0] >= pair[0][0] && pair[1][0] <= pair[0][1];
let val_11_inside = pair[1][1] >= pair[0][0] && pair[1][1] <= pair[0][1];
let val_00_inside = pair[0][0] >= pair[1][0] && pair[0][0] <= pair[1][1];
let val_01_inside = pair[0][1] >= pair[1][0] && pair[0][1] <= pair[1][1];

if val_10_inside || val_11_inside || val_00_inside || val_01_inside{
counter += 1;
}
};
println!("Second part solution: {:?}", counter);

}
Loading

0 comments on commit 4168a1f

Please sign in to comment.