Skip to content

Commit 41ec730

Browse files
committed
chore: more scaffolding
1 parent 13d4ff7 commit 41ec730

File tree

7 files changed

+145
-5
lines changed

7 files changed

+145
-5
lines changed

day_12/src/direction.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use crate::action::Action;
2+
3+
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
4+
pub enum Direction {
5+
Up,
6+
Down,
7+
Left,
8+
Right,
9+
}
10+
11+
use Direction::*;
12+
13+
impl Direction {
14+
pub fn turn(&mut self, action: &Action) {
15+
*self = match action {
16+
&Action::Left => match self {
17+
Up => Left,
18+
Left => Down,
19+
Down => Right,
20+
Right => Up,
21+
},
22+
&Action::Right | _ => match self {
23+
Up => Right,
24+
Right => Down,
25+
Down => Left,
26+
Left => Up,
27+
},
28+
};
29+
}
30+
}
31+
32+
impl Default for Direction {
33+
fn default() -> Self {
34+
Direction::Right
35+
}
36+
}

day_12/src/facing.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::action::Action::{self, *};
2+
3+
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
4+
pub enum Facing {
5+
East,
6+
West,
7+
North,
8+
South,
9+
}
10+
11+
impl Facing {
12+
pub fn from(action: Action) -> Self {
13+
match action {
14+
North => Self::North,
15+
West => Self::West,
16+
South => Self::South,
17+
East | _ => Self::East,
18+
}
19+
}
20+
}

day_12/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
pub mod action;
2+
pub mod direction;
3+
pub mod facing;
4+
pub mod manhattan;
5+
pub mod point;
6+
pub mod ship;
7+
28
pub mod part_01;
39
pub mod part_02;
410

day_12/src/manhattan.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use crate::point::Point;
2+
3+
pub fn manhattan(point: Point, current: Point) -> u64 {
4+
((point.x as i64 - current.x as i64).abs() + (point.y as i64 - current.y as i64).abs()) as u64
5+
}

day_12/src/part_01.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::io::Result;
22

3-
use crate::Input;
3+
use crate::{manhattan::manhattan, point::Point, ship::Ship, Input};
44

5-
pub fn main(_input: &Input) -> Result<()> {
6-
Ok(())
5+
pub fn main(input: &Input) -> Result<u64> {
6+
let point = Ship::default().follow_instruction(&input);
7+
Ok(manhattan(point, Point { x: 0, y: 0 }))
78
}
89

910
#[cfg(test)]
@@ -13,12 +14,16 @@ mod tests {
1314
use super::*;
1415

1516
const EXAMPLE_DATA: &'static str = "
16-
example
17+
F10
18+
N3
19+
F7
20+
R90
21+
F11
1722
";
1823

1924
#[test]
2025
fn it_gets_the_example_correct() -> Result<()> {
21-
assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, ());
26+
assert_eq!(main(&parse_input(&EXAMPLE_DATA))?, 25);
2227
Ok(())
2328
}
2429
}

day_12/src/point.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Default)]
2+
pub struct Point {
3+
pub x: isize,
4+
pub y: isize,
5+
}
6+
7+
impl Point {
8+
pub fn new() -> Self {
9+
Self { ..Point::default() }
10+
}
11+
}

day_12/src/ship.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use crate::{
2+
action::Action::{self, *},
3+
direction::Direction,
4+
point::Point,
5+
Input,
6+
};
7+
8+
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug, Default)]
9+
pub struct Ship {
10+
facing: Direction,
11+
x: isize,
12+
y: isize,
13+
}
14+
15+
impl Ship {
16+
fn turn(&mut self, action: &Action) {
17+
self.facing.turn(action);
18+
}
19+
20+
fn forward(&mut self, value: &u16) {
21+
let value = value.to_owned() as isize;
22+
match self.facing {
23+
Direction::Up => self.y += value,
24+
Direction::Left => self.x -= value,
25+
Direction::Down => self.y -= value,
26+
Direction::Right => self.x += value,
27+
}
28+
}
29+
30+
fn directional_move(&mut self, action: &Action, value: &u16) {
31+
let value = value.to_owned() as isize;
32+
match action {
33+
North => self.y += value,
34+
East => self.x += value,
35+
South => self.y -= value,
36+
West | _ => self.x -= value,
37+
}
38+
}
39+
40+
pub fn follow_instruction(mut self, input: &Input) -> Point {
41+
for (action, value) in input {
42+
println!("action={:?}\taction={}", action, value);
43+
match action {
44+
North | South | East | West => self.directional_move(action, value),
45+
Left | Right => self.turn(action),
46+
Forward => self.forward(value),
47+
}
48+
}
49+
50+
println!("x={}\ty={}", self.x, self.y);
51+
52+
Point {
53+
x: self.x,
54+
y: self.y,
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy