This commit is contained in:
2025-12-09 20:31:55 +00:00
parent fe3aa98c22
commit 31fa002b1b
5 changed files with 739 additions and 0 deletions

View File

@@ -35,5 +35,9 @@ path = "src/day7/main.rs"
name = "day8" name = "day8"
path = "src/day8/main.rs" path = "src/day8/main.rs"
[[bin]]
name = "day9"
path = "src/day9/main.rs"
[dependencies] [dependencies]
anyhow = "1.0.100" anyhow = "1.0.100"

10
src/day9/act.html Normal file

File diff suppressed because one or more lines are too long

496
src/day9/act.txt Normal file
View File

@@ -0,0 +1,496 @@
98004,50283
98004,51502
98073,51502
98073,52727
98144,52727
98144,53937
97953,53937
97953,55168
98003,55168
98003,56406
98033,56406
98033,57473
96926,57473
96926,58748
97199,58748
97199,59926
96895,59926
96895,61220
97084,61220
97084,62314
96426,62314
96426,63395
95789,63395
95789,64490
95244,64490
95244,65829
95432,65829
95432,66934
94907,66934
94907,68050
94423,68050
94423,69341
94345,69341
94345,70156
93192,70156
93192,71485
93153,71485
93153,72568
92590,72568
92590,73702
92113,73702
92113,74741
91467,74741
91467,75567
90483,75567
90483,76688
89978,76688
89978,77537
89068,77537
89068,78372
88163,78372
88163,79469
87609,79469
87609,80612
87093,80612
87093,81578
86346,81578
86346,82422
85463,82422
85463,82939
84244,82939
84244,84206
83804,84206
83804,84848
82735,84848
82735,85434
81634,85434
81634,86745
81168,86745
81168,87730
80395,87730
80395,87950
79018,87950
79018,88564
77968,88564
77968,89681
77272,89681
77272,90032
76045,90032
76045,90883
75148,90883
75148,91125
73882,91125
73882,91934
72952,91934
72952,92557
71910,92557
71910,92993
70774,92993
70774,93784
69804,93784
69804,94065
68601,94065
68601,94737
67564,94737
67564,94644
66234,94644
66234,95260
65172,95260
65172,96308
64228,96308
64228,96569
63027,96569
63027,96815
61826,96815
61826,96343
60465,96343
60465,96835
59336,96835
59336,97723
58262,97723
58262,97006
56916,97006
56916,97227
55731,97227
55731,97435
54540,97435
54540,98266
53388,98266
53388,98391
52167,98391
52167,98195
50936,98195
50936,98172
49715,98172
49715,97959
48500,97959
48500,98135
47273,98135
47273,98219
46040,98219
46040,97295
44907,97295
44907,97942
43605,97942
43605,96967
42520,96967
42520,96937
41300,96937
41300,96652
40124,96652
40124,96994
38801,96994
38801,96419
37687,96419
37687,95965
36552,95965
36552,95897
35300,95897
35300,95312
34211,95312
34211,94929
33056,94929
33056,94007
32118,94007
32118,93804
30894,93804
30894,93729
29593,93729
29593,93046
28568,93046
28568,92453
27503,92453
27503,91601
26586,91601
26586,90840
25632,90840
25632,90635
24336,90635
24336,89546
23599,89546
23599,89479
22173,89479
22173,88691
21234,88691
21234,87455
20651,87455
20651,87191
19306,87191
19306,86273
18485,86273
18485,85089
17918,85089
17918,84736
16587,84736
16587,83568
16032,83568
16032,83102
14760,83102
14760,82116
14026,82116
14026,81313
13084,81313
13084,79817
12987,79817
12987,79115
11922,79115
11922,78148
11187,78148
11187,77151
10493,77151
10493,75950
10112,75950
10112,74905
9511,74905
9511,74201
8326,74201
8326,72904
8152,72904
8152,71838
7581,71838
7581,70676
7210,70676
7210,69798
6230,69798
6230,68607
5921,68607
5921,67692
4935,67692
4935,66370
4983,66370
4983,65240
4534,65240
4534,64208
3759,64208
3759,62870
3992,62870
3992,61715
3622,61715
3622,60663
2780,60663
2780,59343
3127,59343
3127,58209
2581,58209
2581,57040
2155,57040
2155,55761
2518,55761
2518,54592
2022,54592
2022,53344
2362,53344
2362,52146
2079,52146
2079,50925
2365,50925
2365,50265
94901,50265
94901,48488
1659,48488
1659,47308
2494,47308
2494,46043
1812,46043
1812,44875
2401,44875
2401,43591
1950,43591
1950,42377
2134,42377
2134,41191
2472,41191
2472,40153
3483,40153
3483,38963
3684,38963
3684,37617
3317,37617
3317,36454
3697,36454
3697,35377
4342,35377
4342,34263
4837,34263
4837,32955
4800,32955
4800,31869
5379,31869
5379,30923
6262,30923
6262,29782
6675,29782
6675,28650
7119,28650
7119,27415
7379,27415
7379,26415
8095,26415
8095,25406
8781,25406
8781,24145
9061,24145
9061,23300
10005,23300
10005,22154
10494,22154
10494,21597
11796,21597
11796,20131
11881,20131
11881,19735
13328,19735
13328,18722
13998,18722
13998,17795
14776,17795
14776,16811
15496,16811
15496,15841
16243,15841
16243,14681
16822,14681
16822,14162
18004,14162
18004,13340
18903,13340
18903,12715
19963,12715
19963,11815
20802,11815
20802,11108
21793,11108
21793,10828
23078,10828
23078,10062
24016,10062
24016,9070
24823,9070
24823,8392
25837,8392
25837,7547
26764,7547
26764,7731
28238,7731
28238,6379
28922,6379
28922,5921
30062,5921
30062,5661
31282,5661
31282,5137
32386,5137
32386,5118
33678,5118
33678,4277
34673,4277
34673,4403
35989,4403
35989,4090
37156,4090
37156,3656
38292,3656
38292,3559
39512,3559
39512,2594
40549,2594
40549,2472
41771,2472
41771,2323
42984,2323
42984,2689
44258,2689
44258,2443
45448,2443
45448,2139
46639,2139
46639,1555
47830,1555
47830,1845
49064,1845
49064,1634
50286,1634
50286,2188
51494,2188
51494,2386
52697,2386
52697,2595
53892,2595
53892,2621
55101,2621
55101,2661
56313,2661
56313,2728
57528,2728
57528,2364
58828,2364
58828,2818
59987,2818
59987,2845
61236,2845
61236,3836
62244,3836
62244,3791
63518,3791
63518,4250
64651,4250
64651,4284
65928,4284
65928,5346
66839,5346
66839,5849
67939,5849
67939,5650
69343,5650
69343,6127
70473,6127
70473,7026
71396,7026
71396,7311
72621,7311
72621,7898
73695,7898
73695,8360
74844,8360
74844,9323
75689,9323
75689,9834
76813,9834
76813,10909
77552,10909
77552,11764
78426,11764
78426,12198
79619,12198
79619,12720
80766,12720
80766,13911
81353,13911
81353,14300
82639,14300
82639,15444
83238,15444
83238,15925
84480,15925
84480,17167
84951,17167
84951,18280
85528,18280
85528,19271
86225,19271
86225,20163
87036,20163
87036,20724
88286,20724
88286,21775
88917,21775
88917,22924
89395,22924
89395,24077
89844,24077
89844,24888
90823,24888
90823,25962
91392,25962
91392,27042
91944,27042
91944,28174
92391,28174
92391,28956
93550,28956
93550,30107
93978,30107
93978,31501
93820,31501
93820,32387
94861,32387
94861,33531
95288,33531
95288,34727
95560,34727
95560,35758
96349,35758
96349,37174
95847,37174
95847,38217
96640,38217
96640,39523
96391,39523
96391,40646
96922,40646
96922,41796
97381,41796
97381,43019
97440,43019
97440,44222
97609,44222
97609,45404
98016,45404
98016,46605
98352,46605
98352,47838
98266,47838
98266,49062
98278,49062
98278,50283

221
src/day9/main.rs Normal file
View File

@@ -0,0 +1,221 @@
use anyhow::{Result, bail};
use std::{
collections::{HashMap, HashSet},
fs,
ops::RangeInclusive,
};
#[derive(Debug, PartialEq, Eq, Hash)]
struct TilePos {
x: i64,
y: i64,
}
impl TilePos {
fn area(&self, other: &TilePos) -> u64 {
(((self.x - other.x).abs() + 1) * ((self.y - other.y).abs() + 1)) as u64
}
}
impl Into<TilePos> for &str {
fn into(self) -> TilePos {
let mut niter = self.split(",");
let x: i64 = niter.next().unwrap().parse().unwrap();
let y: i64 = niter.next().unwrap().parse().unwrap();
TilePos { x, y }
}
}
#[derive(Debug)]
struct Pair<'a> {
b1: &'a TilePos,
b2: &'a TilePos,
area: u64,
}
impl<'a> Pair<'a> {
fn new(b1: &'a TilePos, b2: &'a TilePos) -> Pair<'a> {
Pair {
b1,
b2,
area: b1.area(b2),
}
}
fn get_x_range(&'a self) -> RangeInclusive<i64> {
match self.b1.x.cmp(&self.b2.x) {
std::cmp::Ordering::Equal => self.b1.x..=self.b1.x,
std::cmp::Ordering::Less => self.b1.x..=self.b2.x,
std::cmp::Ordering::Greater => self.b2.x..=self.b2.x,
}
}
fn get_y_range(&'a self) -> RangeInclusive<i64> {
match self.b1.y.cmp(&self.b2.y) {
std::cmp::Ordering::Equal => self.b1.y..=self.b1.y,
std::cmp::Ordering::Less => self.b1.y..=self.b2.y,
std::cmp::Ordering::Greater => self.b2.y..=self.b2.y,
}
}
// This will only work if the pairs are inline
fn colide(&'a self, other: &'a Pair) -> Result<bool> {
if self.b1.x == self.b2.x {
if other.b1.x == other.b2.x {
let other_range = other.get_y_range();
let b1in = other_range.contains(&self.b1.y);
let b2in = other_range.contains(&self.b1.y);
// Might need to check this
return Ok(self.b1.x == other.b2.x && (b1in || b2in) && !(b1in && b2in));
} else if other.b1.y == other.b2.y {
return Ok(self.get_y_range().contains(&other.b1.y));
} else {
bail!("Did not follow instructions 1 {:?}", other);
}
} else if self.b1.y == self.b2.y {
if other.b1.y == other.b2.y {
let other_range = other.get_x_range();
let b1in = other_range.contains(&self.b1.x);
let b2in = other_range.contains(&self.b1.x);
// Might need to check this
return Ok(self.b1.y == other.b2.y && (b1in || b2in) && !(b1in && b2in));
} else if other.b1.x == other.b2.x {
return Ok(self.get_x_range().contains(&other.b1.y));
} else {
bail!("Did not follow instructions 2 {:?}", other);
}
} else {
bail!("Did not follow instructions 3 {:?}", self);
}
}
}
fn main() -> Result<()> {
let text = fs::read_to_string("src/day9/act.txt")?;
let boxes: Vec<TilePos> = text
.split("\n")
.filter(|s| s.len() > 0)
.map(|s| s.into())
.collect();
let mut order_pair = Vec::with_capacity(boxes.len() + 1);
for (i, b1) in boxes.iter().enumerate() {
order_pair.push(Pair::new(
b1,
if i + 1 >= boxes.len() {
&boxes[0]
} else {
&boxes[i]
},
))
}
let mut pairs: Vec<Pair> = Vec::with_capacity(boxes.len() * boxes.len());
for (i, b1) in boxes.iter().enumerate() {
for b2 in boxes.iter().skip(i) {
pairs.push(Pair::new(b1, b2))
}
}
pairs.sort_by(|a, b| a.area.cmp(&b.area));
let b = pairs.into_iter().rev().take(1).next().unwrap();
println!("sol1: {} {:?} {:?}", b.area, b.b1, b.b2);
let mut pairs: Vec<Pair> = Vec::with_capacity(boxes.len() * boxes.len());
for (i, b1) in boxes.iter().enumerate() {
for b2 in boxes.iter().skip(i) {
if b1.y >= 50265 && b2.y >= 50265 {
pairs.push(Pair::new(b1, b2))
}
if b1.y <= 48488 && b2.y <= 48488 {
pairs.push(Pair::new(b1, b2))
}
if b1.x >= 94901 && b2.x >= 94901 {
pairs.push(Pair::new(b1, b2))
}
}
}
pairs.sort_by(|a, b| a.area.cmp(&b.area));
'b2: for b in pairs.iter().rev() {
// //
// // 1 p
// //
// //
// // t 2
// //
let max_x = b.b1.x.max(b.b2.x);
let min_x = b.b1.x.min(b.b2.x);
let max_y = b.b1.y.max(b.b2.y);
let min_y = b.b1.y.min(b.b2.y);
for p in boxes.iter() {
if p.x > min_x && p.x < max_x && p.y > min_y && p.y < max_y {
println!(
"<path style=\"fill:blue\" d=\"M{} {} L{} {} L{} {} L{} {}\" count=\"{}\" /><circle x=\"{}\" y=\"{}\" r=\"20\">",
b.b1.x,
b.b1.y,
b.b2.x,
b.b1.y,
b.b2.x,
b.b2.y,
b.b1.x,
b.b2.y,
b.area,
p.x,
p.y
);
continue 'b2;
}
}
println!(
"<path style=\"fill:blue\" d=\"M{} {} L{} {} L{} {} L{} {}\" count=\"{}\">",
b.b1.x, b.b1.y, b.b2.x, b.b1.y, b.b2.x, b.b2.y, b.b1.x, b.b2.y, b.area
);
break;
}
// let boxes: Vec<TilePos> = text
// .split("\n")
// .filter(|s| s.len() > 0)
// .map(|s| s.into())
// .collect();
// let mut pairs: Vec<Pair> = Vec::with_capacity(boxes.len() * boxes.len());
// for (i, b1) in boxes.iter().enumerate() {
// for b2 in boxes.iter().skip(i) {
// if b1.y >= 50265 && b2.y >= 50265 {
// pairs.push(Pair::new(b1, b2))
// }
// if b1.y <= 48488 && b2.y <= 48488 {
// pairs.push(Pair::new(b1, b2))
// }
// }
// }
// pairs.sort_by(|a, b| a.area.cmp(&b.area));
// let pairs = pairs.iter().rev().take(20);
// for i in pairs {
// // 1 p
// //
// //
// // t 2
// println!(
// "<path style=\"fill:blue\" d=\"M{} {} L{} {} L{} {} L{} {}\" count=\"{}\">",
// i.b1.x, i.b1.y, i.b2.x, i.b1.y, i.b2.x, i.b2.y, i.b1.x, i.b2.y, i.area
// )
// }
Ok(())
}

8
src/day9/test.txt Normal file
View File

@@ -0,0 +1,8 @@
7,1
11,1
11,7
9,7
9,5
2,5
2,3
7,3