dustin
/
aoc2021
Archived
1
0
Fork 0

Compare commits

..

No commits in common. "778342b2a296feebaca9ce543648d62810faf7b6" and "c378851aedb925ab12fd80542628e0223655bc6c" have entirely different histories.

7 changed files with 24 additions and 1208 deletions

80
Cargo.lock generated
View File

@ -6,91 +6,11 @@ version = 3
name = "aoc2021"
version = "0.1.0"
dependencies = [
"argh",
"bounded-vec-deque",
]
[[package]]
name = "argh"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f023c76cd7975f9969f8e29f0e461decbdc7f51048ce43427107a3d192f1c9bf"
dependencies = [
"argh_derive",
"argh_shared",
]
[[package]]
name = "argh_derive"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48ad219abc0c06ca788aface2e3a1970587e3413ab70acd20e54b6ec524c1f8f"
dependencies = [
"argh_shared",
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "argh_shared"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38de00daab4eac7d753e97697066238d67ce9d7e2d823ab4f72fe14af29f3f33"
[[package]]
name = "bounded-vec-deque"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2225b558afc76c596898f5f1b3fc35cfce0eb1b13635cbd7d1b2a7177dc10ccd"
[[package]]
name = "heck"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "proc-macro2"
version = "1.0.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba508cc11742c0dc5c1659771673afbab7a0efab23aa17e854cbab0837ed0b43"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
"proc-macro2",
]
[[package]]
name = "syn"
version = "1.0.82"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-segmentation"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"

View File

@ -7,4 +7,3 @@ edition = "2018"
[dependencies]
bounded-vec-deque = "0.1.1"
argh = "^0.1.6"

File diff suppressed because it is too large Load Diff

View File

@ -1 +0,0 @@
max_width = 79

View File

@ -1,58 +1,33 @@
use std::io;
use std::io::prelude::*;
use std::env;
use std::fs::File;
use argh::FromArgs;
use bounded_vec_deque::BoundedVecDeque;
mod pilot;
mod sonar;
#[derive(FromArgs)]
/// Elvish Submarine Control
struct Arguments {
#[argh(subcommand)]
command: SubCommand,
}
#[derive(FromArgs)]
#[argh(subcommand)]
enum SubCommand {
Sonar(SonarTracker),
Pilot(SubPilot),
}
#[derive(FromArgs)]
/// sonar data reader
#[argh(subcommand, name = "sonar")]
struct SonarTracker {
/// path to input data file
#[argh(positional)]
input: String,
/// sliding window size
#[argh(option, default = "1")]
window: usize,
}
#[derive(FromArgs)]
/// pilot the sub!
#[argh(subcommand, name = "pilot")]
struct SubPilot {
/// path to input data file
#[argh(positional)]
input: String,
}
fn main() -> io::Result<()> {
let args: Arguments = argh::from_env();
match args.command {
SubCommand::Sonar(args) => {
let count_incr = sonar::count_increases(&args.input, args.window)?;
println!("number of increases: {}", count_incr);
}
SubCommand::Pilot(args) => {
let pos = pilot::track_position(&args.input)?;
println!("Final Position: {},{}", pos.linear, pos.depth);
println!("Product: {}", pos.linear * pos.depth);
let args: Vec<String> = env::args().collect();
let f = File::open(&args[1])?;
let buf = io::BufReader::new(f);
let mut deque: BoundedVecDeque<i32> = BoundedVecDeque::with_capacity(3, 3);
let mut last = -1;
let mut count_incr = 0;
for line in buf.lines() {
if let Ok(l) = line {
if let Ok(value) = i32::from_str_radix(&l, 10) {
deque.push_back(value);
}
if deque.is_full() {
eprintln!("{:?}", deque);
let sum = deque.iter().sum();
if last > -1 && sum > last {
count_incr += 1;
}
last = sum;
}
}
}
println!("number of increases: {}", count_incr);
Ok(())
}

View File

@ -1,48 +0,0 @@
use std::fs::File;
use std::io;
use std::io::prelude::*;
pub struct Position {
pub linear: i32,
pub depth: i32,
aim: i32,
}
pub fn track_position(filename: &str) -> io::Result<Position> {
let f = File::open(filename)?;
let buf = io::BufReader::new(f);
let mut pos = Position {
linear: 0,
depth: 0,
aim: 0,
};
for line in buf.lines() {
if let Ok(line) = line {
let mut parts = line.split_whitespace();
if let Some(op) = parts.next() {
if let Some(value) = parts.next() {
if let Ok(value) = i32::from_str_radix(value, 10) {
match op {
"forward" => {
pos.linear += value;
pos.depth += pos.aim * value;
}
"down" => {
pos.aim += value;
}
"up" => {
pos.aim -= value;
}
_ => {
eprintln!("Invalid operation: {}", op);
}
}
} else {
eprintln!("Invalid value: {}", value);
}
}
}
}
}
Ok(pos)
}

View File

@ -1,29 +0,0 @@
use std::fs::File;
use std::io;
use std::io::prelude::*;
use bounded_vec_deque::BoundedVecDeque;
pub fn count_increases(filename: &str, window: usize) -> io::Result<i32> {
let f = File::open(filename)?;
let buf = io::BufReader::new(f);
let mut deque: BoundedVecDeque<i32> =
BoundedVecDeque::with_capacity(window, window);
let mut last = -1;
let mut count_incr = 0;
for line in buf.lines() {
if let Ok(l) = line {
if let Ok(value) = i32::from_str_radix(&l, 10) {
deque.push_back(value);
}
if deque.is_full() {
let sum = deque.iter().sum();
if last > -1 && sum > last {
count_incr += 1;
}
last = sum;
}
}
}
Ok(count_incr)
}