feat: solve day 1, part 1 & 2
This commit is contained in:
parent
e16a8b44dd
commit
d34bc9a0f2
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/target
|
5
Cargo.lock
generated
Normal file
5
Cargo.lock
generated
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
[[package]]
|
||||||
|
name = "advent-of-code-2021"
|
||||||
|
version = "0.1.0"
|
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "advent-of-code-2021"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Rokas Puzonas <rokas.puz@gmail.com>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
@ -2,3 +2,12 @@
|
|||||||
|
|
||||||
https://adventofcode.com/2021
|
https://adventofcode.com/2021
|
||||||
|
|
||||||
|
|
||||||
|
## Resources
|
||||||
|
* https://github.com/ithinuel/advent-of-code-rust/tree/2020/src/bin
|
||||||
|
* AOC runner setup: https://github.com/johnterickson/adventofcode
|
||||||
|
* Advent of Code 2020 blog: https://danvdk.medium.com/advent-of-code-2020-this-time-in-rust-7904559e24bc
|
||||||
|
* Advent of Code in Rust lessons learned: https://gendignoux.com/blog/2019/08/25/rust-advent-of-code.html
|
||||||
|
* Learning Rust via Advent of Code: https://www.forrestthewoods.com/blog/learning-rust-via-advent-of-code/
|
||||||
|
* Learning Rust through te Advent of Code: https://bennetthardwick.com/aoc-rust-intro/
|
||||||
|
|
||||||
|
51
src/day1.rs
Normal file
51
src/day1.rs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
pub fn input_from_file(filename: &str) -> Vec<u32> {
|
||||||
|
let mut file = File::open(filename).expect("Can't open file");
|
||||||
|
|
||||||
|
let mut contents = String::new();
|
||||||
|
file.read_to_string(&mut contents).expect("Oops! Can not read the file...");
|
||||||
|
|
||||||
|
return contents.split_whitespace().map(|s| s.parse::<u32>().unwrap()).collect();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part1(depths: &[u32]) -> u32 {
|
||||||
|
let mut count = 0;
|
||||||
|
for i in 1..depths.len() {
|
||||||
|
if depths[i] > depths[i-1] {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn part2(depths: &[u32]) -> u32 {
|
||||||
|
let mut count = 0;
|
||||||
|
for i in 2..depths.len()-1 {
|
||||||
|
if depths[i+1] > depths[i-2] {
|
||||||
|
count += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part1_example() {
|
||||||
|
let input = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
|
||||||
|
let result = part1(&input);
|
||||||
|
assert_eq!(result, 7);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn part2_example() {
|
||||||
|
let input = [199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
|
||||||
|
let result = part2(&input);
|
||||||
|
assert_eq!(result, 5);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
11
src/main.rs
Normal file
11
src/main.rs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
mod day1;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let input_filename = "input.txt";
|
||||||
|
let input = day1::input_from_file(input_filename);
|
||||||
|
let result1 = day1::part1(&input);
|
||||||
|
let result2 = day1::part2(&input);
|
||||||
|
println!("Part 1 result: {}", result1);
|
||||||
|
println!("Part 2 result: {}", result2);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user