summaryrefslogtreecommitdiff
path: root/2021/src
diff options
context:
space:
mode:
authorBond_009 <bond.009@outlook.com>2022-12-01 22:30:22 +0100
committerBond_009 <bond.009@outlook.com>2022-12-01 22:30:22 +0100
commitbaf4910870a6e8999802b9a4a22eabd4142a34e3 (patch)
tree2d11443dc21e53bd0d99d015cf789937d6d95862 /2021/src
parent49d0c908f24b2c193c9deed1716fe36061ba26a1 (diff)
Move all Advent of Codes into one repo
Diffstat (limited to '2021/src')
-rw-r--r--2021/src/day01.rs39
-rw-r--r--2021/src/day02.rs67
-rw-r--r--2021/src/main.rs22
3 files changed, 128 insertions, 0 deletions
diff --git a/2021/src/day01.rs b/2021/src/day01.rs
new file mode 100644
index 0000000..7b1e6a9
--- /dev/null
+++ b/2021/src/day01.rs
@@ -0,0 +1,39 @@
+use std::error::Error;
+use std::fs;
+use std::path::Path;
+
+fn get_input(path: &Path) -> Result<Vec<u16>, Box<dyn Error>> {
+ let data = fs::read_to_string(path)?;
+ Ok(data.lines().filter_map(|x| x.parse::<u16>().ok()).collect())
+}
+
+#[cfg(part1)]
+pub fn solve(path: &Path) -> Result<(), Box<dyn Error>> {
+ let values = get_input(path)?;
+ let mut last = u16::MAX;
+ let mut larger = 0;
+ for v in values {
+ if v > last {
+ larger += 1;
+ }
+
+ last = v;
+ }
+
+ println!("{}", larger);
+ Ok(())
+}
+
+#[cfg(part2)]
+pub fn solve(path: &Path) -> Result<(), Box<dyn Error>> {
+ let values = get_input(path)?;
+ let mut increase = 0;
+ for i in 0..(values.len() - 3) {
+ if values[i] < values[i + 3] {
+ increase += 1;
+ }
+ }
+
+ println!("{}", increase);
+ Ok(())
+}
diff --git a/2021/src/day02.rs b/2021/src/day02.rs
new file mode 100644
index 0000000..e64d2e9
--- /dev/null
+++ b/2021/src/day02.rs
@@ -0,0 +1,67 @@
+use std::str::FromStr;
+use std::error::Error;
+use std::fs;
+use std::path::Path;
+
+#[derive(PartialEq, Debug)]
+enum Command {
+ Forward(i32),
+ Down(i32),
+ Up(i32)
+}
+
+#[derive(Debug)]
+struct ParseCommandError;
+
+impl FromStr for Command {
+ type Err = ParseCommandError;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ let i = s.find(' ').ok_or(ParseCommandError)?;
+ let num = s[i + 1..].parse::<i32>().map_err(|_| ParseCommandError)?;
+ match &s[..i] {
+ "forward" => Ok(Command::Forward(num)),
+ "down" => Ok(Command::Down(num)),
+ "up" => Ok(Command::Up(num)),
+ _ => Err(ParseCommandError)
+ }
+ }
+}
+
+#[cfg(part1)]
+pub fn solve(path: &Path) -> Result<(), Box<dyn Error>> {
+ let data = fs::read_to_string(path)?;
+ let mut hor = 0;
+ let mut depth = 0;
+ for command in data.lines().filter_map(|x| x.parse::<Command>().ok()) {
+ match command {
+ Command::Forward(v) => hor += v,
+ Command::Down(v) => depth += v,
+ Command::Up(v) => depth -= v
+ }
+ }
+
+ println!("{}", hor * depth);
+ Ok(())
+}
+
+#[cfg(part2)]
+pub fn solve(path: &Path) -> Result<(), Box<dyn Error>> {
+ let data = fs::read_to_string(path)?;
+ let mut aim = 0;
+ let mut hor = 0;
+ let mut depth = 0;
+ for command in data.lines().filter_map(|x| x.parse::<Command>().ok()) {
+ match command {
+ Command::Forward(v) => {
+ hor += v;
+ depth += aim * v;
+ },
+ Command::Down(v) => aim += v,
+ Command::Up(v) => aim -= v
+ }
+ }
+
+ println!("{}", hor * depth);
+ Ok(())
+}
diff --git a/2021/src/main.rs b/2021/src/main.rs
new file mode 100644
index 0000000..ae5f8f2
--- /dev/null
+++ b/2021/src/main.rs
@@ -0,0 +1,22 @@
+use std::env;
+use std::error::Error;
+use std::path::Path;
+
+#[cfg(day01)]
+use day01::solve;
+
+#[cfg(day02)]
+use day02::solve;
+
+#[cfg(day01)]
+mod day01;
+
+#[cfg(day02)]
+mod day02;
+
+fn main() -> Result<(), Box<dyn Error>> {
+ let args = env::args().last().unwrap();
+ let path = Path::new(&args);
+
+ solve(path)
+}