-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |