K4JWYNNZWUND4QKR75SL2U3VL2KLRLML55AEGYZNDXK5ITHQKUZAC
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "advent"
version = "1.0.0"
dependencies = [
"clap",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[package]
name = "advent"
version = "1.0.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.34.0"
forward 8
down 9
up 1
forward 2
down 6
forward 6
down 5
down 4
down 2
forward 3
forward 8
down 6
up 3
up 3
down 5
up 8
forward 8
up 6
forward 4
down 8
up 2
up 6
forward 7
up 6
down 6
down 1
forward 8
up 7
forward 9
forward 5
forward 8
forward 5
down 3
forward 5
up 1
forward 6
forward 3
forward 3
down 2
forward 4
down 3
forward 6
forward 2
forward 1
down 2
forward 1
down 1
up 8
down 4
forward 3
up 3
down 5
down 2
forward 7
forward 1
down 9
up 1
down 1
forward 2
forward 1
forward 1
forward 7
up 5
forward 2
down 6
down 7
down 3
down 1
forward 4
forward 5
down 3
forward 4
down 5
forward 3
forward 4
forward 2
up 8
down 7
up 3
forward 2
down 5
forward 9
forward 5
up 1
down 5
down 6
forward 2
forward 5
down 6
down 1
up 8
up 9
down 7
down 5
forward 2
forward 7
forward 7
up 6
down 4
up 6
forward 2
down 4
down 3
down 4
forward 2
down 2
down 1
down 4
up 4
forward 7
forward 6
forward 2
forward 5
forward 1
forward 7
forward 2
down 3
forward 7
forward 2
up 4
up 5
down 1
down 2
down 3
down 7
up 8
forward 4
down 5
down 4
forward 3
down 6
forward 5
up 5
forward 6
down 1
down 7
forward 1
forward 6
up 9
forward 2
down 1
forward 5
forward 2
forward 6
forward 8
forward 6
down 8
forward 3
down 9
up 8
forward 9
down 7
down 6
down 1
up 5
down 6
forward 7
up 9
forward 8
forward 3
forward 3
forward 2
down 6
up 1
up 5
forward 7
down 2
down 5
down 1
down 4
forward 8
forward 2
forward 8
up 5
forward 7
up 7
forward 6
forward 9
up 1
down 4
forward 8
forward 4
down 9
up 6
down 8
up 9
down 2
down 2
down 9
up 3
down 7
down 2
up 9
forward 9
down 5
forward 2
forward 1
forward 2
down 4
forward 4
down 2
forward 8
up 3
forward 6
up 7
up 9
forward 4
up 9
down 5
forward 5
up 6
down 2
down 4
forward 1
down 2
forward 3
down 7
down 1
down 5
forward 5
up 5
forward 3
down 5
forward 7
down 3
down 2
up 9
down 4
forward 4
down 4
forward 1
up 9
down 8
forward 2
down 7
forward 8
down 5
up 2
down 9
down 2
up 4
up 2
forward 8
forward 3
forward 4
down 1
down 3
forward 3
down 8
forward 7
down 7
forward 5
forward 5
up 3
down 6
forward 1
down 4
forward 2
down 4
up 2
down 3
forward 4
forward 6
up 7
down 3
forward 7
forward 1
down 1
up 3
down 7
up 3
up 8
up 7
up 7
forward 7
down 2
forward 6
forward 7
up 3
down 3
up 9
down 6
down 7
down 7
up 5
forward 6
forward 2
up 9
forward 2
down 3
down 3
forward 5
forward 3
forward 6
forward 6
down 7
forward 2
down 3
up 4
forward 1
forward 5
down 5
up 1
down 9
forward 3
up 6
forward 5
down 4
forward 9
up 5
forward 7
up 7
forward 9
forward 2
forward 6
forward 3
forward 3
up 7
down 1
down 1
forward 1
down 8
down 2
forward 4
up 7
forward 7
down 5
down 7
up 6
down 4
forward 4
forward 4
up 7
down 9
forward 6
forward 1
down 7
forward 5
forward 2
forward 1
up 7
down 9
forward 2
down 7
up 8
forward 1
forward 4
up 4
forward 1
down 9
down 5
down 8
forward 8
forward 8
down 8
down 3
down 5
down 6
up 9
forward 1
down 1
forward 1
forward 1
down 6
forward 3
down 1
forward 9
forward 3
up 7
down 9
forward 5
down 4
up 9
down 2
down 5
down 6
forward 1
up 2
forward 9
up 7
forward 2
forward 8
down 5
down 6
down 8
forward 4
down 4
forward 9
forward 3
forward 6
forward 9
up 2
down 8
up 2
up 6
up 5
down 5
down 7
forward 1
up 6
forward 9
up 4
forward 4
up 2
forward 3
down 8
down 1
up 5
forward 5
down 3
down 4
down 1
forward 3
up 2
forward 3
down 1
forward 8
up 3
forward 4
down 3
forward 6
down 9
down 7
forward 3
forward 5
forward 1
forward 3
up 8
forward 2
forward 2
forward 7
up 9
forward 4
down 1
down 6
forward 8
forward 5
up 4
up 5
up 9
forward 9
up 8
down 3
up 5
forward 1
forward 5
up 9
forward 8
forward 4
forward 3
forward 1
forward 6
down 2
up 8
down 7
down 1
down 5
up 1
forward 9
down 2
down 4
forward 3
forward 1
down 7
up 9
up 7
down 2
down 4
down 1
forward 9
up 4
down 9
down 9
down 4
down 9
forward 1
down 2
forward 8
up 4
up 3
forward 4
down 5
forward 8
forward 2
forward 4
up 3
up 2
up 3
forward 4
forward 4
forward 6
down 7
down 8
up 3
down 9
down 5
up 7
up 1
down 3
down 4
up 5
up 9
down 2
up 8
forward 9
down 3
forward 4
down 5
down 1
forward 9
forward 2
up 3
down 9
down 8
up 7
forward 2
down 9
up 8
up 2
down 3
forward 3
forward 3
forward 3
forward 4
down 7
up 2
forward 3
forward 7
forward 5
forward 7
down 6
down 9
down 6
down 8
up 4
down 4
down 2
up 1
down 3
forward 7
forward 5
down 7
forward 1
up 7
forward 9
down 8
down 1
down 3
down 1
up 8
forward 7
up 5
forward 6
down 1
down 8
up 1
up 9
forward 7
down 9
forward 3
forward 8
forward 7
down 3
down 5
up 3
up 7
up 3
up 6
up 8
down 3
forward 7
down 7
forward 9
up 9
down 1
down 3
down 8
down 6
forward 1
up 6
down 1
up 4
down 2
forward 9
up 8
up 2
down 6
down 1
forward 2
forward 6
forward 1
forward 2
up 6
down 7
down 8
forward 9
up 2
down 6
down 8
down 5
up 6
forward 6
down 5
forward 4
forward 2
down 1
down 4
up 5
down 5
down 1
down 9
down 1
down 1
forward 1
forward 5
up 4
down 5
down 3
down 2
down 7
forward 5
forward 6
forward 2
up 9
up 3
forward 6
down 4
forward 8
forward 4
forward 9
down 6
down 9
down 1
forward 6
down 8
down 7
forward 5
down 4
forward 5
forward 5
up 3
down 1
down 4
forward 1
forward 7
down 5
down 4
forward 7
forward 3
forward 4
down 4
forward 8
up 2
up 6
down 9
up 1
down 8
up 2
up 6
forward 5
forward 7
up 4
down 4
down 8
forward 5
up 4
down 6
forward 9
up 4
forward 7
up 7
down 3
down 2
down 2
down 6
down 4
forward 4
down 3
down 4
down 4
down 8
up 2
down 3
forward 5
forward 5
up 5
forward 1
down 7
forward 5
forward 7
forward 9
forward 4
down 1
down 1
up 5
down 6
down 8
down 9
down 9
forward 7
down 6
forward 3
down 7
up 3
forward 5
forward 5
up 6
down 5
down 8
down 5
down 1
down 7
forward 8
up 9
forward 2
up 1
down 9
down 2
forward 7
up 1
down 5
forward 3
up 6
forward 9
up 9
down 3
forward 2
up 6
down 6
up 8
down 3
up 5
forward 9
up 3
forward 4
forward 6
down 3
forward 4
down 5
down 7
down 7
forward 9
forward 6
down 9
up 1
forward 2
down 8
down 7
up 7
forward 6
up 5
down 6
forward 6
down 3
forward 2
down 7
forward 2
down 3
down 8
forward 4
up 8
forward 7
down 4
forward 3
down 3
forward 6
down 7
down 7
forward 8
forward 9
down 8
down 4
forward 5
down 1
forward 9
down 7
up 3
down 6
down 4
down 9
forward 3
down 1
forward 1
down 5
forward 1
forward 9
down 9
forward 5
down 1
up 9
down 3
down 2
up 6
forward 4
down 6
down 2
forward 5
up 8
down 7
up 6
down 1
forward 3
forward 4
down 9
down 2
forward 5
forward 8
up 2
forward 9
up 5
down 1
forward 1
forward 6
forward 9
forward 2
down 8
forward 1
up 8
forward 1
forward 8
up 5
forward 3
down 4
up 8
down 6
up 1
forward 2
forward 4
up 6
forward 1
forward 4
up 6
down 8
up 7
forward 5
up 7
down 9
down 2
up 5
forward 2
down 7
forward 1
forward 1
forward 3
down 6
forward 7
up 2
up 1
forward 1
up 5
down 1
forward 7
up 9
forward 7
down 6
up 4
down 5
up 2
down 4
up 3
down 6
down 7
down 7
up 1
forward 4
forward 1
up 2
down 7
down 5
forward 7
forward 5
forward 2
down 4
down 6
down 8
forward 9
forward 8
up 9
forward 6
down 6
down 6
down 8
up 5
forward 6
down 6
forward 5
down 4
down 6
down 2
up 3
forward 1
forward 8
down 2
forward 9
forward 7
forward 8
forward 1
down 8
forward 1
down 1
forward 6
forward 4
forward 5
up 9
up 1
down 7
up 8
down 5
down 2
forward 9
up 2
forward 6
down 7
down 7
down 2
down 6
down 7
forward 9
up 2
down 6
forward 8
forward 1
down 3
down 2
forward 3
forward 7
down 3
down 1
down 4
down 9
forward 1
down 6
down 8
down 4
down 5
down 5
up 9
forward 7
down 9
up 3
up 5
up 1
forward 2
forward 9
down 3
forward 1
up 3
forward 6
up 2
forward 7
forward 5
forward 8
forward 7
forward 8
down 5
up 3
forward 5
forward 8
forward 1
forward 5
forward 7
forward 2
down 6
down 6
down 6
down 2
down 2
up 1
down 1
forward 5
forward 7
down 4
forward 4
up 1
forward 7
forward 8
down 5
down 2
down 2
down 5
forward 3
forward 7
down 5
forward 2
forward 8
down 3
forward 6
up 1
forward 7
forward 4
forward 1
forward 9
forward 2
down 1
down 1
down 8
forward 4
down 6
forward 7
forward 2
extern crate clap;
use clap::{Arg, App};
mod types;
use types::{Direction, Movement, get_direction_by_name};
use std::fs::File;
use std::io::{self, BufRead, BufReader, Lines, Error};
use std::path::Path;
fn read_testcase<P>(filename: P) -> io::Result<Vec<Movement>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
parse_testcase(BufReader::new(file).lines())
}
fn parse_testcase(content: Lines<BufReader<File>>) -> Result<Vec<Movement>, Error> {
Ok(content
.filter(Result::is_ok)
.map(Result::unwrap)
.map(split_line)
.map(|[direction, amount]| Movement{amount:amount.parse::<i32>().unwrap(), direction:get_direction_by_name(direction).unwrap()})
.collect())
}
fn split_line(line: String) -> [String; 2] {
match line.split_once(" ") {
Some(x) => [x.0.to_string(), x.1.to_string()],
None => ["up".to_string(), "0".to_string()]
}
}
fn solve(test_case: Vec<Movement>) -> Result<i32, Error> {
Ok(test_case
.into_iter()
.fold([0, 0], apply_step)
.iter()
.product())
}
fn apply_step(current: [i32; 2], step: Movement) -> [i32; 2] {
match step.direction {
Direction::Forward => [current[0] + step.amount, current[1]],
Direction::Down => [current[0], current[1] + step.amount],
Direction::Up => [current[0], current[1] - step.amount]
}
}
fn print_solution(result: Result<i32, Error>) {
match result {
Ok(solution) => print!("{}\n", solution),
Err(_) => print!("Error solving for provided test case")
};
}
fn main() {
let matches = App::new("Advent of Code")
.version("1.0")
.author("Salvador P. <sisekeom@protonmail.com>")
.about("Solution for advent of code 2021 1.1")
.arg(Arg::with_name("filepath")
.help("path to the test case file")
.required(true)
.index(1))
.get_matches();
let filepath = matches.value_of("filepath").unwrap();
read_testcase(filepath).map(solve).map(print_solution).unwrap();
}
pub struct Movement {
pub amount: i32,
pub direction: Direction
}
pub enum Direction {
Forward,
Down,
Up,
}
pub fn get_direction_by_name(name: String) -> Result<Direction, &'static str> {
match &name[..] {
"forward" => Ok(Direction::Forward),
"down" => Ok(Direction::Down),
"up" => Ok(Direction::Up),
_ => Err("Direction is not valid")
}
}
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "advent"
version = "1.0.0"
dependencies = [
"clap",
]
[[package]]
name = "ansi_term"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi",
"libc",
"winapi",
]
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "clap"
version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"textwrap",
"unicode-width",
"vec_map",
]
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
[[package]]
name = "strsim"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "textwrap"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
dependencies = [
"unicode-width",
]
[[package]]
name = "unicode-width"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[package]
name = "advent"
version = "1.0.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "2.34.0"
forward 8
down 9
up 1
forward 2
down 6
forward 6
down 5
down 4
down 2
forward 3
forward 8
down 6
up 3
up 3
down 5
up 8
forward 8
up 6
forward 4
down 8
up 2
up 6
forward 7
up 6
down 6
down 1
forward 8
up 7
forward 9
forward 5
forward 8
forward 5
down 3
forward 5
up 1
forward 6
forward 3
forward 3
down 2
forward 4
down 3
forward 6
forward 2
forward 1
down 2
forward 1
down 1
up 8
down 4
forward 3
up 3
down 5
down 2
forward 7
forward 1
down 9
up 1
down 1
forward 2
forward 1
forward 1
forward 7
up 5
forward 2
down 6
down 7
down 3
down 1
forward 4
forward 5
down 3
forward 4
down 5
forward 3
forward 4
forward 2
up 8
down 7
up 3
forward 2
down 5
forward 9
forward 5
up 1
down 5
down 6
forward 2
forward 5
down 6
down 1
up 8
up 9
down 7
down 5
forward 2
forward 7
forward 7
up 6
down 4
up 6
forward 2
down 4
down 3
down 4
forward 2
down 2
down 1
down 4
up 4
forward 7
forward 6
forward 2
forward 5
forward 1
forward 7
forward 2
down 3
forward 7
forward 2
up 4
up 5
down 1
down 2
down 3
down 7
up 8
forward 4
down 5
down 4
forward 3
down 6
forward 5
up 5
forward 6
down 1
down 7
forward 1
forward 6
up 9
forward 2
down 1
forward 5
forward 2
forward 6
forward 8
forward 6
down 8
forward 3
down 9
up 8
forward 9
down 7
down 6
down 1
up 5
down 6
forward 7
up 9
forward 8
forward 3
forward 3
forward 2
down 6
up 1
up 5
forward 7
down 2
down 5
down 1
down 4
forward 8
forward 2
forward 8
up 5
forward 7
up 7
forward 6
forward 9
up 1
down 4
forward 8
forward 4
down 9
up 6
down 8
up 9
down 2
down 2
down 9
up 3
down 7
down 2
up 9
forward 9
down 5
forward 2
forward 1
forward 2
down 4
forward 4
down 2
forward 8
up 3
forward 6
up 7
up 9
forward 4
up 9
down 5
forward 5
up 6
down 2
down 4
forward 1
down 2
forward 3
down 7
down 1
down 5
forward 5
up 5
forward 3
down 5
forward 7
down 3
down 2
up 9
down 4
forward 4
down 4
forward 1
up 9
down 8
forward 2
down 7
forward 8
down 5
up 2
down 9
down 2
up 4
up 2
forward 8
forward 3
forward 4
down 1
down 3
forward 3
down 8
forward 7
down 7
forward 5
forward 5
up 3
down 6
forward 1
down 4
forward 2
down 4
up 2
down 3
forward 4
forward 6
up 7
down 3
forward 7
forward 1
down 1
up 3
down 7
up 3
up 8
up 7
up 7
forward 7
down 2
forward 6
forward 7
up 3
down 3
up 9
down 6
down 7
down 7
up 5
forward 6
forward 2
up 9
forward 2
down 3
down 3
forward 5
forward 3
forward 6
forward 6
down 7
forward 2
down 3
up 4
forward 1
forward 5
down 5
up 1
down 9
forward 3
up 6
forward 5
down 4
forward 9
up 5
forward 7
up 7
forward 9
forward 2
forward 6
forward 3
forward 3
up 7
down 1
down 1
forward 1
down 8
down 2
forward 4
up 7
forward 7
down 5
down 7
up 6
down 4
forward 4
forward 4
up 7
down 9
forward 6
forward 1
down 7
forward 5
forward 2
forward 1
up 7
down 9
forward 2
down 7
up 8
forward 1
forward 4
up 4
forward 1
down 9
down 5
down 8
forward 8
forward 8
down 8
down 3
down 5
down 6
up 9
forward 1
down 1
forward 1
forward 1
down 6
forward 3
down 1
forward 9
forward 3
up 7
down 9
forward 5
down 4
up 9
down 2
down 5
down 6
forward 1
up 2
forward 9
up 7
forward 2
forward 8
down 5
down 6
down 8
forward 4
down 4
forward 9
forward 3
forward 6
forward 9
up 2
down 8
up 2
up 6
up 5
down 5
down 7
forward 1
up 6
forward 9
up 4
forward 4
up 2
forward 3
down 8
down 1
up 5
forward 5
down 3
down 4
down 1
forward 3
up 2
forward 3
down 1
forward 8
up 3
forward 4
down 3
forward 6
down 9
down 7
forward 3
forward 5
forward 1
forward 3
up 8
forward 2
forward 2
forward 7
up 9
forward 4
down 1
down 6
forward 8
forward 5
up 4
up 5
up 9
forward 9
up 8
down 3
up 5
forward 1
forward 5
up 9
forward 8
forward 4
forward 3
forward 1
forward 6
down 2
up 8
down 7
down 1
down 5
up 1
forward 9
down 2
down 4
forward 3
forward 1
down 7
up 9
up 7
down 2
down 4
down 1
forward 9
up 4
down 9
down 9
down 4
down 9
forward 1
down 2
forward 8
up 4
up 3
forward 4
down 5
forward 8
forward 2
forward 4
up 3
up 2
up 3
forward 4
forward 4
forward 6
down 7
down 8
up 3
down 9
down 5
up 7
up 1
down 3
down 4
up 5
up 9
down 2
up 8
forward 9
down 3
forward 4
down 5
down 1
forward 9
forward 2
up 3
down 9
down 8
up 7
forward 2
down 9
up 8
up 2
down 3
forward 3
forward 3
forward 3
forward 4
down 7
up 2
forward 3
forward 7
forward 5
forward 7
down 6
down 9
down 6
down 8
up 4
down 4
down 2
up 1
down 3
forward 7
forward 5
down 7
forward 1
up 7
forward 9
down 8
down 1
down 3
down 1
up 8
forward 7
up 5
forward 6
down 1
down 8
up 1
up 9
forward 7
down 9
forward 3
forward 8
forward 7
down 3
down 5
up 3
up 7
up 3
up 6
up 8
down 3
forward 7
down 7
forward 9
up 9
down 1
down 3
down 8
down 6
forward 1
up 6
down 1
up 4
down 2
forward 9
up 8
up 2
down 6
down 1
forward 2
forward 6
forward 1
forward 2
up 6
down 7
down 8
forward 9
up 2
down 6
down 8
down 5
up 6
forward 6
down 5
forward 4
forward 2
down 1
down 4
up 5
down 5
down 1
down 9
down 1
down 1
forward 1
forward 5
up 4
down 5
down 3
down 2
down 7
forward 5
forward 6
forward 2
up 9
up 3
forward 6
down 4
forward 8
forward 4
forward 9
down 6
down 9
down 1
forward 6
down 8
down 7
forward 5
down 4
forward 5
forward 5
up 3
down 1
down 4
forward 1
forward 7
down 5
down 4
forward 7
forward 3
forward 4
down 4
forward 8
up 2
up 6
down 9
up 1
down 8
up 2
up 6
forward 5
forward 7
up 4
down 4
down 8
forward 5
up 4
down 6
forward 9
up 4
forward 7
up 7
down 3
down 2
down 2
down 6
down 4
forward 4
down 3
down 4
down 4
down 8
up 2
down 3
forward 5
forward 5
up 5
forward 1
down 7
forward 5
forward 7
forward 9
forward 4
down 1
down 1
up 5
down 6
down 8
down 9
down 9
forward 7
down 6
forward 3
down 7
up 3
forward 5
forward 5
up 6
down 5
down 8
down 5
down 1
down 7
forward 8
up 9
forward 2
up 1
down 9
down 2
forward 7
up 1
down 5
forward 3
up 6
forward 9
up 9
down 3
forward 2
up 6
down 6
up 8
down 3
up 5
forward 9
up 3
forward 4
forward 6
down 3
forward 4
down 5
down 7
down 7
forward 9
forward 6
down 9
up 1
forward 2
down 8
down 7
up 7
forward 6
up 5
down 6
forward 6
down 3
forward 2
down 7
forward 2
down 3
down 8
forward 4
up 8
forward 7
down 4
forward 3
down 3
forward 6
down 7
down 7
forward 8
forward 9
down 8
down 4
forward 5
down 1
forward 9
down 7
up 3
down 6
down 4
down 9
forward 3
down 1
forward 1
down 5
forward 1
forward 9
down 9
forward 5
down 1
up 9
down 3
down 2
up 6
forward 4
down 6
down 2
forward 5
up 8
down 7
up 6
down 1
forward 3
forward 4
down 9
down 2
forward 5
forward 8
up 2
forward 9
up 5
down 1
forward 1
forward 6
forward 9
forward 2
down 8
forward 1
up 8
forward 1
forward 8
up 5
forward 3
down 4
up 8
down 6
up 1
forward 2
forward 4
up 6
forward 1
forward 4
up 6
down 8
up 7
forward 5
up 7
down 9
down 2
up 5
forward 2
down 7
forward 1
forward 1
forward 3
down 6
forward 7
up 2
up 1
forward 1
up 5
down 1
forward 7
up 9
forward 7
down 6
up 4
down 5
up 2
down 4
up 3
down 6
down 7
down 7
up 1
forward 4
forward 1
up 2
down 7
down 5
forward 7
forward 5
forward 2
down 4
down 6
down 8
forward 9
forward 8
up 9
forward 6
down 6
down 6
down 8
up 5
forward 6
down 6
forward 5
down 4
down 6
down 2
up 3
forward 1
forward 8
down 2
forward 9
forward 7
forward 8
forward 1
down 8
forward 1
down 1
forward 6
forward 4
forward 5
up 9
up 1
down 7
up 8
down 5
down 2
forward 9
up 2
forward 6
down 7
down 7
down 2
down 6
down 7
forward 9
up 2
down 6
forward 8
forward 1
down 3
down 2
forward 3
forward 7
down 3
down 1
down 4
down 9
forward 1
down 6
down 8
down 4
down 5
down 5
up 9
forward 7
down 9
up 3
up 5
up 1
forward 2
forward 9
down 3
forward 1
up 3
forward 6
up 2
forward 7
forward 5
forward 8
forward 7
forward 8
down 5
up 3
forward 5
forward 8
forward 1
forward 5
forward 7
forward 2
down 6
down 6
down 6
down 2
down 2
up 1
down 1
forward 5
forward 7
down 4
forward 4
up 1
forward 7
forward 8
down 5
down 2
down 2
down 5
forward 3
forward 7
down 5
forward 2
forward 8
down 3
forward 6
up 1
forward 7
forward 4
forward 1
forward 9
forward 2
down 1
down 1
down 8
forward 4
down 6
forward 7
forward 2
extern crate clap;
use clap::{Arg, App};
mod types;
use types::{Movement, Position, get_direction_by_name};
use std::fs::File;
use std::io::{self, BufRead, BufReader, Lines, Error};
use std::path::Path;
fn read_testcase<P>(filename: P) -> io::Result<Vec<Movement>>
where P: AsRef<Path>, {
let file = File::open(filename)?;
parse_testcase(BufReader::new(file).lines())
}
fn parse_testcase(content: Lines<BufReader<File>>) -> Result<Vec<Movement>, Error> {
Ok(content
.filter(Result::is_ok)
.map(Result::unwrap)
.map(split_line)
.map(|[direction, amount]| Movement{amount:amount.parse::<i32>().unwrap(), direction:get_direction_by_name(direction).unwrap()})
.collect())
}
fn split_line(line: String) -> [String; 2] {
match line.split_once(" ") {
Some(x) => [x.0.to_string(), x.1.to_string()],
None => ["up".to_string(), "0".to_string()]
}
}
fn solve(test_case: Vec<Movement>) -> Result<i32, Error> {
Ok(test_case
.into_iter()
.fold(Position{x: 0, y: 0, aim: 0}, |position, step| position.apply_step(step))
.product())
}
fn print_solution(result: Result<i32, Error>) {
match result {
Ok(solution) => print!("{}\n", solution),
Err(_) => print!("Error solving for provided test case")
};
}
fn main() {
let matches = App::new("Advent of Code")
.version("1.0")
.author("Salvador P. <sisekeom@protonmail.com>")
.about("Solution for advent of code 2021 1.1")
.arg(Arg::with_name("filepath")
.help("path to the test case file")
.required(true)
.index(1))
.get_matches();
let filepath = matches.value_of("filepath").unwrap();
read_testcase(filepath).map(solve).map(print_solution).unwrap();
}
pub struct Movement {
pub amount: i32,
pub direction: Direction
}
pub struct Position {
pub x: i32,
pub y: i32,
pub aim: i32
}
impl Position {
pub fn product(&self) -> i32 {
self.x * self.y
}
pub fn apply_step(&self, step: Movement) -> Position {
match step.direction {
Direction::Forward => Position{x: self.x + step.amount, y: self.y + (self.aim * step.amount), aim: self.aim},
Direction::Down => Position{x: self.x, y: self.y, aim: self.aim + step.amount},
Direction::Up => Position{x: self.x, y: self.y, aim: self.aim - step.amount}
}
}
}
pub enum Direction {
Forward,
Down,
Up,
}
pub fn get_direction_by_name(name: String) -> Result<Direction, &'static str> {
match &name[..] {
"forward" => Ok(Direction::Forward),
"down" => Ok(Direction::Down),
"up" => Ok(Direction::Up),
_ => Err("Direction is not valid")
}
}