Day1
This commit is contained in:
commit
19d34d9263
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
.idea/
|
171
Cargo.lock
generated
Normal file
171
Cargo.lock
generated
Normal file
|
@ -0,0 +1,171 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "aoc2021"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"clap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
"time",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "2.34.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"atty",
|
||||
"bitflags",
|
||||
"strsim",
|
||||
"textwrap",
|
||||
"unicode-width",
|
||||
"vec_map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.108"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
|
||||
dependencies = [
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"wasi",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
10
Cargo.toml
Normal file
10
Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "aoc2021"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
clap = "2.34.0"
|
||||
chrono = "0.4.19"
|
2000
input/day1
Normal file
2000
input/day1
Normal file
File diff suppressed because it is too large
Load diff
5
src/day.rs
Normal file
5
src/day.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
pub trait Day {
|
||||
fn init(content: String) -> Self where Self: Sized;
|
||||
fn part1(&self) -> String;
|
||||
fn part2(&self) -> String;
|
||||
}
|
71
src/day1.rs
Normal file
71
src/day1.rs
Normal file
|
@ -0,0 +1,71 @@
|
|||
use std::fs::read_to_string;
|
||||
|
||||
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone)]
|
||||
pub struct Day1(Vec<i32>);
|
||||
|
||||
impl crate::day::Day for Day1 {
|
||||
fn init(f: String) -> Self {
|
||||
let v = f.split('\n').map(|x| x.parse::<i32>().unwrap()).collect::<Vec<i32>>();
|
||||
Self(v)
|
||||
}
|
||||
|
||||
fn part1(&self) -> String {
|
||||
let (a, _) = self.0.iter().fold((0, -1), increments);
|
||||
format!("{}", a)
|
||||
}
|
||||
|
||||
fn part2(&self) -> String {
|
||||
let ws = self.0.windows(3).map(|a| a.iter().fold(0, std::ops::Add::add)).collect::<Vec<i32>>();
|
||||
let (a, _) = ws.iter().fold((0, -1), increments);
|
||||
format!("{}", a)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn increments((a, b): (i32, i32), c: &i32) -> (i32, i32) {
|
||||
let c = *c;
|
||||
if b == -1 {
|
||||
(a, c)
|
||||
} else if c > b {
|
||||
(a + 1, c)
|
||||
} else {
|
||||
(a, c)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use std::fs::read_to_string;
|
||||
use super::Day1;
|
||||
use crate::day::Day;
|
||||
|
||||
#[test]
|
||||
pub fn part1_real() {
|
||||
let f = read_to_string("./input/day1").expect("Could not load input");
|
||||
let d = Day1::init(f);
|
||||
assert_eq!("1466", d.part1())
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn part2_real() {
|
||||
let f = read_to_string("./input/day1").expect("Could not load input");
|
||||
let d = Day1::init(f);
|
||||
assert_eq!("1491", d.part2())
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn part1_test() {
|
||||
let v = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
|
||||
let strs = v.iter().map(i32::to_string).collect::<Vec<String>>().join("\n");
|
||||
let d = Day1::init(strs);
|
||||
assert_eq!("7", d.part1())
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn part2_test() {
|
||||
let v = vec![199, 200, 208, 210, 200, 207, 240, 269, 260, 263];
|
||||
let strs = v.iter().map(i32::to_string).collect::<Vec<String>>().join("\n");
|
||||
let d = Day1::init(strs);
|
||||
assert_eq!("5", d.part2())
|
||||
}
|
||||
}
|
62
src/main.rs
Normal file
62
src/main.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use std::fs::read_to_string;
|
||||
use std::time::SystemTime;
|
||||
use chrono::Datelike;
|
||||
use clap::{App, Arg, SubCommand};
|
||||
use crate::day1::Day1;
|
||||
use crate::day::Day;
|
||||
|
||||
mod day;
|
||||
mod day1;
|
||||
|
||||
fn load_input(day: &str) -> String {
|
||||
read_to_string(format!("./input/day{}", day)).expect("Could not load input")
|
||||
}
|
||||
|
||||
fn run_day(days: &Vec<Box<dyn Day>>, day: usize) {
|
||||
if let Some(d) = days.get(day - 1) {
|
||||
let day_code = d.clone();
|
||||
println!("Running day {}!", day);
|
||||
println!("\tPart 1: {}", d.part1());
|
||||
println!("\tPart 2: {}", day_code.part2());
|
||||
} else {
|
||||
eprintln!("Day {} is not available!", day);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let matches = App::new("AOC Time!")
|
||||
.version("1.0")
|
||||
.author("J00LZ")
|
||||
.about("How to overengineer AOC in a single program...")
|
||||
.arg(Arg::with_name("v")
|
||||
.short("v")
|
||||
.multiple(true)
|
||||
.help("Sets the level of verbosity"))
|
||||
.arg(Arg::with_name("all")
|
||||
.short("a")
|
||||
.long("all")
|
||||
.help("run all available days"))
|
||||
.arg(Arg::with_name("DAY")
|
||||
.takes_value(true)
|
||||
.multiple(true))
|
||||
.get_matches();
|
||||
|
||||
let days: Vec<Box<dyn Day>> = vec![Box::new(Day1::init(load_input("1")))];
|
||||
|
||||
let verbosity = matches.occurrences_of("v");
|
||||
if matches.is_present("all") {
|
||||
let l = days.len();
|
||||
println!("running {} days!", l);
|
||||
for v in 1..l + 1 {
|
||||
run_day(&days, v);
|
||||
}
|
||||
} else if let Some(d) = matches.values_of("DAY") {
|
||||
let d = d.map(|x| x.parse::<i32>().expect("Did not provide an int!"));
|
||||
for v in d {
|
||||
run_day(&days, v as usize);
|
||||
}
|
||||
} else {
|
||||
let v = chrono::Local::now().day();
|
||||
run_day(&days, v as usize)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue