Solve 2021/01

master
Alfred Melch 3 years ago
parent 3850dba4ec
commit c60a498358

1
.gitignore vendored

@ -1,2 +1,3 @@
venv
*.pyc
target/

@ -0,0 +1,10 @@
199
200
208
210
200
207
240
269
260
263

File diff suppressed because it is too large Load Diff

@ -0,0 +1,25 @@
use std::io::stdin;
use std::io::BufRead;
fn main() {
let mut prev: Option<i32> = None;
let mut increased = 0;
for line in stdin().lock().lines() {
let cur = line.unwrap().parse::<i32>().unwrap();
match prev {
None => println!("{} (N/A)", cur),
Some(prev) => {
if cur > prev {
increased += 1;
}
let evaluate = if cur > prev { "increased" } else { "decreased" };
println!("{} ({})", cur, evaluate)
}
}
prev = Some(cur);
}
println!("{}", increased)
}

@ -0,0 +1,27 @@
use itertools::Itertools;
use std::io::stdin;
use std::io::BufRead;
fn main() {
let mut prev: Option<i32> = None;
let mut increased = 0;
for (a, b, c) in stdin()
.lock()
.lines()
.map(|line| line.unwrap().parse::<i32>().unwrap())
.tuple_windows()
{
let sum = a + b + c;
match prev {
Some(prev) => {
if sum > prev {
increased += 1
}
}
_ => (),
}
prev = Some(sum)
}
println!("{}", increased)
}

25
2021/Cargo.lock generated

@ -0,0 +1,25 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aoc-2021"
version = "0.1.0"
dependencies = [
"itertools",
]
[[package]]
name = "either"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "itertools"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
dependencies = [
"either",
]

@ -0,0 +1,19 @@
[package]
name = "aoc-2021"
version = "0.1.0"
edition = "2021"
[dependencies]
itertools = "0.10.1"
[lib]
name = "util"
path = "util/main.rs"
[[bin]]
name = "day-01-part-1"
path = "01/part1.rs"
[[bin]]
name = "day-01-part-2"
path = "01/part2.rs"

@ -0,0 +1,11 @@
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Lines;
pub mod math;
pub fn file_lines(path: String) -> Lines<BufReader<File>> {
let input = File::open(path).unwrap();
return BufReader::new(input).lines();
}

@ -0,0 +1,7 @@
pub fn sum(x: i32, y: i32) -> i32 {
x + y
}
pub fn sub(x: i32, y: i32) -> i32 {
x - y
}
Loading…
Cancel
Save