Started AOC
authorJustin Worthe <justin.worthe@entelect.co.za>
Fri, 2 Dec 2016 05:51:59 +0000 (07:51 +0200)
committerJustin Worthe <justin.worthe@entelect.co.za>
Fri, 2 Dec 2016 05:51:59 +0000 (07:51 +0200)
16 files changed:
aoc1/Cargo.lock [new file with mode: 0644]
aoc1/Cargo.toml [new file with mode: 0644]
aoc1/input.txt [new file with mode: 0644]
aoc1/src/main.rs [new file with mode: 0644]
aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1 [new file with mode: 0644]
aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1.json [new file with mode: 0644]
aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/dep-bin-aoc1 [new file with mode: 0644]
aoc1/target/debug/aoc1.exe [new file with mode: 0644]
aoc2/Cargo.lock [new file with mode: 0644]
aoc2/Cargo.toml [new file with mode: 0644]
aoc2/input.txt [new file with mode: 0644]
aoc2/src/main.rs [new file with mode: 0644]
aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/bin-aoc2 [new file with mode: 0644]
aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/bin-aoc2.json [new file with mode: 0644]
aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/dep-bin-aoc2 [new file with mode: 0644]
aoc2/target/debug/aoc2.exe [new file with mode: 0644]

diff --git a/aoc1/Cargo.lock b/aoc1/Cargo.lock
new file mode 100644 (file)
index 0000000..b771521
--- /dev/null
@@ -0,0 +1,4 @@
+[root]
+name = "aoc1"
+version = "0.1.0"
+
diff --git a/aoc1/Cargo.toml b/aoc1/Cargo.toml
new file mode 100644 (file)
index 0000000..096aad6
--- /dev/null
@@ -0,0 +1,6 @@
+[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
new file mode 100644 (file)
index 0000000..456904d
--- /dev/null
@@ -0,0 +1 @@
+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
new file mode 100644 (file)
index 0000000..e9b4138
--- /dev/null
@@ -0,0 +1,98 @@
+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)
+}
diff --git a/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1 b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1
new file mode 100644 (file)
index 0000000..1ef2d34
--- /dev/null
@@ -0,0 +1 @@
+aa49b87236c3d407
\ No newline at end of file
diff --git a/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1.json b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/bin-aoc1.json
new file mode 100644 (file)
index 0000000..d370c06
--- /dev/null
@@ -0,0 +1 @@
+{"rustc":3005860601515633040,"target":7416218395859320575,"profile":13023446655302672390,"local":{"variant":"MtimeBased","fields":[[1480577027,916536700],[67,58,92,80,114,111,106,92,67,111,109,112,101,116,105,116,105,111,110,115,92,97,100,118,101,110,116,111,102,99,111,100,101,50,48,49,54,92,97,111,99,49,92,116,97,114,103,101,116,92,100,101,98,117,103,92,46,102,105,110,103,101,114,112,114,105,110,116,92,97,111,99,49,45,53,55,57,54,53,56,55,100,101,55,53,51,48,102,57,57,92,100,101,112,45,98,105,110,45,97,111,99,49]]},"features":"None","deps":[]}
\ No newline at end of file
diff --git a/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/dep-bin-aoc1 b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/dep-bin-aoc1
new file mode 100644 (file)
index 0000000..f805f30
Binary files /dev/null and b/aoc1/target/debug/.fingerprint/aoc1-5796587de7530f99/dep-bin-aoc1 differ
diff --git a/aoc1/target/debug/aoc1.exe b/aoc1/target/debug/aoc1.exe
new file mode 100644 (file)
index 0000000..e594df4
Binary files /dev/null and b/aoc1/target/debug/aoc1.exe differ
diff --git a/aoc2/Cargo.lock b/aoc2/Cargo.lock
new file mode 100644 (file)
index 0000000..b0dbf6e
--- /dev/null
@@ -0,0 +1,4 @@
+[root]
+name = "aoc2"
+version = "0.1.0"
+
diff --git a/aoc2/Cargo.toml b/aoc2/Cargo.toml
new file mode 100644 (file)
index 0000000..e4dcc6f
--- /dev/null
@@ -0,0 +1,6 @@
+[package]
+name = "aoc2"
+version = "0.1.0"
+authors = ["Justin Worthe <justin.worthe@entelect.co.za>"]
+
+[dependencies]
diff --git a/aoc2/input.txt b/aoc2/input.txt
new file mode 100644 (file)
index 0000000..ba5a73d
--- /dev/null
@@ -0,0 +1,5 @@
+RUDULRLLUULRURDDRRUDURULLLDRLRLUDDLUDUDDUDRRDUDULDUUULLRULLRLDDLDLDDRLRRRRUDLLDDUULDRLLUDDRRUURLULRRRDLLURRUUDURUDDURLUDDDLUDDUUDUURUDLRDRDRLRDRLDRUDRUUDLRDDRRURDDLRDDRRURDUDDLULLUDRURURRRLRRUDUULULULRRLDLUDUURRLLRUDLLDRDDLRRRULRUDLULDDLLLULDLRUDLLLLRDDLRDRLDRLLRDRRDLRDULULRLLLDRUDRRRUULRUULDRURLUDRURRDLLDLRDLDDDDRRLUDLRRLUUUURDRDDLRRURURRDUULLRLURLURUDDDRDURDUUDRLRLRRLDDLDLDLDDDUDDULURLDDLLRLRRDULUDDLULRLUDDLDLRULUUUDRLDRUDURLUDDRLLRUULDLRRRRDLLLLURULLRDRRUDLUULRRDLLRLRLUDLDDULLDLLRDLDLL
+LLUUUUUUDUDRLRDRDLDURRRLLRRLRURLLUURRLLUDUDLULUURUUURDLUDLDDLULLRDLRUULDLRDUDURLLDDUDUDULLUDDUULLLUULRRRLULRURRDLRUDUDDURRRDRUURDURLLULLRULLDRUULLURLDRDUUDDDDDDRRLDRLRRRLULDDUURRLLLLDRURLURDRDRDURUDUURRDUDUDRLLUUDDRLUDDDRDLDLRLDRURRDLLRULDRLLURURRLUULLRLRRURDDRDRUUURUURUUUDLLRRLUDRLDLRLURLDLUDDUDDDLDUDRRLDLRURULRLLRDUULURRRULDLLLRLDDDUURRRRDULLRURRLULULDLRRUDUDDLRUURDLDUDDUDRRDLRRRDUDUUUDLLDDDDLURLURRRUUULLLULRRLLLLLLULDUUDLRUDRRDLRDUUDUDLLRLDLLRUURDUUURUUUDDLLUUDLULDURLULULUUUDRUDULLURRULRULLRDLDDU
+RLUUURULLDLRLDUDRDURRDUURLLUDDDUULRRRLRLURDDRUULUDULDUUDDDDUDDDDRUDDLDUUDRUDLRRRLLRDDLLLRLLRUULRUULDDRURRLURRLRLULDDRRRDDURDDRDRDULRUDRUUDULRLLULDLRLLDRULRDDRRDDUDLRLLUDRDRRRLUDULRDLRDDURRUUDDRRUDURRUUUDDRRDUDURLUUDUDUURDDDLURLULLUULULURUDUUDRUDULLUUULURDLDUULLDDLLDULRLRLRDUUURUUDLRLDURUDRLDULLUDLDLLRDUURRDUDURLUUUDLLRRULRLULRLDLLURDURRULRLLRRDUDLLRDRRRRDLUUDRUUUDDLRLUDDDDDDRURRRUUURRDLLRURLDDLLDLRRLLLDRRULRRUDLDRDDRRLULURLLUURURURRRRUUUUURUDURLRLLLULULDLLDLRDRRULUDUDRDRRDRDRRDUDLLLRUDRUDDDULRULRRRDRLRUUUURUDURDUUULLULRUDDULDUUDLDURRD
+ULRULDDLDLULLLRRRLRUDDDDDLLDDUDLRRDULUUDRDLRRURDRRLUULRURUDRRULDLLLUDRUUDULULUDDRUDDDRDURRRDRDUUURLRDULUDRDRLDRUDDLLLDRRULUDLUDLDLLRRUDUULULDLDLLUURDLDDLLUUDURLURLLLDRDLDRRLRULUURRDRULRUUURULRRUDDDDLLDLDDLLRRLRRRRDUUDUDLDRDRRURDLRURULDLRDLLLLRUDRLLRDLRLRDURDRUDURRRLRDRDLLRLUDDDDRLRLLDUURRURLUURUULUDLUURDRRUDDLUDUDDDURRDRUDRLRULDULUUUUUUDDUDRUDUUURUDRRDLUDLUUDUULUDURDLDDDLLURRURUUDUDDRRDRLLULULDRLRURRDDDRDUUURDDDRULUDRDDLDURRLDDDLRRRLDDRDURULDLUDLLLURLURRLRRULDLLDDUDRRULDRRRRLURRUULRRRUDLURDLLDLLDULUUDRRLDLLLDRLRUDLUULDLDRUDUDURDRUDRDDDLRLULLUR
+LRLUUURRLRRRRRUURRLLULRLULLDLUDLUDRDDRLDLRLULLURDURLURDLLRLDUUDDURRRRLDLLRULLRLDLLUUDRLDDLLDRULDRLLRURDLRURRUDLULLRURDLURRURUDULLDRLLUUULUDRURRUUDUDULUUULRLDDULDRDLUDDUDDDLRURULLDLLLRLLUURDLRUDLLLLDLLRLRUUUDDRUUUUDLDLRDDURLDURUULLLUUDLLLLDULRRRLLDLDRRDRLUDRUDURLLUDLRLLUDUDRDDDRDLRDLRULUULDRLUDLRLDUURLRRLUDDDUUDDDUDRLDLDUDLURUULLDDDURUUULRLUDLDURUUDRDRURUDDUURDUUUDLLDLDLDURUURLLLLRURUURURULRULLRUDLRRUUUUUDRRLLRDDUURDRDRDDDUDRLURDRRRUDLLLDURDLUUDLLUDDULUUDLDUUULLDRDLRURUURRDURRDLURRRRLLUUULRDULDDLDUURRDLDLLULRRLLUDLDUDLUUL
diff --git a/aoc2/src/main.rs b/aoc2/src/main.rs
new file mode 100644 (file)
index 0000000..e3188e7
--- /dev/null
@@ -0,0 +1,91 @@
+use std::io::BufReader;
+use std::io::prelude::*;
+use std::fs::File;
+
+fn main() {
+    let lines = read_file();
+    let mut current = '5';
+    for line in lines {
+        current = line.chars().fold(current, |current, dir| move_char_2(current, dir));
+        println!("{}", current);
+    }
+}
+
+fn read_file() -> Vec<String> {
+    let file = BufReader::new(File::open("input.txt").unwrap());
+    file.lines()
+        .map(|line| line.unwrap().trim().to_string())
+        .filter(|line| line.len() > 0)
+        .collect()
+}
+
+
+//assume current in 1-9 range, and char is in UDLR
+fn move_char(current: i32, dir: char) -> i32 {
+    match dir {
+        'U' => if current <= 3 { current } else { current - 3},
+        'D' => if current >= 7 { current } else { current + 3},
+        'L' => if current%3 == 1 { current } else { current - 1},
+        'R' => if current%3 == 0 { current } else { current + 1},
+        _ => panic!("Bad direction character")
+    }
+}
+
+//     1
+//   2 3 4
+// 5 6 7 8 9
+//   A B C
+//     D
+fn move_char_2(current: char, dir: char) -> char {
+    match dir {
+        'U' => match current {
+            '1'|'2'|'4'|'5'|'9' => current,
+            '3' => '1',
+            '6' => '2',
+            '7' => '3',
+            '8' => '4',
+            'A' => '6',
+            'B' => '7',
+            'C' => '8',
+            'D' => 'B',
+            _ => panic!("Bad current char")
+        },
+        'D' => match current {
+            'A'|'D'|'C'|'5'|'9' => current,
+            '1' => '3',
+            '2' => '6',
+            '3' => '7',
+            '4' => '8',
+            '6' => 'A',
+            '7' => 'B',
+            '8' => 'C',
+            'B' => 'D',
+            _ => panic!("Bad current char")
+        },
+        'L' => match current {
+            '1'|'2'|'5'|'A'|'D' => current,
+            '3' => '2',
+            '4' => '3',
+            '6' => '5',
+            '7' => '6',
+            '8' => '7',
+            '9' => '8',
+            'B' => 'A',
+            'C' => 'B',
+            _ => panic!("Bad current char")
+        },
+        'R' => match current {
+            '1'|'4'|'9'|'C'|'D' => current,
+            '2' => '3',
+            '3' => '4',
+            '5' => '6',
+            '6' => '7',
+            '7' => '8',
+            '8' => '9',
+            'A' => 'B',
+            'B' => 'C',
+            _ => panic!("Bad current char")
+        },
+        _ => panic!("Bad direction character")
+    }
+}
diff --git a/aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/bin-aoc2 b/aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/bin-aoc2
new file mode 100644 (file)
index 0000000..0dc8be0
--- /dev/null
@@ -0,0 +1 @@
+0c5f58e77a9a46ab
\ No newline at end of file
diff --git a/aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/bin-aoc2.json b/aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/bin-aoc2.json
new file mode 100644 (file)
index 0000000..0324547
--- /dev/null
@@ -0,0 +1 @@
+{"rustc":3005860601515633040,"target":2412896966351654203,"profile":13023446655302672390,"local":{"variant":"MtimeBased","fields":[[1480657613,555107500],[67,58,92,80,114,111,106,92,67,111,109,112,101,116,105,116,105,111,110,115,92,97,100,118,101,110,116,111,102,99,111,100,101,50,48,49,54,92,97,111,99,50,92,116,97,114,103,101,116,92,100,101,98,117,103,92,46,102,105,110,103,101,114,112,114,105,110,116,92,97,111,99,50,45,101,101,54,48,52,53,53,102,55,99,53,99,55,50,56,50,92,100,101,112,45,98,105,110,45,97,111,99,50]]},"features":"None","deps":[]}
\ No newline at end of file
diff --git a/aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/dep-bin-aoc2 b/aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/dep-bin-aoc2
new file mode 100644 (file)
index 0000000..0eb816a
Binary files /dev/null and b/aoc2/target/debug/.fingerprint/aoc2-ee60455f7c5c7282/dep-bin-aoc2 differ
diff --git a/aoc2/target/debug/aoc2.exe b/aoc2/target/debug/aoc2.exe
new file mode 100644 (file)
index 0000000..0bd13a6
Binary files /dev/null and b/aoc2/target/debug/aoc2.exe differ