Skip to content

Commit 85d640e

Browse files
committed
day21pt2: done
This is completely impractical to simulate a solution for. Instead we compute significant early values, feed those values into Wolfram Alpha to get the quadratic formula that fits them, then extrapolate that formula the necessary number of steps.
1 parent e38aaef commit 85d640e

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ members = [
4141
"day20",
4242
"day20pt2",
4343
"day21",
44+
"day21pt2",
4445
]
4546

4647
[profile.dev]

day21pt2/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "day21pt2"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

day21pt2/quadtratic.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
15427 * x^2 + 15560 * x + 3921
2+
https://www.wolframalpha.com/input?i=quadratic+fit+calculator&assumption=%7B%22F%22%2C+%22QuadraticFitCalculator%22%2C+%22data3x%22%7D+-%3E%22%7B0%2C+1%2C+2%7D%22&assumption=%7B%22F%22%2C+%22QuadraticFitCalculator%22%2C+%22data3y%22%7D+-%3E%22%7B3921%2C+34908%2C+96749%7D%22
3+
https://www.wolframalpha.com/input?i=3921+%2B+15560+x+%2B+15427+x%5E2%2C+x+%3D+202300
4+
631357596621921

day21pt2/src/main.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
use std::collections::HashSet;
2+
use std::io::Write;
3+
4+
fn take_step(
5+
garden: &HashSet<(isize, isize)>,
6+
w: isize,
7+
h: isize,
8+
positions: &HashSet<(isize, isize)>,
9+
) -> HashSet<(isize, isize)> {
10+
let mut new_positions: HashSet<(isize, isize)> = HashSet::new();
11+
for pos in positions {
12+
let mut x = pos.0;
13+
let mut left_x = pos.0 - 1;
14+
let mut right_x = pos.0 + 1;
15+
let mut y = pos.1;
16+
let mut up_y = pos.1 - 1;
17+
let mut down_y = pos.1 + 1;
18+
while x < 0 {
19+
x += w;
20+
}
21+
while left_x < 0 {
22+
left_x += w;
23+
}
24+
while right_x < 0 {
25+
right_x += w;
26+
}
27+
while y < 0 {
28+
y += h;
29+
}
30+
while up_y < 0 {
31+
up_y += h;
32+
}
33+
while down_y < 0 {
34+
down_y += h;
35+
}
36+
x %= w;
37+
left_x %= w;
38+
right_x %= w;
39+
y %= h;
40+
up_y %= h;
41+
down_y %= h;
42+
if !garden.contains(&(left_x, y)) {
43+
new_positions.insert((pos.0 - 1, pos.1));
44+
}
45+
if !garden.contains(&(right_x, y)) {
46+
new_positions.insert((pos.0 + 1, pos.1));
47+
}
48+
if !garden.contains(&(x, up_y)) {
49+
new_positions.insert((pos.0, pos.1 - 1));
50+
}
51+
if !garden.contains(&(x, down_y)) {
52+
new_positions.insert((pos.0, pos.1 + 1));
53+
}
54+
}
55+
new_positions
56+
}
57+
58+
fn main() {
59+
let mut garden: HashSet<(isize, isize)> = HashSet::new();
60+
let mut positions: HashSet<(isize, isize)> = HashSet::new();
61+
let mut garden_width: isize = 0;
62+
let mut garden_height: isize = 0;
63+
for (y, line) in std::io::stdin().lines().map(Result::unwrap).enumerate() {
64+
garden_height += 1;
65+
for (x, c) in line.chars().enumerate() {
66+
if garden_height == 1 {
67+
garden_width += 1;
68+
}
69+
if c == '#' {
70+
garden.insert((x as isize, y as isize));
71+
}
72+
if c == 'S' {
73+
positions.insert((x as isize, y as isize));
74+
}
75+
}
76+
}
77+
for i in 1..=garden_width / 2 + garden_width * 2 {
78+
positions = take_step(&garden, garden_width, garden_height, &positions);
79+
if i == garden_width / 2
80+
|| i == garden_width / 2 + garden_width
81+
|| i == garden_width / 2 + garden_width * 2
82+
{
83+
print!("{} ", positions.len());
84+
let _ = std::io::stdout().flush();
85+
}
86+
}
87+
println!("\nfind quadratic with Wolfram Alpha (x = 0, 1, 2), then solve for x = 202300");
88+
}

0 commit comments

Comments
 (0)