mirror of
https://github.com/tomru/adventofcode.git
synced 2026-03-03 14:37:22 +01:00
day02 task2 - still poor but working 😓
This commit is contained in:
@@ -3,8 +3,9 @@ use std::io::{self, BufRead};
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut depth = 0;
|
let mut depth: i32 = 0;
|
||||||
let mut pos_h = 0;
|
let mut pos_h: i32 = 0;
|
||||||
|
let mut aim: i32 = 0;
|
||||||
if let Ok(lines) = read_lines("./data/input") {
|
if let Ok(lines) = read_lines("./data/input") {
|
||||||
for line in lines {
|
for line in lines {
|
||||||
if let Ok(str) = line {
|
if let Ok(str) = line {
|
||||||
@@ -12,21 +13,27 @@ fn main() {
|
|||||||
let direction = words[0];
|
let direction = words[0];
|
||||||
let number = words[1].parse::<i32>().unwrap();
|
let number = words[1].parse::<i32>().unwrap();
|
||||||
|
|
||||||
match direction {
|
if direction == "down" {
|
||||||
"down" => depth = depth + number,
|
aim += number;
|
||||||
"up" => depth = depth - number,
|
} else if direction == "up" {
|
||||||
"forward" => pos_h = pos_h + number,
|
aim -= number;
|
||||||
_ => eprintln!("UNKNOWN!!!!!"),
|
} else if direction == "forward" {
|
||||||
|
depth += number * aim;
|
||||||
|
pos_h += number;
|
||||||
|
} else {
|
||||||
|
eprintln!("UNKNOWN!!!!!")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("{}", depth * pos_h);
|
let result = depth * pos_h;
|
||||||
|
println!("{}", result);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
|
||||||
where P: AsRef<Path>, {
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
let file = File::open(filename)?;
|
let file = File::open(filename)?;
|
||||||
Ok(io::BufReader::new(file).lines())
|
Ok(io::BufReader::new(file).lines())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user