second day
This commit is contained in:
parent
f3ad8468f5
commit
904b6f34d0
@ -1,3 +1,2 @@
|
|||||||
# aoc
|
# Advent of Code
|
||||||
|
My attempt to these festival coding challenges. Written in Rust.
|
||||||
Advent of Code - solutions in Rust
|
|
||||||
|
1000
src/aoc2021/day02/input.txt
Normal file
1000
src/aoc2021/day02/input.txt
Normal file
File diff suppressed because it is too large
Load Diff
41
src/aoc2021/day02/mod.rs
Normal file
41
src/aoc2021/day02/mod.rs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
pub fn task_one(input: &str) -> String {
|
||||||
|
let mut horizontal_pos: usize = 0;
|
||||||
|
let mut depth: usize = 0;
|
||||||
|
|
||||||
|
for line in input.lines() {
|
||||||
|
let split = line.split_once(' ').unwrap();
|
||||||
|
let amount = split.1.trim().parse::<usize>().unwrap();
|
||||||
|
|
||||||
|
match split.0 {
|
||||||
|
"forward" => horizontal_pos = horizontal_pos + amount,
|
||||||
|
"down" => depth = depth + amount,
|
||||||
|
"up" => depth = depth - amount,
|
||||||
|
_ => panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (horizontal_pos * depth).to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task_two(input: &str) -> String {
|
||||||
|
let mut horizontal_pos: usize = 0;
|
||||||
|
let mut depth: usize = 0;
|
||||||
|
let mut aim: usize = 0;
|
||||||
|
|
||||||
|
for line in input.lines() {
|
||||||
|
let split = line.split_once(' ').unwrap();
|
||||||
|
let amount = split.1.trim().parse::<usize>().unwrap();
|
||||||
|
|
||||||
|
match split.0 {
|
||||||
|
"forward" => {
|
||||||
|
horizontal_pos = horizontal_pos + amount;
|
||||||
|
depth = depth + aim * amount;
|
||||||
|
},
|
||||||
|
"down" => aim = aim + amount,
|
||||||
|
"up" => aim = aim - amount,
|
||||||
|
_ => panic!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (horizontal_pos * depth).to_string();
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
pub mod day01;
|
pub mod day01;
|
||||||
|
pub mod day02;
|
||||||
|
@ -7,5 +7,10 @@ fn main() {
|
|||||||
let input_day01 = fs::read_to_string("src/aoc2021/day01/input.txt").expect("Error on reading file.");
|
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_task1 = aoc2021::day01::task_one(&input_day01);
|
||||||
let result_day01_task2 = aoc2021::day01::task_two(&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);
|
println!("AOC2021 Day 01 \t Task 1: {},\t\tTask 2: {}", result_day01_task1, result_day01_task2);
|
||||||
|
|
||||||
|
let input_day02 = fs::read_to_string("src/aoc2021/day02/input.txt").expect("Error on reading file.");
|
||||||
|
let result_day02_task1 = aoc2021::day02::task_one(&input_day02);
|
||||||
|
let result_day02_task2 = aoc2021::day02::task_two(&input_day02);
|
||||||
|
println!("AOC2021 Day 02 \t Task 1: {},\tTask 2: {}", result_day02_task1, result_day02_task2);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user