Start rust rewrite of old problems

This commit is contained in:
Thom Dickson 2021-12-13 01:21:42 -05:00
parent 2e74bf7d37
commit 5e8e0ae3ef
Signed by: boots
GPG Key ID: 40BE2AF8EBF8D2BB
3 changed files with 2044 additions and 0 deletions

View File

@ -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]

2000
rust-rewrite/day01/day01.in Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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
}