mirror of
https://gitlab.com/MisterBiggs/aoc2021.git
synced 2025-06-16 15:06:55 +00:00
messed up my structure
This commit is contained in:
parent
ece33d6fd3
commit
1083237339
@ -1,9 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "day2"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
serde = "1.0.130"
|
|
@ -1,3 +1,57 @@
|
|||||||
|
use std::fs;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
let input = fs::read_to_string("./input.txt").expect("Could not read file");
|
||||||
|
|
||||||
|
let commands: Vec<(&str, i32)> = input
|
||||||
|
.lines()
|
||||||
|
.map(|s| {
|
||||||
|
let mut split = s.split_whitespace();
|
||||||
|
(
|
||||||
|
split.next().unwrap(),
|
||||||
|
split.next().unwrap().parse::<i32>().unwrap(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
part1(&commands);
|
||||||
|
part2(&commands);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part1(commands: &Vec<(&str, i32)>) {
|
||||||
|
let mut position = 0;
|
||||||
|
let mut depth = 0;
|
||||||
|
|
||||||
|
for (dir, amt) in commands.iter().map(|a| *a) {
|
||||||
|
let change = match dir {
|
||||||
|
"forward" => (amt, 0),
|
||||||
|
"up" => (0, -amt),
|
||||||
|
"down" => (0, amt),
|
||||||
|
_ => (0, 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
position += change.0;
|
||||||
|
depth += change.1;
|
||||||
|
}
|
||||||
|
println!("Part 1: {}", position * depth);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn part2(commands: &Vec<(&str, i32)>) {
|
||||||
|
let mut position = 0;
|
||||||
|
let mut depth = 0;
|
||||||
|
let mut aim = 0;
|
||||||
|
|
||||||
|
for (dir, amt) in commands.iter().map(|a| *a) {
|
||||||
|
let change = match dir {
|
||||||
|
"forward" => (amt, 0),
|
||||||
|
"up" => (0, -amt),
|
||||||
|
"down" => (0, amt),
|
||||||
|
_ => (0, 0),
|
||||||
|
};
|
||||||
|
|
||||||
|
aim += change.1;
|
||||||
|
position += change.0;
|
||||||
|
depth += aim * change.0;
|
||||||
|
}
|
||||||
|
println!("Part 2: {}", position * depth);
|
||||||
}
|
}
|
||||||
|
@ -1,57 +0,0 @@
|
|||||||
use std::fs;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let input = fs::read_to_string("./input.txt").expect("Could not read file");
|
|
||||||
|
|
||||||
let commands: Vec<(&str, i32)> = input
|
|
||||||
.lines()
|
|
||||||
.map(|s| {
|
|
||||||
let mut split = s.split_whitespace();
|
|
||||||
(
|
|
||||||
split.next().unwrap(),
|
|
||||||
split.next().unwrap().parse::<i32>().unwrap(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
part1(&commands);
|
|
||||||
part2(&commands);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn part1(commands: &Vec<(&str, i32)>) {
|
|
||||||
let mut position = 0;
|
|
||||||
let mut depth = 0;
|
|
||||||
|
|
||||||
for (dir, amt) in commands.iter().map(|a| *a) {
|
|
||||||
let change = match dir {
|
|
||||||
"forward" => (amt, 0),
|
|
||||||
"up" => (0, -amt),
|
|
||||||
"down" => (0, amt),
|
|
||||||
_ => (0, 0),
|
|
||||||
};
|
|
||||||
|
|
||||||
position += change.0;
|
|
||||||
depth += change.1;
|
|
||||||
}
|
|
||||||
println!("Part 1: {}", position * depth);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn part2(commands: &Vec<(&str, i32)>) {
|
|
||||||
let mut position = 0;
|
|
||||||
let mut depth = 0;
|
|
||||||
let mut aim = 0;
|
|
||||||
|
|
||||||
for (dir, amt) in commands.iter().map(|a| *a) {
|
|
||||||
let change = match dir {
|
|
||||||
"forward" => (amt, 0),
|
|
||||||
"up" => (0, -amt),
|
|
||||||
"down" => (0, amt),
|
|
||||||
_ => (0, 0),
|
|
||||||
};
|
|
||||||
|
|
||||||
aim += change.1;
|
|
||||||
position += change.0;
|
|
||||||
depth += aim * change.0;
|
|
||||||
}
|
|
||||||
println!("Part 2: {}", position * depth);
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user