Added 1 and 2

This commit is contained in:
2021-12-03 15:17:28 +01:00
parent 069a30205f
commit 641a176c77
12 changed files with 6161 additions and 0 deletions

8
december_1_1/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "december_1_1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2000
december_1_1/input.txt Normal file

File diff suppressed because it is too large Load Diff

21
december_1_1/src/main.rs Normal file
View File

@@ -0,0 +1,21 @@
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
fn main(){
let file = File::open("./input.txt").expect("Read failed");
let reader = BufReader::new(file);
let mut previous_value: i32 = -1;
let mut number_of_bigger_values: i32 = 0;
for line in reader.lines(){
let current_value = line.expect("-1").parse::<i32>().unwrap();
if previous_value != -1 && current_value > previous_value {
number_of_bigger_values += 1;
}
previous_value = current_value;
}
println!("Bigger values: {}", number_of_bigger_values)
}

8
december_1_2/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "december_1_2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

2000
december_1_2/input.txt Normal file

File diff suppressed because it is too large Load Diff

34
december_1_2/src/main.rs Normal file
View File

@@ -0,0 +1,34 @@
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
fn main(){
let file = File::open("./input.txt").expect("Read failed");
let reader = BufReader::new(file);
let mut number_of_bigger_values: i32 = 0;
let mut previous_sum = -1;
let mut values: Vec<i32> = Vec::new();
let all_lines = reader.lines();
for line in all_lines{
values.push(line.expect("-1").parse::<i32>().unwrap());
}
for x in 2..values.len(){
println!("{}", x);
let sum = values[x] + values[x-1] + values[x-2];
if previous_sum != -1 && sum > previous_sum {
number_of_bigger_values += 1;
}
previous_sum = sum;
}
println!("Bigger values: {}", number_of_bigger_values)
}

8
december_2_1/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "december_2_1"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1000
december_2_1/input.txt Normal file

File diff suppressed because it is too large Load Diff

35
december_2_1/src/main.rs Normal file
View File

@@ -0,0 +1,35 @@
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
fn main(){
let file = File::open("./input.txt").expect("Read failed");
let reader = BufReader::new(file);
let mut current_depth = 0;
let mut current_horizontal = 0;
let all_lines = reader.lines();
for line in all_lines{
let line_as_string = line.unwrap();
let split_line = line_as_string.split_whitespace().collect::<Vec<&str>>();
let direction = split_line[0];
let distance = split_line[1].parse::<i32>().unwrap();
match direction {
"forward" => current_horizontal += distance,
"down" => current_depth += distance,
"up" => current_depth -= distance,
_ => println!("You should never be here")
}
}
println!("Depth: {}, Horizontal: {}, Result: {}", current_depth, current_horizontal, current_horizontal*current_depth)
}

8
december_2_2/Cargo.toml Normal file
View File

@@ -0,0 +1,8 @@
[package]
name = "december_2_2"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1000
december_2_2/input.txt Normal file

File diff suppressed because it is too large Load Diff

39
december_2_2/src/main.rs Normal file
View File

@@ -0,0 +1,39 @@
use std::fs::File;
use std::io::{BufReader};
use std::io::prelude::*;
fn main(){
let file = File::open("./input.txt").expect("Read failed");
let reader = BufReader::new(file);
let mut current_depth = 0;
let mut current_horizontal = 0;
let mut aim = 0;
let all_lines = reader.lines();
for line in all_lines{
let line_as_string = line.unwrap();
let split_line = line_as_string.split_whitespace().collect::<Vec<&str>>();
let direction = split_line[0];
let distance = split_line[1].parse::<i32>().unwrap();
match direction {
"down" => aim += distance,
"up" => aim -= distance,
"forward" => {
current_horizontal += distance;
current_depth += aim * distance;
}
_ => println!("You should never be here")
}
}
println!("Depth: {}, Horizontal: {}, Result: {}", current_depth, current_horizontal, current_horizontal*current_depth)
}