Skip to content

Commit

Permalink
feat: Day 2 2021, ⭐ ⭐ Rust + Deno
Browse files Browse the repository at this point in the history
  • Loading branch information
icyJoseph committed Dec 2, 2021
1 parent 54a4b3f commit 22d4f08
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 2021/day-2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
authors = ["Joseph Chamochumbi <[email protected]>"]
edition = "2018"
name = "day-2"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aoc = {git = "https://github.com/icyJoseph/advent-of-code.git"}
48 changes: 48 additions & 0 deletions 2021/day-2/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use aoc;

fn parse_num<T: std::str::FromStr>(str: &str) -> T {
match str.trim().parse::<T>() {
Ok(n) => n,
_ => panic!("Error parsing"),
}
}

fn solve(raw: String) -> () {
let rows = raw.trim().split("\n").collect::<Vec<&str>>();

let part_one: (u32, u32) = rows.iter().fold((0, 0), |prev, row| {
let command = row.split(" ").collect::<Vec<&str>>();
let dir: &str = command[0];
let step = parse_num::<u32>(command[1]);

match dir {
"forward" => return (prev.0 + step, prev.1),
"down" => return (prev.0, prev.1 + step),
"up" => return (prev.0, prev.1 - step),
_ => panic!("Missing command"),
}
});

println!("Part One: {}", part_one.0 * part_one.1); // 1746616

let part_two = rows.iter().fold((0, 0, 0), |prev, row| {
let command = row.split(" ").collect::<Vec<&str>>();
let dir: &str = command[0];
let step = parse_num::<u32>(command[1]);

match dir {
"forward" => return (prev.0 + step, prev.1 + prev.2 * step, prev.2),
"down" => return (prev.0, prev.1, prev.2 + step),
"up" => return (prev.0, prev.1, prev.2 - step),
_ => panic!("Missing command"),
}
});

println!("Part Two: {}", part_two.0 * part_two.1); // 1741971043
}

fn main() {
let input = aoc::get_input(2021, 2);

solve(input);
}
57 changes: 57 additions & 0 deletions 2021/deno/day-2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const input = await Deno.readTextFile("./input/day-2.in");

const rows = input.split("\n");

let instructions = rows.map((row) => {
const [key, value] = row.split(" ");

return [key, Number(value)] as const;
});

/**
* Part One
*/

const partOne = instructions.reduce(
(prev, [dir, value]) => {
switch (dir) {
case "forward":
return { ...prev, horizontal: prev.horizontal + value };
case "down":
return { ...prev, depth: prev.depth + value };
case "up":
return { ...prev, depth: prev.depth - value };
default:
return prev;
}
},
{ horizontal: 0, depth: 0 }
);

console.log("Part One:", partOne.horizontal * partOne.depth);

/**
* Part Two
*/

const partTwo = instructions.reduce(
(prev, [dir, value]) => {
switch (dir) {
case "forward":
return {
...prev,
horizontal: prev.horizontal + value,
depth: prev.depth + prev.aim * value
};
case "down":
return { ...prev, aim: prev.aim + value };
case "up":
return { ...prev, aim: prev.aim - value };
default:
return prev;
}
},
{ horizontal: 0, depth: 0, aim: 0 }
);

console.log("Part Two:", partTwo.horizontal * partTwo.depth);

0 comments on commit 22d4f08

Please sign in to comment.