day10 part2

This commit is contained in:
Andrew Glaze 2023-12-13 12:18:18 -05:00
parent 4984ad4801
commit f9095fdf8f

View File

@ -20,29 +20,33 @@ fn main() {
}
let mut finished = false;
let mut count = 1;
let mut first_pre = start_dirs[0].reverse();
let mut second_pre = start_dirs[1].reverse();
let mut first_pos = ((start.0 as i32 + start_dirs[0].to_ind().1) as usize, (start.1 as i32 + start_dirs[0].to_ind().0) as usize);
let mut second_pos = ((start.0 as i32 + start_dirs[1].to_ind().1) as usize, (start.1 as i32 + start_dirs[1].to_ind().0) as usize);
let mut pre = start_dirs[0].reverse();
let mut pos = ((start.0 as i32 + start_dirs[0].to_ind().1) as usize, (start.1 as i32 + start_dirs[0].to_ind().0) as usize);
let mut the_loop = vec![start, pos];
let mut area = 0;
while !finished {
let first_next = &input[first_pos.0][first_pos.1].iter().filter(|x| x != &&first_pre).next().unwrap();
first_pos = ((first_pos.0 as i32 + first_next.to_ind().1) as usize, (first_pos.1 as i32 + first_next.to_ind().0) as usize);
first_pre = first_next.reverse();
let second_next = &input[second_pos.0][second_pos.1].iter().filter(|x| x != &&second_pre).next().unwrap();
second_pos = ((second_pos.0 as i32 + second_next.to_ind().1) as usize, (second_pos.1 as i32 + second_next.to_ind().0) as usize);
second_pre = second_next.reverse();
count += 1;
finished = first_pos == second_pos;
let first_next = &input[pos.0][pos.1].iter().filter(|x| x != &&pre).next().unwrap();
pos = ((pos.0 as i32 + first_next.to_ind().1) as usize, (pos.1 as i32 + first_next.to_ind().0) as usize);
pre = first_next.reverse();
finished = pos == start;
the_loop.push(pos);
}
println!("{:?}", count)
for win in the_loop.windows(2) {
area += (win[0].1 * win[1].0) as i64;
area -= (win[0].0 * win[1].1) as i64;
}
let area = i64::abs(area) / 2;
let spaces = area - (the_loop.len() as i64 / 2) + 1;
println!("{}", spaces)
}
fn parse_input(input: &String) -> Vec<Vec<Vec<Direction>>> {
@ -111,4 +115,120 @@ impl Direction {
Direction::Start => panic!("Start should never be reversed. AHH"),
}
}
}
}
// use std::fs;
// use strum::IntoEnumIterator;
// use strum_macros::EnumIter;
// fn main() {
// let input = fs::read_to_string("input.txt").unwrap();
// let input = parse_input(&input);
// let start = find_start(&input);
// let mut start_dirs: Vec<Direction> = vec![];
// for dir in Direction::iter().filter(|x| x != &Direction::Start) {
// let (x, y) = ((start.0 as i32 + dir.to_ind().1), (start.1 as i32 + dir.to_ind().0));
// if x < 0 || y < 0 {
// continue;
// }
// let neibhor = &input[x as usize][y as usize];
// if neibhor.contains(&dir.reverse()) {
// start_dirs.push(dir);
// }
// }
// let mut finished = false;
// let mut count = 1;
// let mut first_pre = start_dirs[0].reverse();
// let mut second_pre = start_dirs[1].reverse();
// let mut first_pos = ((start.0 as i32 + start_dirs[0].to_ind().1) as usize, (start.1 as i32 + start_dirs[0].to_ind().0) as usize);
// let mut second_pos = ((start.0 as i32 + start_dirs[1].to_ind().1) as usize, (start.1 as i32 + start_dirs[1].to_ind().0) as usize);
// while !finished {
// let first_next = &input[first_pos.0][first_pos.1].iter().filter(|x| x != &&first_pre).next().unwrap();
// first_pos = ((first_pos.0 as i32 + first_next.to_ind().1) as usize, (first_pos.1 as i32 + first_next.to_ind().0) as usize);
// first_pre = first_next.reverse();
// let second_next = &input[second_pos.0][second_pos.1].iter().filter(|x| x != &&second_pre).next().unwrap();
// second_pos = ((second_pos.0 as i32 + second_next.to_ind().1) as usize, (second_pos.1 as i32 + second_next.to_ind().0) as usize);
// second_pre = second_next.reverse();
// count += 1;
// finished = first_pos == second_pos;
// }
// println!("{:?}", count)
// }
// fn parse_input(input: &String) -> Vec<Vec<Vec<Direction>>> {
// let input: Vec<_> = input.split('\n')
// .map(|line| {
// line.chars()
// .map(|char| {
// match char {
// '|' => vec![Direction::North, Direction::South],
// '-' => vec![Direction::East, Direction::West],
// 'L' => vec![Direction::North, Direction::East],
// 'J' => vec![Direction::North, Direction::West],
// '7' => vec![Direction::South, Direction::West],
// 'F' => vec![Direction::South, Direction::East],
// '.' => vec![],
// 'S' => vec![Direction::Start],
// _ => panic!("Invalid pipe char")
// }
// }).collect::<Vec<_>>()
// }).collect();
// return input;
// }
// fn find_start(input: &Vec<Vec<Vec<Direction>>>) -> (usize, usize) {
// let mut start_point: Option<(usize, usize)> = None;
// for i in 0..input.len() {
// for j in 0..input[0].len() {
// if input[i][j].contains(&Direction::Start) {
// start_point = Some((i,j));
// }
// }
// }
// match start_point {
// Some(x) => x,
// None => panic!("No start point found! AHHHHH")
// }
// }
// #[derive(Debug, PartialEq, EnumIter)]
// enum Direction {
// North,
// South,
// East,
// West,
// Start
// }
// impl Direction {
// pub fn to_ind(&self) -> (i32, i32) {
// match self {
// Direction::North => (0,-1),
// Direction::South => (0,1),
// Direction::East => (1,0),
// Direction::West => (-1,0),
// Direction::Start => panic!("Start should never be converted to an index. AHH"),
// }
// }
// pub fn reverse(&self) -> Direction {
// match self {
// Direction::North => Direction::South,
// Direction::South => Direction::North,
// Direction::East => Direction::West,
// Direction::West => Direction::East,
// Direction::Start => panic!("Start should never be reversed. AHH"),
// }
// }
// }