Start rust rewrite of old problems
This commit is contained in:
parent
2e74bf7d37
commit
5e8e0ae3ef
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day01"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,36 @@
|
|||
use std::fs;
|
||||
|
||||
fn main() {
|
||||
let file = fs::read_to_string("day01.in").unwrap();
|
||||
|
||||
let mut nums: Vec<u32> = vec![];
|
||||
for line in file.split('\n') {
|
||||
if line != "" {
|
||||
nums.push(u32::from_str_radix(line, 10).unwrap());
|
||||
}
|
||||
}
|
||||
let count_a = count_inc(&nums);
|
||||
|
||||
let mut nums2: Vec<u32> = vec![];
|
||||
for (i, _) in nums.iter().enumerate() {
|
||||
if i + 2 < nums.len() {
|
||||
nums2.push(nums[i..i + 3].iter().sum());
|
||||
}
|
||||
}
|
||||
let count_b = count_inc(&nums2);
|
||||
|
||||
println!("Part 1: {}", count_a);
|
||||
println!("Part 2: {}", count_b);
|
||||
}
|
||||
|
||||
fn count_inc(input: &Vec<u32>) -> usize {
|
||||
let mut count = 0;
|
||||
let mut prev: u32 = 0;
|
||||
for num in input.iter() {
|
||||
if num > &prev && prev != 0 {
|
||||
count += 1;
|
||||
}
|
||||
prev = *num;
|
||||
}
|
||||
count
|
||||
}
|
Loading…
Reference in New Issue