--- /dev/null
+#[derive(Copy, Clone, Debug, PartialEq)]
+pub enum Movement {
+ Forward(u32),
+ Down(u32),
+ Up(u32),
+}
+
+pub struct Position {
+ pub horizontal: i32,
+ pub depth: i32,
+}
+
+pub fn parse_movements(movements: &str) -> Vec<Movement> {
+ movements
+ .split('\n')
+ .map(|line| {
+ let trimmed_lowercase_line = line.trim().to_lowercase();
+ let command_and_distance: Vec<&str> = trimmed_lowercase_line.split(' ').collect();
+ let distance = command_and_distance[1].parse::<u32>().unwrap();
+ match command_and_distance[0] {
+ "forward" => Movement::Forward(distance),
+ "down" => Movement::Down(distance),
+ "up" => Movement::Up(distance),
+ _ => Movement::Forward(0)
+ }
+ })
+ .collect()
+}
+
+pub fn get_final_position(movements: &[Movement]) -> Position {
+ let mut pos = Position { horizontal: 0, depth: 0 };
+ for m in movements {
+ match m {
+ Movement::Forward(d) => pos.horizontal += *d as i32,
+ Movement::Down(d) => pos.depth += *d as i32,
+ Movement::Up(d) => pos.depth -= *d as i32,
+ }
+ }
+ pos
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn parsing() {
+ let commands_str =
+ "forward 5
+ down 5
+ forward 8
+ up 3
+ down 8
+ forward 2";
+
+ let commands = parse_movements(commands_str);
+ assert_eq!(commands, Vec::from([Movement::Forward(5), Movement::Down(5), Movement::Forward(8), Movement::Up(3), Movement::Down(8), Movement::Forward(2)]));
+ }
+
+ #[test]
+ fn part1() {
+ let commands =
+ parse_movements(
+ "forward 5
+ down 5
+ forward 8
+ up 3
+ down 8
+ forward 2"
+ );
+ let final_position = get_final_position(&commands);
+ assert_eq!(final_position.horizontal * final_position.depth, 150);
+ }
+
+ #[test]
+ fn part2() {
+ }
+}
\ No newline at end of file
use std::env;
+use std::fs;
use std::time::Instant;
mod common;
mod day01;
+mod day02;
fn day01() -> String {
let report = common::read_list_of_numbers("data/day01.input", "\n");
format!("part1: {}, part2: {}", day01::count_number_of_decreased_values(&report, 1), day01::count_number_of_decreased_values(&report, 3))
}
+fn day02() -> String {
+ let movements = day02::parse_movements(&fs::read_to_string("data/day02.input").unwrap());
+ let final_position = day02::get_final_position(&movements);
+ format!("part1: {}, part2: {}", final_position.horizontal * final_position.depth, "")
+}
+
fn format_micros(t: u128) -> String {
if t < 10_000 {
format!("{} μs", t)
let days: Vec<fn() -> String> = vec!(
day01,
+ day02,
);
let args: Vec<String> = env::args().skip(1).collect();