Day 2, part 1
parent
733813e719
commit
e9d49de28d
File diff suppressed because it is too large
Load Diff
16
src/main.rs
16
src/main.rs
|
@ -2,6 +2,7 @@ use std::io;
|
||||||
|
|
||||||
use argh::FromArgs;
|
use argh::FromArgs;
|
||||||
|
|
||||||
|
mod pilot;
|
||||||
mod sonar;
|
mod sonar;
|
||||||
|
|
||||||
#[derive(FromArgs)]
|
#[derive(FromArgs)]
|
||||||
|
@ -15,6 +16,7 @@ struct Arguments {
|
||||||
#[argh(subcommand)]
|
#[argh(subcommand)]
|
||||||
enum SubCommand {
|
enum SubCommand {
|
||||||
Sonar(SonarTracker),
|
Sonar(SonarTracker),
|
||||||
|
Pilot(SubPilot),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(FromArgs)]
|
#[derive(FromArgs)]
|
||||||
|
@ -30,6 +32,15 @@ struct SonarTracker {
|
||||||
window: usize,
|
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<()> {
|
fn main() -> io::Result<()> {
|
||||||
let args: Arguments = argh::from_env();
|
let args: Arguments = argh::from_env();
|
||||||
match args.command {
|
match args.command {
|
||||||
|
@ -37,6 +48,11 @@ fn main() -> io::Result<()> {
|
||||||
let count_incr = sonar::count_increases(&args.input, args.window)?;
|
let count_incr = sonar::count_increases(&args.input, args.window)?;
|
||||||
println!("number of increases: {}", count_incr);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
|
use std::io::prelude::*;
|
||||||
|
|
||||||
|
pub struct Position {
|
||||||
|
pub linear: i32,
|
||||||
|
pub depth: 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,
|
||||||
|
};
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
"down" => {
|
||||||
|
pos.depth += value;
|
||||||
|
}
|
||||||
|
"up" => {
|
||||||
|
pos.depth -= value;
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
eprintln!("Invalid operation: {}", op);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
eprintln!("Invalid value: {}", value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(pos)
|
||||||
|
}
|
Reference in New Issue