Compare commits
No commits in common. "cf1fb4c7922b0e9044ceea02e8d4948fc006be5b" and "cde23745ef6eb36168dcb48c998bee73fc864373" have entirely different histories.
cf1fb4c792
...
cde23745ef
@ -1,86 +0,0 @@
|
|||||||
use std::str::FromStr;
|
|
||||||
|
|
||||||
use color_eyre::eyre;
|
|
||||||
use eyre::{eyre, ensure};
|
|
||||||
|
|
||||||
use crate::lib::ParseLines;
|
|
||||||
|
|
||||||
|
|
||||||
enum Direction {
|
|
||||||
Forward,
|
|
||||||
Up,
|
|
||||||
Down,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
struct MoveCmd {
|
|
||||||
dir: Direction,
|
|
||||||
value: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
impl<'a> FromStr for MoveCmd {
|
|
||||||
type Err = eyre::Report;
|
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
||||||
let parts: Vec<&str> = s.split_whitespace().collect();
|
|
||||||
ensure!(parts.len() == 2, "Could not parse: \"{}\" (Wrong number of words)", s);
|
|
||||||
|
|
||||||
let dir = match parts[0] {
|
|
||||||
"forward" => Direction::Forward,
|
|
||||||
"up" => Direction::Up,
|
|
||||||
"down" => Direction::Down,
|
|
||||||
_ => return Err(eyre!("Could not parse: \"{}\" (invalid axis)", s)),
|
|
||||||
};
|
|
||||||
|
|
||||||
let value = parts[1].parse::<i32>()?;
|
|
||||||
|
|
||||||
Ok(MoveCmd{dir, value})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn part1(commands: &Vec<MoveCmd>) -> i32 {
|
|
||||||
let mut horiz = 0; // how many units forward
|
|
||||||
let mut vert = 0; // how many units DOWN (so flipped)
|
|
||||||
for cmd in commands {
|
|
||||||
match cmd.dir {
|
|
||||||
Direction::Forward => horiz += cmd.value,
|
|
||||||
Direction::Up => vert -= cmd.value,
|
|
||||||
Direction::Down => vert += cmd.value,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
horiz * vert
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn part2(commands: &Vec<MoveCmd>) -> i32 {
|
|
||||||
let mut horiz = 0;
|
|
||||||
let mut vert = 0;
|
|
||||||
let mut aim = 0;
|
|
||||||
for cmd in commands {
|
|
||||||
match cmd.dir {
|
|
||||||
Direction::Down => aim += cmd.value, // flipped again
|
|
||||||
Direction::Up => aim -= cmd.value,
|
|
||||||
Direction::Forward => {
|
|
||||||
horiz += cmd.value;
|
|
||||||
vert += cmd.value * aim;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
horiz * vert
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
pub fn run(data: &str) -> eyre::Result<()> {
|
|
||||||
let mut commands = Vec::new();
|
|
||||||
for res in data.parse_lines::<MoveCmd>() {
|
|
||||||
commands.push(res?);
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("One: {}", part1(&commands));
|
|
||||||
println!("Two: {}", part2(&commands));
|
|
||||||
Ok(())
|
|
||||||
}
|
|
@ -36,7 +36,7 @@ pub trait ParseLines {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl ParseLines for str {
|
impl ParseLines for String {
|
||||||
fn parse_lines<T: FromStr>(&self) -> LineParser<'_, T> {
|
fn parse_lines<T: FromStr>(&self) -> LineParser<'_, T> {
|
||||||
LineParser {
|
LineParser {
|
||||||
it: self.lines(),
|
it: self.lines(),
|
||||||
@ -45,10 +45,3 @@ impl ParseLines for str {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
impl ParseLines for String {
|
|
||||||
fn parse_lines<T: FromStr>(&self) -> LineParser<'_, T> {
|
|
||||||
self[..].parse_lines::<T>()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -1,14 +1,45 @@
|
|||||||
use color_eyre::eyre;
|
use color_eyre::eyre;
|
||||||
|
|
||||||
mod lib;
|
mod lib;
|
||||||
use lib::load;
|
use lib::{load, ParseLines};
|
||||||
|
|
||||||
mod day2;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn main() -> eyre::Result<()> {
|
fn main() -> eyre::Result<()> {
|
||||||
let data = load("data/02.txt")?;
|
day_one()
|
||||||
day2::run(&data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fn day_one() -> eyre::Result<()> {
|
||||||
|
let data = load("data/01.txt")?;
|
||||||
|
|
||||||
|
let mut single_increases = 0;
|
||||||
|
let mut window_increases = 0;
|
||||||
|
let mut prev = (None, None, None);
|
||||||
|
for res in data.parse_lines::<i32>() {
|
||||||
|
let n = res?;
|
||||||
|
if let Some(p) = prev.2 {
|
||||||
|
// apparently we can't combine "if let" and regular boolean conditions
|
||||||
|
if n > p {
|
||||||
|
single_increases += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let (Some(first), Some(second), Some(third)) = prev {
|
||||||
|
if (n + second + third) > (first + second + third) {
|
||||||
|
window_increases += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// would have forgotten this if not for the warning triggered by "let mut" without mutation
|
||||||
|
prev = (prev.1, prev.2, Some(n));
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("One: {}", single_increases);
|
||||||
|
println!("Two: {}", window_increases);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// fn day_two() -> eyre::Result<()> {
|
||||||
|
|
||||||
|
// }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user