Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ path = "./day-08/part-1/atauveron.rs"
[[bin]]
name = "day-08-part-2-atauveron"
path = "./day-08/part-2/atauveron.rs"

[[bin]]
name = "day-10-part-1-atauveron"
path = "./day-10/part-1/atauveron.rs"
31 changes: 31 additions & 0 deletions day-10/input/atauveron.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#...##.####.#.......#.##..##.#.
#.##.#..#..#...##..##.##.#.....
#..#####.#......#..#....#.###.#
...#.#.#...#..#.....#..#..#.#..
.#.....##..#...#..#.#...##.....
##.....#..........##..#......##
.##..##.#.#....##..##.......#..
#.##.##....###..#...##...##....
##.#.#............##..#...##..#
###..##.###.....#.##...####....
...##..#...##...##..#.#..#...#.
..#.#.##.#.#.#####.#....####.#.
#......###.##....#...#...#...##
.....#...#.#.#.#....#...#......
#..#.#.#..#....#..#...#..#..##.
#.....#..##.....#...###..#..#.#
.....####.#..#...##..#..#..#..#
..#.....#.#........#.#.##..####
.#.....##..#.##.....#...###....
###.###....#..#..#.....#####...
#..##.##..##.#.#....#.#......#.
.#....#.##..#.#.#.......##.....
##.##...#...#....###.#....#....
.....#.######.#.#..#..#.#.....#
.#..#.##.#....#.##..#.#...##..#
.##.###..#..#..#.###...#####.#.
#...#...........#.....#.......#
#....##.#.#..##...#..####...#..
#.####......#####.....#.##..#..
.#...#....#...##..##.#.#......#
#..###.....##.#.......#.##...##
80 changes: 80 additions & 0 deletions day-10/part-1/atauveron.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::env::args;
use std::time::Instant;

use std::collections::HashSet;

fn main() {
let now = Instant::now();
let output = run(&args().nth(1).expect("Please provide an input"));
let elapsed = now.elapsed();
println!("_duration:{}", elapsed.as_secs_f64() * 1000.);
println!("{}", output);
}

fn run(input: &str) -> usize {
let mut asteroids: Vec<(isize, isize)> = Vec::new();
let mut x = 0;
for line in input.lines() {
for (y, c) in line.char_indices() {
if c == '#' {
asteroids.push((x, y as isize));
}
}
x += 1;
}
let mut count: Vec<usize> = Vec::new();
for index in 0..asteroids.len() {
count.push(count_visible(index, &asteroids));
}
// Find and return the maximum
let mut max_loc = 0;
for v in count {
if v > max_loc {
max_loc = v;
}
}
max_loc
}

fn count_visible(index: usize, asteroids: &Vec<(isize, isize)>) -> usize {
let (pos_x, pos_y) = asteroids[index];
let mut directions: HashSet<(isize, isize)> = HashSet::new();
for asteroid in asteroids {
if asteroid.0 == pos_x && asteroid.1 == pos_y {
continue;
}
let d_x = asteroid.0 - pos_x;
let d_y = asteroid.1 - pos_y;
let d = gcd(d_x.abs() as usize, d_y.abs() as usize) as isize;
directions.insert((d_x / d, d_y / d));
}
directions.len()
}

fn gcd(first: usize, second: usize) -> usize {
// variable names based off Euclidean division equation: a = b * q + r
let (mut a, mut b) = if first > second {
(first, second)
} else {
(second, first)
};

while b != 0 {
let r = a % b;
a = b;
b = r;
}

a
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn run_test() {
let input = "......#.#.\n#..#.#....\n..#######.\n.#.#.###..\n.#..#.....\n..#....#.#\n#..#....#.\n.##.#..###\n##...#..#.\n.#....####\n";
assert_eq!(run(input), 33);
}
}