45 lines
1.3 KiB
Rust
45 lines
1.3 KiB
Rust
use std::fs;
|
|
|
|
fn main() {
|
|
// part_1();
|
|
part_2();
|
|
}
|
|
|
|
fn part_1() {
|
|
let contents = fs::read_to_string("./input").expect("Couldn't read file!");
|
|
let position = contents
|
|
.lines()
|
|
.map(|s| {
|
|
let c: Vec<&str> = s.split(" ").collect();
|
|
let num = c[1].parse::<usize>().unwrap();
|
|
return (c[0].to_string(), num);
|
|
})
|
|
.fold((0, 0), |acc, command| match command.0.as_str() {
|
|
"forward" => (acc.0 + command.1, acc.1),
|
|
"up" => (acc.0, acc.1 - command.1),
|
|
"down" => (acc.0, acc.1 + command.1),
|
|
_ => acc,
|
|
});
|
|
|
|
println!("{}", position.0 * position.1);
|
|
}
|
|
|
|
fn part_2() {
|
|
let contents = fs::read_to_string("./input").expect("Couldn't read file!");
|
|
let position = contents
|
|
.lines()
|
|
.map(|s| {
|
|
let c: Vec<&str> = s.split(" ").collect();
|
|
let num = c[1].parse::<usize>().unwrap();
|
|
return (c[0].to_string(), num);
|
|
})
|
|
.fold((0, 0, 0), |acc, command| match command.0.as_str() {
|
|
"forward" => (acc.0 + command.1, acc.1, acc.2 + acc.1 * command.1),
|
|
"up" => (acc.0, acc.1 - command.1, acc.2),
|
|
"down" => (acc.0, acc.1 + command.1, acc.2),
|
|
_ => acc,
|
|
});
|
|
|
|
println!("{}", position.0 * position.2);
|
|
}
|