Skip to content

Commit

Permalink
Add 2016 day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
WillFlame14 committed Apr 13, 2024
1 parent 9732c7a commit ef6cf98
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules/
*.hi
*.o
*.exe
target/
input.txt
7 changes: 7 additions & 0 deletions 2016/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions 2016/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "aoc2016"
version = "0.1.0"
edition = "2021"

[dependencies]
89 changes: 89 additions & 0 deletions 2016/src/bin/day1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::collections::HashSet;

#[derive(PartialEq, Clone, Copy)]
enum Direction {
North,
East,
South,
West
}

fn turn(dir: &str, curr: &Direction) -> Direction {
let dirs = [Direction::North, Direction::East, Direction::South, Direction::West];
let index = dirs.iter().position(|s| s == curr).unwrap();

match dir {
"R" => { dirs[(index + 1) % 4] }
"L" => { dirs[(4 + index - 1) % 4] },
_ => panic!("Tried to turn unknown direction {}", dir)
}
}

fn move_dist(loc: (i32, i32), dir: &Direction, dist: i32, visited: &mut HashSet<(i32, i32)>) -> ((i32, i32), Option<(i32, i32)>) {
let (x, y) = loc;

let (new_x, new_y) = match *dir {
Direction::North => { (x, y + dist) },
Direction::East => { (x + dist, y) },
Direction::South => { (x, y - dist) },
Direction::West => { (x - dist, y) }
};

let mut already_visited = None;

for i in 1..dist {
let curr = (x + ((new_x - x) / dist) * i, y + ((new_y - y) / dist) * i);

if visited.contains(&curr) && already_visited.is_none() {
already_visited = Some(curr);
}
else {
visited.insert(curr);
}
}

((new_x, new_y), already_visited)
}

fn manhattan_dist(loc: (i32, i32)) -> i32 {
return loc.0.abs() + loc.1.abs();
}

fn main() {
let path = Path::new("input.txt");
let display = path.display();

let mut file = match File::open(&path) {
Err(why) => panic!("couldn't open {}: {}", display, why),
Ok(file) => file,
};

let mut s = String::new();
file.read_to_string(&mut s).unwrap();

let parts = s.trim().split(", ");

let mut loc = (0, 0);
let mut dir = Direction::North;
let mut visited = HashSet::new();
let mut already_visited = None;

for p in parts {
let (first, rest) = p.split_at(1);
let distance = rest.parse::<i32>().unwrap();

dir = turn(first, &dir);
let result = move_dist(loc, &dir, distance, &mut visited);
loc = result.0;

if already_visited.is_none() && result.1.is_some() {
already_visited = result.1;
}
}

println!("part 1 {}", manhattan_dist(loc));
println!("part 2 {}", manhattan_dist(already_visited.unwrap()));
}

0 comments on commit ef6cf98

Please sign in to comment.