summaryrefslogtreecommitdiff
path: root/aoc1
diff options
context:
space:
mode:
authorJustin Wernick <justin@worthe-it.co.za>2022-04-19 20:22:56 +0200
committerJustin Wernick <justin@worthe-it.co.za>2022-04-19 20:23:15 +0200
commit174772b5b8d9f5bf5e3c8e8152adfd89f0e83f6b (patch)
treea003b748ee939b30a2bcd3caf2378228baa304c1 /aoc1
parentfd75b3fb95ad049b0025cb8fc0b3459b8f872d61 (diff)
Refile for merging repos
Diffstat (limited to 'aoc1')
-rw-r--r--aoc1/Cargo.lock4
-rw-r--r--aoc1/Cargo.toml6
-rw-r--r--aoc1/input.txt1
-rw-r--r--aoc1/src/main.rs98
4 files changed, 0 insertions, 109 deletions
diff --git a/aoc1/Cargo.lock b/aoc1/Cargo.lock
deleted file mode 100644
index b771521..0000000
--- a/aoc1/Cargo.lock
+++ /dev/null
@@ -1,4 +0,0 @@
-[root]
-name = "aoc1"
-version = "0.1.0"
-
diff --git a/aoc1/Cargo.toml b/aoc1/Cargo.toml
deleted file mode 100644
index 096aad6..0000000
--- a/aoc1/Cargo.toml
+++ /dev/null
@@ -1,6 +0,0 @@
-[package]
-name = "aoc1"
-version = "0.1.0"
-authors = ["Justin Worthe <justin.worthe@entelect.co.za>"]
-
-[dependencies]
diff --git a/aoc1/input.txt b/aoc1/input.txt
deleted file mode 100644
index 456904d..0000000
--- a/aoc1/input.txt
+++ /dev/null
@@ -1 +0,0 @@
-R4, R5, L5, L5, L3, R2, R1, R1, L5, R5, R2, L1, L3, L4, R3, L1, L1, R2, R3, R3, R1, L3, L5, R3, R1, L1, R1, R2, L1, L4, L5, R4, R2, L192, R5, L2, R53, R1, L5, R73, R5, L5, R186, L3, L2, R1, R3, L3, L3, R1, L4, L2, R3, L5, R4, R3, R1, L1, R5, R2, R1, R1, R1, R3, R2, L1, R5, R1, L5, R2, L2, L4, R3, L1, R4, L5, R4, R3, L5, L3, R4, R2, L5, L5, R2, R3, R5, R4, R2, R1, L1, L5, L2, L3, L4, L5, L4, L5, L1, R3, R4, R5, R3, L5, L4, L3, L1, L4, R2, R5, R5, R4, L2, L4, R3, R1, L2, R5, L5, R1, R1, L1, L5, L5, L2, L1, R5, R2, L4, L1, R4, R3, L3, R1, R5, L1, L4, R2, L3, R5, R3, R1, L3
diff --git a/aoc1/src/main.rs b/aoc1/src/main.rs
deleted file mode 100644
index e9b4138..0000000
--- a/aoc1/src/main.rs
+++ /dev/null
@@ -1,98 +0,0 @@
-use std::io::Read;
-use std::fs::File;
-
-#[derive(Clone, Copy, Debug)]
-enum Direction {
- Up,
- Left,
- Down,
- Right
-}
-
-impl Direction {
- fn turn_right(self) -> Direction {
- match self {
- Direction::Up => Direction::Right,
- Direction::Right => Direction::Down,
- Direction::Down => Direction::Left,
- Direction::Left => Direction::Up
- }
- }
- fn turn_left(self) -> Direction {
- //not an ambiturner
- self.turn_right().turn_right().turn_right()
- }
-
- fn as_vector(self, dist: i32) -> (i32,i32) {
- match self {
- Direction::Up => (0, -dist),
- Direction::Right => (dist, 0),
- Direction::Down => (0, dist),
- Direction::Left => (-dist, 0)
- }
- }
-}
-
-fn main() {
- let content = read_file().expect("Failed to read file");
- let (_, dist_x, dist_y) = first_repeated_dest(content);
- println!("Total: ({}, {})", dist_x, dist_y);
- println!("Net: {}", dist_x+dist_y);
-}
-
-fn net_distance(content: String) -> (Direction, i32, i32) {
- content.trim().split(", ")
- .map(|action|
- (
- action.chars().nth(0).unwrap(),
- action.chars().skip(1).collect::<String>().parse::<i32>().unwrap()
- ))
- .fold((Direction::Up, 0,0), |(facing, acc_x, acc_y), (dir, dist)| {
- let new_facing = match dir {
- 'R' => facing.turn_right(),
- 'L' => facing.turn_left(),
- _ => panic!("bad input")
- };
-
- let (new_x, new_y) = new_facing.as_vector(dist);
- (new_facing, acc_x+new_x, acc_y+new_y)
- })
-}
-
-fn first_repeated_dest(content: String) -> (Direction, i32, i32) {
- let mut stops: Vec<(i32, i32)> = Vec::new();
- let actions = content.trim().split(", ")
- .map(|action|
- (
- action.chars().nth(0).unwrap(),
- action.chars().skip(1).collect::<String>().parse::<i32>().unwrap()
- ));
- let mut current_facing = Direction::Up;
- let (mut acc_x, mut acc_y) = (0, 0);
- for (dir, dist) in actions {
- current_facing = match dir {
- 'R' => current_facing.turn_right(),
- 'L' => current_facing.turn_left(),
- _ => panic!("bad input")
- };
-
- for _ in 0..dist {
- let (new_x, new_y) = current_facing.as_vector(1);
- acc_x += new_x;
- acc_y += new_y;
-
- if stops.iter().any(|&(x, y)| x==acc_x && y==acc_y) {
- return (current_facing, acc_x, acc_y);
- }
- stops.push((acc_x, acc_y));
- }
- }
- (current_facing, acc_x, acc_y)
-}
-
-fn read_file() -> Result<String, String> {
- let mut file = try!(File::open("input.txt").map_err(|e| e.to_string()));
- let mut content = String::new();
- try!(file.read_to_string(&mut content).map_err(|e| e.to_string()));
- Ok(content)
-}