mirror of
https://codeberg.org/JasterV/aoc_2021.git
synced 2026-04-26 18:40:05 +00:00
day2 puzzle 1
This commit is contained in:
parent
09ea8b7687
commit
6d0452b8e2
6 changed files with 1114 additions and 0 deletions
1
day2/.gitignore
vendored
Normal file
1
day2/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
/target
|
||||
7
day2/Cargo.lock
generated
Normal file
7
day2/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "day2"
|
||||
version = "0.1.0"
|
||||
8
day2/Cargo.toml
Normal file
8
day2/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day2"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
1000
day2/input.txt
Normal file
1000
day2/input.txt
Normal file
File diff suppressed because it is too large
Load diff
41
day2/src/main.rs
Normal file
41
day2/src/main.rs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
mod models;
|
||||
|
||||
use models::{Command, Submarine};
|
||||
use std::fs::File;
|
||||
use std::io::{self, BufRead, BufReader, Lines, Result};
|
||||
use std::iter::Iterator;
|
||||
use std::path::Path;
|
||||
|
||||
static INPUT_PATH: &str = "input.txt";
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let submarine = Submarine { xcord: 0, depth: 0 };
|
||||
let commands = read_commands(INPUT_PATH)?;
|
||||
let submarine = apply_commands(submarine, &commands);
|
||||
println!("First puzzle: {}", submarine.depth * submarine.xcord);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_commands(submarine: Submarine, commands: &[Command]) -> Submarine {
|
||||
commands.iter().copied().fold(submarine, Submarine::exec)
|
||||
}
|
||||
|
||||
fn read_commands(file_path: &str) -> Result<Vec<Command>> {
|
||||
read_lines(file_path).map(lines_to_commands)
|
||||
}
|
||||
|
||||
fn lines_to_commands(lines: Lines<BufReader<File>>) -> Vec<Command> {
|
||||
lines
|
||||
.filter_map(|line| line.ok().map(Command::from))
|
||||
.collect()
|
||||
}
|
||||
|
||||
// The output is wrapped in a Result to allow matching on errors
|
||||
// Returns an Iterator to the Reader of the lines of the file.
|
||||
fn read_lines<P>(filename: P) -> io::Result<Lines<BufReader<File>>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
let file = File::open(filename)?;
|
||||
Ok(io::BufReader::new(file).lines())
|
||||
}
|
||||
57
day2/src/models/mod.rs
Normal file
57
day2/src/models/mod.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Submarine {
|
||||
pub xcord: i32,
|
||||
pub depth: i32,
|
||||
}
|
||||
|
||||
impl Submarine {
|
||||
pub fn exec(submarine: Self, command: Command) -> Self {
|
||||
match command {
|
||||
Command::Forward(unit) => Self::forward(submarine, unit),
|
||||
Command::Down(unit) => Self::down(submarine, unit),
|
||||
Command::Up(unit) => Self::up(submarine, unit),
|
||||
}
|
||||
}
|
||||
|
||||
fn forward(submarine: Self, unit: i32) -> Self {
|
||||
Submarine {
|
||||
xcord: submarine.xcord + unit,
|
||||
depth: submarine.depth,
|
||||
}
|
||||
}
|
||||
|
||||
fn down(submarine: Self, unit: i32) -> Self {
|
||||
Submarine {
|
||||
xcord: submarine.xcord,
|
||||
depth: submarine.depth + unit,
|
||||
}
|
||||
}
|
||||
|
||||
fn up(submarine: Self, unit: i32) -> Self {
|
||||
Submarine {
|
||||
xcord: submarine.xcord,
|
||||
depth: submarine.depth - unit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub enum Command {
|
||||
Forward(i32),
|
||||
Down(i32),
|
||||
Up(i32),
|
||||
}
|
||||
|
||||
impl From<String> for Command {
|
||||
fn from(str: String) -> Self {
|
||||
let words: Vec<&str> = str.split_ascii_whitespace().collect();
|
||||
let action = words[0];
|
||||
let unit: i32 = words[1].parse().unwrap();
|
||||
match action {
|
||||
"forward" => Command::Forward(unit),
|
||||
"down" => Command::Down(unit),
|
||||
"up" => Command::Up(unit),
|
||||
_ => panic!("Invalid command {}", action),
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue