2021 Day 1 Rust
This commit is contained in:
parent
5b78fa4674
commit
bd10a789f5
7
2021/rust/Cargo.lock
generated
Normal file
7
2021/rust/Cargo.lock
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rust"
|
||||||
|
version = "0.1.0"
|
8
2021/rust/Cargo.toml
Normal file
8
2021/rust/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
[package]
|
||||||
|
name = "rust"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
2000
2021/rust/inputs/day01.txt
Normal file
2000
2021/rust/inputs/day01.txt
Normal file
File diff suppressed because it is too large
Load diff
7
2021/rust/src/main.rs
Normal file
7
2021/rust/src/main.rs
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
mod utils;
|
||||||
|
mod solutions;
|
||||||
|
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
solutions::day01::run();
|
||||||
|
}
|
43
2021/rust/src/solutions/day01.rs
Normal file
43
2021/rust/src/solutions/day01.rs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
use crate::utils;
|
||||||
|
use crate::utils::Day;
|
||||||
|
|
||||||
|
|
||||||
|
pub fn run() -> () {
|
||||||
|
let data = parse_data(utils::read_lines(Day(1)));
|
||||||
|
|
||||||
|
println!("Day 1");
|
||||||
|
println!("Part 1: {}", solve1(&data));
|
||||||
|
println!("Part 2: {}", solve2(&data));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve1(lines: &Vec<i32>) -> i32 {
|
||||||
|
let mut increments = 0;
|
||||||
|
let mut prev = i32::MAX;
|
||||||
|
for &n in lines {
|
||||||
|
if n > prev { increments += 1 };
|
||||||
|
prev = n;
|
||||||
|
};
|
||||||
|
increments
|
||||||
|
}
|
||||||
|
|
||||||
|
fn solve2(lines: &Vec<i32>) -> i32 {
|
||||||
|
let length = lines.len();
|
||||||
|
|
||||||
|
let left = lines[..length - 1].windows(3);
|
||||||
|
let right = lines[1..].windows(3);
|
||||||
|
|
||||||
|
|
||||||
|
left.zip(right)
|
||||||
|
.fold(0, |acc, (left, right)| {
|
||||||
|
let sum_left: i32 = left.iter().sum();
|
||||||
|
let sum_right: i32 = right.iter().sum();
|
||||||
|
acc + if sum_left < sum_right { 1 } else { 0 }
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_data(lines: Vec<String>) -> Vec<i32> {
|
||||||
|
lines.iter()
|
||||||
|
.map(|x| x.parse())
|
||||||
|
.collect::<Result<Vec<i32>, _>>()
|
||||||
|
.expect("Failed to parse int")
|
||||||
|
}
|
1
2021/rust/src/solutions/mod.rs
Normal file
1
2021/rust/src/solutions/mod.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod day01;
|
20
2021/rust/src/utils.rs
Normal file
20
2021/rust/src/utils.rs
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufRead, BufReader};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
|
||||||
|
#[repr(transparent)]
|
||||||
|
pub struct Day(pub i32);
|
||||||
|
|
||||||
|
|
||||||
|
pub fn read_lines(day: Day) -> Vec<String> {
|
||||||
|
assert!(1 <= day.0 && day.0 < 25);
|
||||||
|
_read_lines(format!("inputs/day{:0>2}.txt", day.0))
|
||||||
|
.expect(format!("Failed to load day {}", day.0).as_str())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _read_lines<P>(filename: P) -> std::io::Result<Vec<String>>
|
||||||
|
where P: AsRef<Path> {
|
||||||
|
let file = File::open(filename)?;
|
||||||
|
BufReader::new(file).lines().collect()
|
||||||
|
}
|
Loading…
Reference in a new issue