159 lines
5.5 KiB
Rust
Executable File
159 lines
5.5 KiB
Rust
Executable File
use std::fs::File;
|
|
use std::io::{self, prelude::*, BufReader};
|
|
use std::collections::HashMap;
|
|
use regex::Regex;
|
|
|
|
fn find_S(line: &str) -> Vec<(i32, i32)> {
|
|
let re = Regex::new(r"\d+").unwrap();
|
|
let matches: Vec<_> = re.find_iter(line).map(|m| m.range()).collect();
|
|
let mut return_vec: Vec<(i32, i32)> = vec![];
|
|
for i in matches {
|
|
return_vec.push((i.start as i32, i.end as i32));
|
|
// println!("first {}, Last {}", i.start, i.end);
|
|
// println!("number {}", line.get(i.start..i.end).unwrap().parse::<u32>().unwrap());
|
|
}
|
|
return return_vec;
|
|
}
|
|
|
|
fn find_next_location(wall_of_text: &Vec<String>, current_location: (i32, i32), previous_location: (i32, i32)) -> (i32, i32)
|
|
{
|
|
let current_char = wall_of_text[current_location.0 as usize].get(current_location.1 as usize..current_location.1 as usize+1).unwrap();
|
|
println!("current_char {}", current_char);
|
|
match current_char{
|
|
"|" => (current_location.0 + (current_location.0 - previous_location.0) , current_location.1),
|
|
"-" => (current_location.0, current_location.1 + (current_location.1 - previous_location.1)),
|
|
"L" => {
|
|
if current_location.0 == previous_location.0 {
|
|
(current_location.0-1 , current_location.1)
|
|
}
|
|
else
|
|
{
|
|
(current_location.0 , current_location.1+1)
|
|
}
|
|
},
|
|
"J" => {
|
|
if current_location.0 == previous_location.0 {
|
|
(current_location.0-1 , current_location.1)
|
|
}
|
|
else
|
|
{
|
|
(current_location.0 , current_location.1-1)
|
|
}
|
|
}
|
|
"7" => {
|
|
if current_location.0 == previous_location.0 {
|
|
(current_location.0+1 , current_location.1)
|
|
}
|
|
else
|
|
{
|
|
(current_location.0 , current_location.1-1)
|
|
}
|
|
}
|
|
"F" => {
|
|
if current_location.0 == previous_location.0 {
|
|
(current_location.0+1 , current_location.1)
|
|
}
|
|
else
|
|
{
|
|
(current_location.0 , current_location.1+1)
|
|
}
|
|
}
|
|
"." => (current_location.0 , current_location.1),
|
|
&_ => (current_location.0 , current_location.1),
|
|
}
|
|
|
|
}
|
|
|
|
fn process_text(wall_of_text: Vec<String>) -> u32 {
|
|
let mut starting_location = (0,0);
|
|
for (line_count, line) in wall_of_text.clone().into_iter().enumerate() {
|
|
let x = line.find("S");
|
|
|
|
if x.is_some(){
|
|
println!("S is located at {} {}", line_count, x.unwrap());
|
|
starting_location = (line_count as i32, x.unwrap() as i32);
|
|
let _ignore = find_next_location(&wall_of_text, starting_location, starting_location);
|
|
break;
|
|
}
|
|
}
|
|
let mut cardinal_directions: Vec<_> = vec![];
|
|
let mut heads: Vec<(i32, i32)> = vec![];
|
|
let mut prev_locs: Vec<(i32, i32)> = vec![];
|
|
prev_locs.push(starting_location);
|
|
prev_locs.push(starting_location);
|
|
cardinal_directions.push(wall_of_text[(starting_location.0 - 1) as usize].get(starting_location.1 as usize..starting_location.1 as usize+1).unwrap());
|
|
cardinal_directions.push(wall_of_text[(starting_location.0 + 1) as usize].get(starting_location.1 as usize..starting_location.1 as usize+1).unwrap());
|
|
cardinal_directions.push(wall_of_text[starting_location.0 as usize].get((starting_location.1 + 1) as usize..(starting_location.1 + 1) as usize+1).unwrap());
|
|
cardinal_directions.push(wall_of_text[starting_location.0 as usize].get((starting_location.1 - 1) as usize..(starting_location.1 - 1) as usize+1).unwrap());
|
|
|
|
if match cardinal_directions[0]{
|
|
"|" => true,
|
|
"-" => false,
|
|
"L" => false,
|
|
"J" => false,
|
|
"7" => true,
|
|
"F" => true,
|
|
"." => false,
|
|
&_ => false,
|
|
} {heads.push(((starting_location.0 - 1), (starting_location.1)))};
|
|
|
|
if match cardinal_directions[1]{
|
|
"|" => true,
|
|
"-" => false,
|
|
"L" => true,
|
|
"J" => true,
|
|
"7" => false,
|
|
"F" => false,
|
|
"." => false,
|
|
&_ => false,
|
|
} {heads.push(((starting_location.0 + 1), (starting_location.1)))};
|
|
|
|
if match cardinal_directions[2]{
|
|
"|" => false,
|
|
"-" => true,
|
|
"L" => false,
|
|
"J" => true,
|
|
"7" => true,
|
|
"F" => false,
|
|
"." => false,
|
|
&_ => false,
|
|
} {heads.push(((starting_location.0), (starting_location.1+1)))};
|
|
|
|
if match cardinal_directions[3]{
|
|
"|" => false,
|
|
"-" => true,
|
|
"L" => true,
|
|
"J" => false,
|
|
"7" => false,
|
|
"F" => true,
|
|
"." => false,
|
|
&_ => false,
|
|
} {heads.push(((starting_location.0), (starting_location.1-1)))};
|
|
|
|
let mut count = 1;
|
|
while heads[0] != heads[1]
|
|
{
|
|
let temp0 = heads[0];
|
|
let temp1 = heads[1];
|
|
heads[0] = find_next_location(&wall_of_text, heads[0], prev_locs[0]);
|
|
heads[1] = find_next_location(&wall_of_text, heads[1], prev_locs[1]);
|
|
prev_locs[0] = temp0;
|
|
prev_locs[1] = temp1;
|
|
count += 1;
|
|
}
|
|
|
|
return count;
|
|
}
|
|
|
|
fn main() -> io::Result<()> {
|
|
// let file = File::open("../test.txt")?;
|
|
let file = File::open("../input.txt")?;
|
|
let reader = BufReader::new(file);
|
|
let answer: u32 = process_text(
|
|
reader.lines().collect::<io::Result<Vec<String>>>().expect("failed")
|
|
);
|
|
println!("{}", answer);
|
|
|
|
Ok(())
|
|
}
|