basic project structure and first day in 2021
This commit is contained in:
parent
4a2373aa82
commit
f3ad8468f5
5
.gitignore
vendored
5
.gitignore
vendored
@ -11,3 +11,8 @@ Cargo.lock
|
|||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# Added by cargo
|
||||||
|
|
||||||
|
/target
|
||||||
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "aoc"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
2000
src/aoc2021/day01/input.txt
Normal file
2000
src/aoc2021/day01/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
60
src/aoc2021/day01/mod.rs
Normal file
60
src/aoc2021/day01/mod.rs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
pub fn task_one(input: &str) -> String {
|
||||||
|
let mut counter: usize = 0;
|
||||||
|
|
||||||
|
let mut last_line: String = String::new();
|
||||||
|
|
||||||
|
for line in input.lines() {
|
||||||
|
if last_line.is_empty() {
|
||||||
|
last_line = line.to_string();
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
if last_line.parse::<usize>().unwrap() < line.parse::<usize>().unwrap() {
|
||||||
|
counter = counter + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_line = line.to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return counter.to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task_two(input: &str) -> String {
|
||||||
|
let mut counter: usize = 0;
|
||||||
|
|
||||||
|
let mut top_line: String = String::new();
|
||||||
|
let mut middle_line: String = String::new();
|
||||||
|
let mut last_line: String = String::new();
|
||||||
|
|
||||||
|
let mut current_frame: usize;
|
||||||
|
let mut last_frame: usize = 0;
|
||||||
|
|
||||||
|
for (i, line) in input.lines().enumerate() {
|
||||||
|
match i {
|
||||||
|
0 => top_line = line.to_string(),
|
||||||
|
1 => middle_line = line.to_string(),
|
||||||
|
2 => last_line = line.to_string(),
|
||||||
|
_ => {
|
||||||
|
current_frame = top_line.parse::<usize>().unwrap() + middle_line.parse::<usize>().unwrap() + last_line.parse::<usize>().unwrap();
|
||||||
|
|
||||||
|
if last_frame != 0 && last_frame < current_frame {
|
||||||
|
counter = counter + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
last_frame = current_frame;
|
||||||
|
|
||||||
|
top_line = middle_line;
|
||||||
|
middle_line = last_line;
|
||||||
|
last_line = line.to_string();
|
||||||
|
},
|
||||||
|
}
|
||||||
|
// println!("{}\t\t {}\t{}\t{}\t current_frame: {}", i, top_line, middle_line, last_line, current_frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
current_frame = top_line.parse::<usize>().unwrap() + middle_line.parse::<usize>().unwrap() + last_line.parse::<usize>().unwrap();
|
||||||
|
if last_frame < current_frame {
|
||||||
|
counter = counter + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return counter.to_string();
|
||||||
|
}
|
1
src/aoc2021/mod.rs
Normal file
1
src/aoc2021/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod day01;
|
11
src/main.rs
Normal file
11
src/main.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
mod aoc2021;
|
||||||
|
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
let input_day01 = fs::read_to_string("src/aoc2021/day01/input.txt").expect("Error on reading file.");
|
||||||
|
let result_day01_task1 = aoc2021::day01::task_one(&input_day01);
|
||||||
|
let result_day01_task2 = aoc2021::day01::task_two(&input_day01);
|
||||||
|
println!("AOC2021 Day 01 \t Task 1: {}, Task 2: {}", result_day01_task1, result_day01_task2);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user