Last active
December 10, 2022 00:14
-
-
Save padajo/a3608ce3708de4e6d0e9b2b37c36da26 to your computer and use it in GitHub Desktop.
Advent of Code 2022 - Day 9 - Part 1 - Incomplete solution, and I do not know why it doesn't work - https://adventofcode.com/2022/day/9 - I found an incorrect < or > and it looks like it might work. I needed the visualisation of the entire solution to see it wasn't doing "Down"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
use std::fs::File; | |
use std::io::{self, BufRead}; | |
use std::path::Path; | |
use std::thread; | |
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> | |
where | |
P: AsRef<Path>, | |
{ | |
let file = File::open(filename)?; | |
Ok(io::BufReader::new(file).lines()) | |
} | |
#[derive(Debug)] | |
struct Point { | |
x: i32, | |
y: i32, | |
} | |
impl Point { | |
fn new(x: i32, y: i32) -> Point { | |
Point { x: x, y: y } | |
} | |
} | |
fn print_grid( | |
head: &Point, | |
tail: &Point, | |
min_point: Point, | |
max_point: Point, | |
tail_visited: &HashSet<String>, | |
final_grid: bool, | |
) -> () { | |
print!("\x1B[2J\x1B[1;1H"); | |
let max_i = max_point.x; | |
let max_j = max_point.y; | |
let min_i = min_point.x; | |
let min_j = min_point.y; | |
let hx = head.x.clone(); | |
let hy = head.y.clone(); | |
let tx = tail.x.clone(); | |
let ty = tail.y.clone(); | |
for j in min_j..max_j { | |
for i in min_i..max_i { | |
let x = i; // this is the x axis value | |
let y = max_j - j - 1 - min_j.abs(); // this is the y axis value | |
if hx == x && hy == y && !final_grid { | |
print!("H"); | |
} else if tx == x && ty == y && !final_grid { | |
print!("T"); | |
} else if tail_visited.contains(&format!("{},{}", x, y)) { | |
print!("#"); | |
} else { | |
print!(" "); | |
} | |
} | |
println!(""); | |
} | |
println!(""); | |
thread::sleep(std::time::Duration::from_millis(20)); | |
} | |
fn update_history_and_visited( | |
history: &mut Vec<Point>, | |
visited: &mut HashSet<String>, | |
p: &Point, | |
) -> () { | |
let x = p.x; | |
let y = p.y; | |
let new_point: Point = Point::new(x, y); | |
history.push(new_point); | |
visited.insert(format!("{},{}", x, y)); | |
} | |
fn main() { | |
println!("Advent of code 2022, Day 9 Part 1\n"); | |
let mut head: Point = Point::new(0, 0); | |
let mut tail: Point = Point::new(0, 0); | |
let mut head_history: Vec<Point> = Vec::new(); | |
let mut tail_history: Vec<Point> = Vec::new(); | |
let mut head_visited: HashSet<String> = HashSet::new(); | |
let mut tail_visited: HashSet<String> = HashSet::new(); | |
head_history.push(Point::new(head.x, head.y)); | |
tail_history.push(Point::new(tail.x, tail.y)); | |
head_visited.insert("0,0".to_string()); | |
tail_visited.insert("0,0".to_string()); | |
let mut counter: u32 = 0; // includes start point as a position visited | |
let mut tail_counter: u32 = 0; // includes start point as a position visited | |
// if let Ok(lines) = read_lines("./test-data.txt") { | |
if let Ok(lines) = read_lines("./input.txt") { | |
for line in lines { | |
let chars: Vec<char> = Ok::<String, String>(line.unwrap()) | |
.unwrap() | |
.chars() | |
.collect(); | |
let direction: char = chars[0].clone(); | |
let count: u32 = chars[2].to_digit(10).unwrap().clone() as u32; | |
for _i in 0..count { | |
match direction { | |
'U' => { | |
head.y += 1; | |
if head.y - tail.y > 1 { | |
tail.y += 1; | |
tail.x = head.x; // when moving diagonally with this move x always moves below the head | |
update_history_and_visited(&mut tail_history, &mut tail_visited, &tail); | |
tail_counter += 1; | |
} | |
} | |
'D' => { | |
head.y -= 1; | |
if head.y - tail.y < -1 { | |
tail.y -= 1; | |
tail.x = head.x; // when moving diagonally with this move x always moves above the head | |
update_history_and_visited(&mut tail_history, &mut tail_visited, &tail); | |
tail_counter += 1; | |
} | |
} | |
'R' => { | |
head.x += 1; | |
if head.x - tail.x > 1 { | |
tail.x += 1; | |
tail.y = head.y; // when moving diagonally with this move x always moves to left of the head | |
update_history_and_visited(&mut tail_history, &mut tail_visited, &tail); | |
tail_counter += 1; | |
} | |
} | |
'L' => { | |
head.x -= 1; | |
if head.x - tail.x < -1 { | |
tail.x -= 1; | |
tail.y = head.y; // when moving diagonally with this move x always moves to right of the head | |
update_history_and_visited(&mut tail_history, &mut tail_visited, &tail); | |
tail_counter += 1; | |
} | |
} | |
_ => println!("Unknown direction"), | |
} | |
update_history_and_visited(&mut head_history, &mut head_visited, &head); | |
counter += 1; | |
print_grid( | |
&head, | |
&tail, | |
Point::new(-80, 80), | |
Point::new(200, 200), | |
&tail_visited, | |
false, | |
); | |
} | |
} | |
} | |
print_grid( | |
&head, | |
&tail, | |
Point::new(-5, -5), | |
Point::new(8, 8), | |
&tail_visited, | |
true, | |
); | |
println!("head: {:?} (at end)", head); | |
println!("tail: {:?} (at end)", tail); | |
println!("head history count: {:?}", head_history.len()); | |
println!("tail history count: {:?}", tail_history.len()); | |
println!("head visited count: {:?}", head_visited.len()); | |
println!("tail visited count: {:?}", tail_visited.len()); | |
println!("counter: {:?}", counter); | |
println!("tail counter: {:?}", tail_counter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment