|
@@ -1,9 +1,10 @@
|
|
use std::collections::HashMap;
|
|
use std::collections::HashMap;
|
|
|
|
|
|
fn spawn_points(pattern: &str) -> Vec<(i32, i32)> {
|
|
fn spawn_points(pattern: &str) -> Vec<(i32, i32)> {
|
|
-
|
|
|
|
|
|
+ // The points to return, for example R3 will return (0,1),(0,1),(0,1) (three steps to the right)
|
|
let mut pts = vec![];
|
|
let mut pts = vec![];
|
|
|
|
|
|
|
|
+ // the digits in pattern (3 in 'R3')
|
|
let digits = pattern
|
|
let digits = pattern
|
|
.chars()
|
|
.chars()
|
|
.filter(|x| x.is_numeric())
|
|
.filter(|x| x.is_numeric())
|
|
@@ -11,36 +12,39 @@ fn spawn_points(pattern: &str) -> Vec<(i32, i32)> {
|
|
.parse()
|
|
.parse()
|
|
.unwrap();
|
|
.unwrap();
|
|
|
|
|
|
|
|
+ // The direction of a pattern ('R' in 'R3')
|
|
let direction: String = pattern.chars().filter(|x| !x.is_numeric()).collect();
|
|
let direction: String = pattern.chars().filter(|x| !x.is_numeric()).collect();
|
|
|
|
|
|
match direction.as_ref() {
|
|
match direction.as_ref() {
|
|
"R" => {
|
|
"R" => {
|
|
- for d in 0..digits {
|
|
|
|
|
|
+ for _d in 0..digits {
|
|
pts.push((0,1));
|
|
pts.push((0,1));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"L" => {
|
|
"L" => {
|
|
- for d in 0..digits {
|
|
|
|
|
|
+ for _d in 0..digits {
|
|
pts.push((0,-1));
|
|
pts.push((0,-1));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"U" => {
|
|
"U" => {
|
|
- for d in 0..digits {
|
|
|
|
|
|
+ for _d in 0..digits {
|
|
pts.push((1,0));
|
|
pts.push((1,0));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
"D" => {
|
|
"D" => {
|
|
- for d in 0..digits {
|
|
|
|
|
|
+ for _d in 0..digits {
|
|
pts.push((-1,0));
|
|
pts.push((-1,0));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
_ => {}
|
|
_ => {}
|
|
-
|
|
|
|
}
|
|
}
|
|
pts
|
|
pts
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+fn manhattan_to_origin(pt: (i32,i32)) -> i32 {
|
|
|
|
+ pt.0.abs()+pt.1.abs()
|
|
|
|
+}
|
|
|
|
|
|
fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32), i32>) -> Option<i32>{
|
|
fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32), i32>) -> Option<i32>{
|
|
let mut cursor = (0,0);
|
|
let mut cursor = (0,0);
|
|
@@ -48,10 +52,9 @@ fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32),
|
|
for instruction in instructions {
|
|
for instruction in instructions {
|
|
|
|
|
|
for pt in spawn_points(&instruction) {
|
|
for pt in spawn_points(&instruction) {
|
|
- // println!("Marching to {:?}", cursor);
|
|
|
|
if let Some(occupied_pt) = grid.get(&cursor) {
|
|
if let Some(occupied_pt) = grid.get(&cursor) {
|
|
println!("Point present @{:?}", cursor);
|
|
println!("Point present @{:?}", cursor);
|
|
- let dist = cursor.0.abs()+cursor.1.abs();
|
|
|
|
|
|
+ let dist = manhattan_to_origin(cursor);
|
|
if dist < closest && dist > 0 {
|
|
if dist < closest && dist > 0 {
|
|
closest = dist;
|
|
closest = dist;
|
|
}
|
|
}
|
|
@@ -63,8 +66,6 @@ fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32),
|
|
cursor.1 += pt.1;
|
|
cursor.1 += pt.1;
|
|
|
|
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
|
|
match closest {
|
|
match closest {
|
|
@@ -74,7 +75,6 @@ fn run_instruction_set(instructions: Vec<String>, grid: &mut HashMap<(i32,i32),
|
|
}
|
|
}
|
|
|
|
|
|
fn solve() {
|
|
fn solve() {
|
|
- // R75,D30,R83,U83,L12,D49,R71,U7,L72
|
|
|
|
|
|
|
|
// Hashmap that has a 2d point as key and a vec for wire indices
|
|
// Hashmap that has a 2d point as key and a vec for wire indices
|
|
let mut grid: HashMap<(i32,i32), i32> = HashMap::new();
|
|
let mut grid: HashMap<(i32,i32), i32> = HashMap::new();
|
|
@@ -88,14 +88,10 @@ fn solve() {
|
|
run_instruction_set(w1, &mut grid);
|
|
run_instruction_set(w1, &mut grid);
|
|
dbg!(run_instruction_set(w2, &mut grid));
|
|
dbg!(run_instruction_set(w2, &mut grid));
|
|
|
|
|
|
- // dbg!(grid);
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-
|
|
|
|
pub fn main() {
|
|
pub fn main() {
|
|
solve();
|
|
solve();
|
|
}
|
|
}
|