Skip to content

Commit 75083d7

Browse files
committed
feat: simplified things
1 parent 9ecf42a commit 75083d7

File tree

3 files changed

+102
-36
lines changed

3 files changed

+102
-36
lines changed

day03/src/main.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ use std::io::Result;
44

55
pub mod manhattan;
66
mod part_01;
7+
mod part_02;
78
pub mod structs;
89

910
use part_01::main as part_01;
11+
use part_02::main as part_02;
1012
use structs::Direction::{Down, Left, Right, Up};
1113
use structs::Movement;
1214

@@ -16,6 +18,8 @@ fn main() -> Result<()> {
1618
let wires = parse_input(include_str!("../../input/day_03"));
1719

1820
println!("part_01: {:?}", part_01(&wires).unwrap());
21+
println!("part_02: {:?}", part_02(&wires).unwrap());
22+
1923
Ok(())
2024
}
2125

@@ -59,12 +63,25 @@ mod tests {
5963

6064
const EXAMPLE_DATA_02: &'static str = "
6165
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
62-
U98,R91,D20,R16,D67,R40,U7,R15,U6,R77
66+
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7
67+
";
68+
69+
const EXAMPLE_DATA_00: &'static str = "
70+
R8,U5,L5,D3
71+
U7,R6,D4,L4
6372
";
6473

6574
#[test]
66-
fn it_gets_the_correct_result_based_on_examples() {
75+
fn it_gets_the_correct_result_based_on_examples_for_part_1() {
76+
assert_eq!(6, part_01(&parse_input(EXAMPLE_DATA_00)).unwrap());
6777
assert_eq!(159, part_01(&parse_input(EXAMPLE_DATA_01)).unwrap());
6878
assert_eq!(135, part_01(&parse_input(EXAMPLE_DATA_02)).unwrap());
6979
}
80+
81+
#[test]
82+
fn it_gets_the_correct_result_based_on_examples_for_part_2() {
83+
assert_eq!(30, part_02(&parse_input(EXAMPLE_DATA_00)).unwrap());
84+
assert_eq!(610, part_02(&parse_input(EXAMPLE_DATA_01)).unwrap());
85+
assert_eq!(410, part_02(&parse_input(EXAMPLE_DATA_02)).unwrap());
86+
}
7087
}

day03/src/part_01.rs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,51 +15,18 @@ enum State {
1515

1616
use State::{Empty, Intersect, Occupied, Start};
1717

18-
#[allow(dead_code)]
19-
fn min_max(input: &Vec<Vec<Point>>) -> (i32, i32, i32, i32) {
20-
let (max_x, max_y) = input.iter().fold((0, 0), |(max_x, max_y), point| {
21-
point.iter().fold((0, 0), |(max_x_x, max_y_y), point| {
22-
(
23-
max(max_x, max(max_x_x, point.x)),
24-
max(max_y, max(max_y_y, point.y)),
25-
)
26-
})
27-
});
28-
29-
let (min_x, min_y) = input.iter().fold((0, 0), |(min_x, min_y), point| {
30-
point.iter().fold((0, 0), |(min_x_x, min_y_y), point| {
31-
(
32-
min(min_x, min(min_x_x, point.x)),
33-
min(min_y, min(min_y_y, point.y)),
34-
)
35-
})
36-
});
37-
38-
(min_x, min_y, max_x, max_y)
39-
}
40-
4118
pub fn main(input: &Vec<Vec<Point>>) -> Result<u64> {
4219
let mut grid = HashMap::new();
4320
let mut intersecting = Vec::new();
4421
let start_point = Point { x: 0, y: 0 };
4522

4623
grid.insert(start_point, Start);
4724

48-
// # for printing the board, slow process
49-
// let (min_x, min_y, max_x, max_y) = min_max(&input);
50-
// for x in min_x..max_x {
51-
// for y in min_y..max_y {
52-
// let state = if x == 0 && y == 0 { Start } else { Empty };
53-
// let point = Point { x, y };
54-
// grid.insert(point, state);
55-
// }
56-
// }
57-
5825
let mut current_wire = 0;
5926
for points in input.iter() {
6027
let mut prev = Point::new();
6128

62-
for current in points.iter().peekable() {
29+
for current in points.iter() {
6330
for x in min(prev.x, current.x)..=max(prev.x, current.x) {
6431
for y in min(prev.y, current.y)..=max(prev.y, current.y) {
6532
let point = Point { x, y };
@@ -78,8 +45,10 @@ pub fn main(input: &Vec<Vec<Point>>) -> Result<u64> {
7845
};
7946
}
8047
}
48+
8149
prev = *current;
8250
}
51+
8352
current_wire += 1;
8453
}
8554

day03/src/part_02.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
use std::cmp::{max, min};
2+
use std::collections::HashMap;
3+
use std::io::Result;
4+
use std::u64::MAX;
5+
6+
use super::Point;
7+
8+
enum State {
9+
Occupied(u8, u64),
10+
Intersect,
11+
Start,
12+
Empty,
13+
}
14+
15+
use State::{Empty, Intersect, Occupied, Start};
16+
17+
fn calc_steps(from: i32, to: i32) -> i64 {
18+
(to as i64 - from as i64).abs()
19+
}
20+
21+
pub fn main(input: &Vec<Vec<Point>>) -> Result<u64> {
22+
let mut grid = HashMap::new();
23+
let mut shortest_route = MAX;
24+
25+
grid.insert(Point::new(), Start);
26+
27+
let mut current_wire = 0;
28+
for wire in input.iter() {
29+
let mut prev = Point::new();
30+
let mut steps_total = 0;
31+
32+
for current in wire.iter() {
33+
let x_neg = prev.x > current.x;
34+
let y_neg = prev.y > current.y;
35+
36+
let mut x = prev.x;
37+
let mut y = prev.y;
38+
39+
let mut steps_x = 0;
40+
let mut steps_y = 0;
41+
42+
let mut check_current_state = |point: Point, current_steps: u64| {
43+
let state = grid.entry(point).or_insert(Empty);
44+
match state {
45+
Empty => *state = Occupied(current_wire, current_steps),
46+
Occupied(occupied_by, occupied_by_steps) => {
47+
if *occupied_by != current_wire {
48+
shortest_route =
49+
min(shortest_route, *occupied_by_steps + current_steps);
50+
*state = Intersect;
51+
}
52+
}
53+
_ => {}
54+
};
55+
};
56+
57+
let check_current_steps =
58+
|steps_x: i64, steps_y: i64| -> u64 { (steps_total + (steps_x + steps_y)) as u64 };
59+
60+
for _ in min(prev.x, current.x)..max(prev.x, current.x) {
61+
x = if x_neg { x - 1 } else { x + 1 };
62+
steps_x = calc_steps(prev.x, x);
63+
check_current_state(Point { x, y }, check_current_steps(steps_x, steps_y));
64+
}
65+
66+
for _ in min(prev.y, current.y)..max(prev.y, current.y) {
67+
y = if y_neg { y - 1 } else { y + 1 };
68+
steps_y = calc_steps(prev.y, y);
69+
check_current_state(Point { x, y }, check_current_steps(steps_x, steps_y));
70+
}
71+
72+
steps_total += steps_x + steps_y;
73+
prev = *current;
74+
}
75+
76+
current_wire += 1;
77+
}
78+
79+
Ok(shortest_route)
80+
}

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