Implementation of d02 in rust

This commit is contained in:
IamTheFij 2021-12-03 11:01:48 -08:00
parent cfbaaf8126
commit b487214421
5 changed files with 1152 additions and 0 deletions

7
d02/Cargo.lock generated Normal file
View File

@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "d02"
version = "0.1.0"

8
d02/Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "d02"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

1000
d02/input.txt Normal file

File diff suppressed because it is too large Load Diff

6
d02/input_short.txt Normal file
View File

@ -0,0 +1,6 @@
forward 5
down 5
forward 8
up 3
down 8
forward 2

131
d02/src/main.rs Normal file
View File

@ -0,0 +1,131 @@
use std::fmt;
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
where
P: AsRef<Path>,
{
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}
struct Coord(i32, i32);
impl fmt::Display for Coord {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "(x={}, y={})", self.0, self.1)
}
}
fn part1() {
let result = read_lines("input.txt")
.expect("Expected a file to read")
.map(|line| line.expect("Could not read line"))
.map(|line| {
let mut parts = line.split_whitespace();
let direction = parts
.next()
.expect(format!("Missing direction on line: {}", line).as_str());
let distance = parts
.next()
.map(|value| {
value.parse::<i32>().expect(
format!("Expect distance to be a value instead got: {}", value).as_str(),
)
})
.expect(format!("Missing distance on line: {}", line).as_str());
if parts.next().is_some() {
panic!("More than 2 parts in line: {}", line);
}
match direction {
"forward" => Coord(distance, 0),
"down" => Coord(0, -1 * distance),
"up" => Coord(0, distance),
&_ => panic!("Unknown direction {}", direction),
}
})
.reduce(|location, delta| Coord(location.0 + delta.0, location.1 + delta.1))
.expect("No value reduced");
println!(
"Part 1 location: {}, result: {}",
result,
result.0 * result.1 * -1
)
}
struct Position {
location: Coord,
aim: i32,
}
fn part2() {
let result = read_lines("input.txt")
.expect("Expected a file to read")
.map(|line| line.expect("Could not read line"))
.map(|line| {
let mut parts = line.split_whitespace();
let direction = parts
.next()
.expect(format!("Missing direction on line: {}", line).as_str());
let value = parts
.next()
.map(|value| {
value.parse::<i32>().expect(
format!("Expect distance to be a value instead got: {}", value).as_str(),
)
})
.expect(format!("Missing distance on line: {}", line).as_str());
if parts.next().is_some() {
panic!("More than 2 parts in line: {}", line);
}
match direction {
// Increase aim
"down" => Position {
location: Coord(0, 0),
aim: value,
},
// Decrease aim
"up" => Position {
location: Coord(0, 0),
aim: -1 * value,
},
// Increase horiziontal and depty by aim
"forward" => Position {
location: Coord(value, value),
aim: 0,
},
&_ => panic!("Unknown direction {}", direction),
}
})
.fold(
Position {
location: Coord(0, 0),
aim: 0,
},
|position, delta| {
let aim = position.aim + delta.aim;
let x = position.location.0 + delta.location.0;
let y = position.location.1 - (aim * delta.location.1);
let coord = Coord(x, y);
println!("Coord: {}, aim: {}", coord, aim);
Position {
location: coord,
aim: aim,
}
},
);
println!(
"Part 2 location: {}, result: {}",
result.location,
result.location.0 * result.location.1 * -1
)
}
fn main() {
part1();
part2();
}