5 changed files with 209 additions and 42 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,106 @@ |
|||
pub struct Data { |
|||
inst: Vec<Dir>, |
|||
} |
|||
|
|||
impl Data { |
|||
pub fn new() -> Data { |
|||
Data { |
|||
inst: Vec::new(), |
|||
} |
|||
} |
|||
|
|||
pub fn parse(&mut self, input: String) { |
|||
for c in input.chars() { |
|||
match c { |
|||
'^' => { |
|||
self.inst.push(Dir::North); |
|||
}, |
|||
'>' => { |
|||
self.inst.push(Dir::East); |
|||
}, |
|||
'v' => { |
|||
self.inst.push(Dir::South); |
|||
}, |
|||
'<' => { |
|||
self.inst.push(Dir::West); |
|||
}, |
|||
_ => (), |
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
pub fn visit(&mut self) -> i32 { |
|||
let mut pos = Point{x: 0, y: 0}; |
|||
let mut way = Vec::new(); |
|||
way.push(pos); |
|||
for d in self.inst.clone() { |
|||
pos.walk(d, 1); |
|||
way.push(pos); |
|||
} |
|||
way.sort_by(|a, b| a.abs().cmp(&b.abs())); |
|||
way.dedup_by(|a, b| a.x == b.x && a.y == b.y); |
|||
way.len() as i32 |
|||
} |
|||
|
|||
pub fn robot(&mut self) -> i32 { |
|||
let mut pos = Point{x: 0, y: 0}; |
|||
let mut way = Vec::new(); |
|||
let mut pos_robot = Point{x: 0, y: 0}; |
|||
way.push(pos); |
|||
way.push(pos_robot); |
|||
for (i, d) in self.inst.clone().into_iter().enumerate() { |
|||
match i % 2 { |
|||
0 => { |
|||
pos.walk(d, 1); |
|||
way.push(pos); |
|||
}, |
|||
_ => { |
|||
pos_robot.walk(d, 1); |
|||
way.push(pos_robot); |
|||
}, |
|||
} |
|||
} |
|||
way.sort_by(|a, b| a.abs().cmp(&b.abs())); |
|||
way.dedup_by(|a, b| a.x == b.x && a.y == b.y); |
|||
way.len() as i32 |
|||
} |
|||
} |
|||
|
|||
#[derive(Copy, Clone)] |
|||
struct Point { |
|||
x: i32, |
|||
y: i32, |
|||
} |
|||
|
|||
impl Point { |
|||
pub fn walk(&mut self, dir: Dir, dist: i32) { |
|||
match dir { |
|||
Dir::North => self.y += dist, |
|||
Dir::East => self.x += dist, |
|||
Dir::South => self.y -= dist, |
|||
Dir::West => self.x -= dist, |
|||
} |
|||
} |
|||
pub fn abs(&self) -> i64 { |
|||
((self.x as i64) << 32) + self.y as i64 |
|||
} |
|||
} |
|||
|
|||
impl PartialEq for Point { |
|||
fn eq(&self, other: &Point) -> bool { |
|||
let mut res = self.x == other.x; |
|||
res &= self.y == other.y; |
|||
res |
|||
} |
|||
} |
|||
|
|||
impl Eq for Point {} |
|||
|
|||
#[derive(Copy, Clone)] |
|||
enum Dir { |
|||
North, |
|||
East, |
|||
South, |
|||
West, |
|||
} |
|||
Loading…
Reference in new issue