Skip to content

Commit 548ee58

Browse files
committed
feat: added part 1 of day 10
1 parent da4b159 commit 548ee58

File tree

6 files changed

+217
-0
lines changed

6 files changed

+217
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
- [Day 7](https://github.com/ankjevel/adventofcode/tree/2019/day_07) 🌟 🌟
1313
- [Day 8](https://github.com/ankjevel/adventofcode/tree/2019/day_08) 🌟 🌟
1414
- [Day 9](https://github.com/ankjevel/adventofcode/tree/2019/day_09) 🌟 🌟
15+
- [Day 10](https://github.com/ankjevel/adventofcode/tree/2019/day_10) 🌟

day_10/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "day_10"
3+
version = "0.1.0"
4+
authors = ["Dennis Pettersson <mail@dennispettersson.se>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
float-cmp = "0.6.0"
11+
12+
[[bin]]
13+
name = "day_10"
14+
path = "src/bin/main.rs"

day_10/src/bin/main.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
use ::day_10::{parse_input, part_01::main as part_01, to_points};
2+
3+
fn main() {
4+
let input = to_points(&parse_input(include_str!("../../../input/day_10")));
5+
println!("part_01: {}", part_01(&input).0);
6+
}
7+
8+
#[cfg(test)]
9+
#[allow(dead_code)]
10+
mod tests {
11+
use ::day_10::{parse_input, part_01::main as part_01, point::Point, to_points};
12+
13+
const EXAMPLE_01_00: &'static str = "
14+
.#..#
15+
.....
16+
#####
17+
....#
18+
...x#
19+
";
20+
21+
const EXAMPLE_01_01: &'static str = "
22+
......#.#.
23+
#..#.#....
24+
..#######.
25+
.#.#.###..
26+
.#..#.....
27+
..#....#.#
28+
#..#....#.
29+
.##.#..###
30+
##...x..#.
31+
.#....####
32+
";
33+
34+
const EXAMPLE_01_02: &'static str = "
35+
#.#...#.#.
36+
.###....#.
37+
.x....#...
38+
##.#.#.#.#
39+
....#.#.#.
40+
.##..###.#
41+
..#...##..
42+
..##....##
43+
......#...
44+
.####.###.
45+
";
46+
47+
const EXAMPLE_01_03: &'static str = "
48+
.#..#..###
49+
####.###.#
50+
....###.#.
51+
..###.x#.#
52+
##.##.#.#.
53+
....###..#
54+
..#.#..#.#
55+
#..#.#.###
56+
.##...##.#
57+
.....#.#..
58+
";
59+
60+
const EXAMPLE_01_04: &'static str = "
61+
.#..##.###...#######
62+
##.############..##.
63+
.#.######.########.#
64+
.###.#######.####.#.
65+
#####.##.#.##.###.##
66+
..#####..#.#########
67+
####################
68+
#.####....###.#.#.##
69+
##.#################
70+
#####.##.###..####..
71+
..######..##.#######
72+
####.##.####...##..#
73+
.#####..#.######.###
74+
##...#.####x#####...
75+
#.##########.#######
76+
.####.#.###.###.#.##
77+
....##.##.###..#####
78+
.#.#.###########.###
79+
#.#.#.#####.####.###
80+
###.##.####.##.#..##
81+
";
82+
83+
#[test]
84+
fn it_gets_the_examples_on_part_01_right() {
85+
let inputs: Vec<Vec<Point>> = vec![
86+
EXAMPLE_01_00,
87+
EXAMPLE_01_01,
88+
EXAMPLE_01_02,
89+
EXAMPLE_01_03,
90+
EXAMPLE_01_04,
91+
]
92+
.into_iter()
93+
.map(|example| to_points(&parse_input(&example)))
94+
.collect();
95+
96+
assert_eq!(part_01(&inputs[0]), (8, Some(Point { x: 3, y: 4 })));
97+
assert_eq!(part_01(&inputs[1]), (33, Some(Point { x: 5, y: 8 })));
98+
assert_eq!(part_01(&inputs[2]), (35, Some(Point { x: 1, y: 2 })));
99+
assert_eq!(part_01(&inputs[3]), (41, Some(Point { x: 6, y: 3 })));
100+
assert_eq!(part_01(&inputs[4]), (210, Some(Point { x: 11, y: 13 })));
101+
}
102+
}

day_10/src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#[macro_use]
2+
extern crate float_cmp;
3+
4+
pub mod part_01;
5+
pub mod point;
6+
7+
pub type Input = Vec<Vec<char>>;
8+
9+
pub fn to_points(input: &Input) -> Vec<point::Point> {
10+
let mut points = Vec::new();
11+
for (y, row) in input.into_iter().enumerate() {
12+
for (x, item) in row.into_iter().enumerate() {
13+
if item == &'.' {
14+
continue;
15+
}
16+
17+
points.push(point::Point::from_usize(x, y));
18+
}
19+
}
20+
21+
points
22+
}
23+
24+
pub fn parse_input(string: &str) -> Input {
25+
string
26+
.lines()
27+
.map(|string| string.trim())
28+
.filter(|string| !string.is_empty())
29+
.map(|string| string.chars().collect())
30+
.collect()
31+
}

day_10/src/part_01.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
use crate::point::Point;
2+
3+
fn distance(p1: &Point, p2: &Point) -> f64 {
4+
((p2.x as f64 - p1.x as f64).powf(2f64) + (p2.y as f64 - p1.y as f64).powf(2f64)).sqrt()
5+
}
6+
7+
fn is_between(a: &Point, c: &Point, b: &Point) -> bool {
8+
approx_eq!(
9+
f64,
10+
distance(&a, &c) + distance(&c, &b),
11+
distance(&a, &b),
12+
ulps = 2
13+
)
14+
}
15+
16+
pub fn main(input: &Vec<Point>) -> (u32, Option<Point>) {
17+
let mut max = 0;
18+
let mut best_match = None;
19+
20+
'a: for a in input {
21+
let mut new_max = 0;
22+
let mut matches = Vec::new();
23+
24+
'b: for b in input {
25+
if b == a {
26+
continue 'b;
27+
}
28+
29+
let mut intersected = false;
30+
31+
'c: for c in input {
32+
if a == c || b == c {
33+
continue 'c;
34+
}
35+
36+
if is_between(a, c, b) {
37+
intersected = true;
38+
break 'c;
39+
}
40+
}
41+
42+
if !intersected {
43+
matches.push(b.clone());
44+
new_max += 1;
45+
}
46+
}
47+
48+
if new_max > max {
49+
best_match = Some(a.clone());
50+
max = new_max;
51+
}
52+
}
53+
54+
(max, best_match)
55+
}

day_10/src/point.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#[derive(Clone, Copy, Eq, PartialEq, Hash, Debug)]
2+
pub struct Point {
3+
pub x: u64,
4+
pub y: u64,
5+
}
6+
7+
impl Point {
8+
pub fn from_usize(x: usize, y: usize) -> Point {
9+
Point {
10+
x: x as u64,
11+
y: y as u64,
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy