-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday_17.rs
126 lines (101 loc) · 3.79 KB
/
day_17.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use std::fs;
#[derive(Debug)]
struct Bounds {
x: [i32; 2],
y: [i32; 2],
}
pub fn run() {
let input_str: String = fs::read_to_string("inputs/input_17.txt")
.expect("Failed to read file");
let bounds: Bounds = parse_input(input_str);
part_one(&bounds);
part_two(&bounds);
}
fn parse_input(mut input_str: String) -> Bounds {
input_str = input_str[input_str.chars().position(|c| c == '=').unwrap()+1..].to_string();
let x_bounds: Vec<i32> = input_str[..input_str.chars().position(|c| c == ',').unwrap()]
.split("..")
.map(|s| s.parse::<i32>().expect("Couldn't parse x bounds"))
.collect();
let y_bounds: Vec<i32> = input_str[input_str.chars().position(|c| c == '=').unwrap()+1..]
.split("..")
.map(|s| s.parse::<i32>().expect("Couldn't parse y bounds"))
.collect();
Bounds {
x: [x_bounds[0], x_bounds[1]],
y: [y_bounds[0], y_bounds[1]],
}
}
fn part_one(bounds: &Bounds) {
// calculate x velocity
// final xpos = vx^2 - (vx*(vx-1))/2 (triangle nums) => (vx^2 + vx) / 2
// therefore:
let x_velocity: i32 = ((-1.0 + (1.0 + 8.0 * bounds.x[0] as f64).sqrt()) / 2.0).ceil() as i32;
println!("vx = {}", x_velocity);
// above is unnecessary but i feel bad removing it because i put effort into it :(
let y_velocity: i32; // launch y velocity
// vy of object at y=0 is y_velocity...
if bounds.y[0] < 0 {
// ...if bounds are below then distance to lowest bound must be y_velocity + 1
y_velocity = -bounds.y[0] - 1;
}
else {
// ...if bounds are above then distance to lowest bound must be y_velocity
y_velocity = bounds.y[0];
}
let highest_point: i32 = (y_velocity*y_velocity + y_velocity) / 2;
println!("Part 1: {}", highest_point);
}
fn part_two(bounds: &Bounds) {
// store amount of initial velocities found
let mut count: u32 = 0;
// gets lower bound for velocity using now-not-entirely-useless equation from part 1
let min_vx: i32 = ((-1.0 + (1.0 + 8.0 * bounds.x[0] as f64).sqrt()) / 2.0).ceil() as i32;
for vx_start in min_vx..(bounds.x[1] + 1) {
let mut time = 0;
let mut x = 0;
let mut vx = vx_start;
// calculate time, vx, and x when the probe first enters the target area
loop {
x += vx;
vx = if vx > 0 { vx - 1 } else { 0 };
time += 1;
if x >= bounds.x[0] {
break;
}
}
// if x is outside the boundaries, go to the next vx_start
// no need to account for x not reaching the boundaries (< bounds.x[0]) since min_vx is being used
if x > bounds.x[1] {
continue;
}
for vy_start in bounds.y[0]..1000 {
// set variables to shadow upper variables
// so that upper ones dont get changed within this nested loop
let mut time = time;
let mut x = x;
let mut vx = vx;
// calculate y position when x first enters target area
let mut y = 0;
for i in 0..time {
y += vy_start - i;
}
let mut vy = vy_start - time;
// loop while probe could still potentially hit
while x <= bounds.x[1] && (vx > 0 || vy > 0 || bounds.y[0] <= y) {
// if target hit, increment count and go to next vx_start
if y >= bounds.y[0] && y <= bounds.y[1] {
count += 1;
break;
}
// update position, velocity, and time
x += vx;
y += vy;
vy -= 1;
vx = if vx > 0 { vx - 1 } else { 0 };
time += 1;
}
}
}
println!("Part 2: {count}");
}