Compare commits
2 Commits
1630b3c627
...
c90f871f7a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c90f871f7a | ||
|
|
f2d621dcb4 |
138
2025/09.rs
Normal file
138
2025/09.rs
Normal file
@@ -0,0 +1,138 @@
|
||||
---cargo
|
||||
[profile.dev]
|
||||
opt-level = 3
|
||||
---
|
||||
|
||||
static CONTENT: &'static str = include_str!("./inputs/09_1.txt");
|
||||
|
||||
const PRINT: bool = false;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
enum Cell {
|
||||
Empty,
|
||||
Red,
|
||||
Green,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let points: Vec<(usize, usize)> = CONTENT.lines().map(|l| l.trim()).filter(|l| !l.is_empty()).map(|l| {
|
||||
let (first, second) = l.split_once(',').unwrap();
|
||||
|
||||
let first = first.parse::<usize>().unwrap();
|
||||
let second = second.parse::<usize>().unwrap();
|
||||
|
||||
(first, second)
|
||||
}).collect();
|
||||
// println!("Points:\n{:?}", points);
|
||||
|
||||
let grid_width: usize = points.iter().map(|(x, _)| *x).max().unwrap()+1;
|
||||
let grid_height: usize = points.iter().map(|(y, _)| *y).max().unwrap()+1;
|
||||
|
||||
let mut grid = vec![Cell::Empty;grid_width*grid_height];
|
||||
for parts in points.windows(2).chain(core::iter::once(&[points.last().copied().unwrap(), points.first().copied().unwrap()] as &[(usize, usize)])) {
|
||||
let start = parts[0];
|
||||
let end = parts[1];
|
||||
|
||||
let g_points = line_point_iter(start, end);
|
||||
|
||||
grid[start.1 *grid_width+ start.0] = Cell::Red;
|
||||
for (x,y) in g_points {
|
||||
grid[y*grid_width+x] = Cell::Green;
|
||||
}
|
||||
}
|
||||
print_grid(&grid, grid_width, grid_height);
|
||||
|
||||
let inner_point = points.windows(2)
|
||||
.filter(|p| {
|
||||
let start = p[0];
|
||||
let end = p[1];
|
||||
|
||||
start.0 == end.0
|
||||
}).filter_map(|p| {
|
||||
let start = p[0];
|
||||
let end = p[1];
|
||||
|
||||
line_point_iter(start, end).next()
|
||||
}).flat_map(|(x, y)| {
|
||||
[(x-1, y), (x+1, y)]
|
||||
}).filter(|(x, y)| {
|
||||
(0..grid_width).contains(x)
|
||||
}).find(|(x, y)| {
|
||||
let c = (0..*x).map(|x| grid[y*grid_width + x]).filter(|c| *c == Cell::Green).count();
|
||||
|
||||
c == 1
|
||||
}).unwrap();
|
||||
|
||||
let mut queue = vec![inner_point];
|
||||
while let Some((x, y)) = queue.pop() {
|
||||
if !(0..grid_width).contains(&x) {
|
||||
continue;
|
||||
}
|
||||
if !(0..grid_height).contains(&y) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if grid[y*grid_width+x] != Cell::Empty {
|
||||
continue;
|
||||
}
|
||||
|
||||
grid[y*grid_width+x] = Cell::Green;
|
||||
|
||||
queue.extend([(x-1, y), (x+1, y), (x, y-1), (x, y+1)]);
|
||||
}
|
||||
|
||||
println!("After filling");
|
||||
print_grid(&grid, grid_width, grid_height);
|
||||
|
||||
let mut rects: Vec<((usize, usize), (usize, usize), usize)> = points.iter()
|
||||
.flat_map(|p| core::iter::repeat(*p).zip(points.iter().copied()))
|
||||
.filter(|(f, s)| f != s)
|
||||
.map(|(f, s)| (f, s, (f.0.abs_diff(s.0)+1) * (f.1.abs_diff(s.1)+1)))
|
||||
.filter(|(f, s, _)| {
|
||||
let x0 = f.0.min(s.0);
|
||||
let x1 = f.0.max(s.0);
|
||||
|
||||
let y0 = f.1.min(s.1);
|
||||
let y1 = f.1.max(s.1);
|
||||
|
||||
(x0..=x1).flat_map(|x| {
|
||||
core::iter::repeat(x).zip(y0..=y1)
|
||||
}).all(|(x, y)| grid[y*grid_width+x] != Cell::Empty)
|
||||
})
|
||||
.collect();
|
||||
|
||||
rects.sort_by_key(|(_, _, a)| *a);
|
||||
|
||||
// println!("Rectangle: {:?}", rects);
|
||||
|
||||
println!("Largest: {:?}", rects.last().unwrap());
|
||||
}
|
||||
|
||||
fn print_grid(grid: &[Cell], width: usize, height: usize) {
|
||||
if !PRINT {
|
||||
return;
|
||||
}
|
||||
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
match grid[y*width+x] {
|
||||
Cell::Empty => print!("."),
|
||||
Cell::Red => print!("#"),
|
||||
Cell::Green => print!("X"),
|
||||
};
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
fn line_point_iter(start: (usize, usize), end: (usize, usize)) -> Box<dyn Iterator<Item = (usize, usize)>> {
|
||||
if start.0 == end.0 {
|
||||
let s = start.1.min(end.1);
|
||||
let e = start.1.max(end.1);
|
||||
Box::new(core::iter::repeat(start.0).zip(s..e).skip(1))
|
||||
} else {
|
||||
let s = start.0.min(end.0);
|
||||
let e = start.0.max(end.0);
|
||||
Box::new((s..e).zip(core::iter::repeat(start.1)).skip(1))
|
||||
}
|
||||
}
|
||||
496
2025/inputs/09_1.txt
Normal file
496
2025/inputs/09_1.txt
Normal file
@@ -0,0 +1,496 @@
|
||||
98149,50096
|
||||
98149,51320
|
||||
98283,51320
|
||||
98283,52552
|
||||
98418,52552
|
||||
98418,53717
|
||||
97561,53717
|
||||
97561,54916
|
||||
97402,54916
|
||||
97402,56191
|
||||
97852,56191
|
||||
97852,57374
|
||||
97504,57374
|
||||
97504,58479
|
||||
96769,58479
|
||||
96769,59792
|
||||
97171,59792
|
||||
97171,60938
|
||||
96714,60938
|
||||
96714,62177
|
||||
96649,62177
|
||||
96649,63356
|
||||
96328,63356
|
||||
96328,64508
|
||||
95920,64508
|
||||
95920,65463
|
||||
94945,65463
|
||||
94945,66631
|
||||
94632,66631
|
||||
94632,67713
|
||||
94088,67713
|
||||
94088,69039
|
||||
94123,69039
|
||||
94123,70099
|
||||
93515,70099
|
||||
93515,71061
|
||||
92720,71061
|
||||
92720,72524
|
||||
92910,72524
|
||||
92910,73136
|
||||
91489,73136
|
||||
91489,74419
|
||||
91293,74419
|
||||
91293,75648
|
||||
90966,75648
|
||||
90966,76623
|
||||
90221,76623
|
||||
90221,77323
|
||||
89089,77323
|
||||
89089,78420
|
||||
88541,78420
|
||||
88541,79539
|
||||
88005,79539
|
||||
88005,80155
|
||||
86832,80155
|
||||
86832,81503
|
||||
86549,81503
|
||||
86549,82398
|
||||
85716,82398
|
||||
85716,82997
|
||||
84574,82997
|
||||
84574,84354
|
||||
84217,84354
|
||||
84217,84671
|
||||
82826,84671
|
||||
82826,85481
|
||||
81928,85481
|
||||
81928,86391
|
||||
81114,86391
|
||||
81114,87480
|
||||
80436,87480
|
||||
80436,88103
|
||||
79372,88103
|
||||
79372,89046
|
||||
78552,89046
|
||||
78552,89441
|
||||
77335,89441
|
||||
77335,89838
|
||||
76141,89838
|
||||
76141,90654
|
||||
75227,90654
|
||||
75227,91365
|
||||
74239,91365
|
||||
74239,92081
|
||||
73247,92081
|
||||
73247,92782
|
||||
72239,92782
|
||||
72239,93026
|
||||
70998,93026
|
||||
70998,93413
|
||||
69842,93413
|
||||
69842,94169
|
||||
68850,94169
|
||||
68850,94753
|
||||
67772,94753
|
||||
67772,94968
|
||||
66552,94968
|
||||
66552,95166
|
||||
65337,95166
|
||||
65337,96281
|
||||
64419,96281
|
||||
64419,96109
|
||||
63094,96109
|
||||
63094,96659
|
||||
61981,96659
|
||||
61981,96466
|
||||
60685,96466
|
||||
60685,96669
|
||||
59493,96669
|
||||
59493,97420
|
||||
58401,97420
|
||||
58401,97047
|
||||
57111,97047
|
||||
57111,98113
|
||||
56029,98113
|
||||
56029,97715
|
||||
54755,97715
|
||||
54755,97760
|
||||
53541,97760
|
||||
53541,97640
|
||||
52320,97640
|
||||
52320,98082
|
||||
51122,98082
|
||||
51122,98415
|
||||
49903,98415
|
||||
49903,97492
|
||||
48701,97492
|
||||
48701,98259
|
||||
47455,98259
|
||||
47455,97501
|
||||
46287,97501
|
||||
46287,97603
|
||||
45063,97603
|
||||
45063,97948
|
||||
43795,97948
|
||||
43795,97422
|
||||
42638,97422
|
||||
42638,96938
|
||||
41490,96938
|
||||
41490,96859
|
||||
40272,96859
|
||||
40272,96275
|
||||
39163,96275
|
||||
39163,96615
|
||||
37830,96615
|
||||
37830,95679
|
||||
36830,95679
|
||||
36830,95815
|
||||
35524,95815
|
||||
35524,95641
|
||||
34297,95641
|
||||
34297,94734
|
||||
33330,94734
|
||||
33330,94188
|
||||
32246,94188
|
||||
32246,93771
|
||||
31112,93771
|
||||
31112,93562
|
||||
29878,93562
|
||||
29878,92941
|
||||
28829,92941
|
||||
28829,92633
|
||||
27621,92633
|
||||
27621,92127
|
||||
26507,92127
|
||||
26507,90926
|
||||
25797,90926
|
||||
25797,91063
|
||||
24291,91063
|
||||
24291,89724
|
||||
23705,89724
|
||||
23705,89668
|
||||
22272,89668
|
||||
22272,88546
|
||||
21576,88546
|
||||
21576,88024
|
||||
20445,88024
|
||||
20445,87398
|
||||
19380,87398
|
||||
19380,86115
|
||||
18869,86115
|
||||
18869,85268
|
||||
18007,85268
|
||||
18007,84643
|
||||
16936,84643
|
||||
16936,83906
|
||||
15957,83906
|
||||
15957,82842
|
||||
15310,82842
|
||||
15310,82330
|
||||
14071,82330
|
||||
14071,80937
|
||||
13816,80937
|
||||
13816,80518
|
||||
12418,80518
|
||||
12418,79314
|
||||
11971,79314
|
||||
11971,78448
|
||||
11096,78448
|
||||
11096,77441
|
||||
10404,77441
|
||||
10404,76510
|
||||
9599,76510
|
||||
9599,75048
|
||||
9633,75048
|
||||
9633,74084
|
||||
8898,74084
|
||||
8898,73037
|
||||
8297,73037
|
||||
8297,71955
|
||||
7763,71955
|
||||
7763,71218
|
||||
6522,71218
|
||||
6522,69800
|
||||
6678,69800
|
||||
6678,68987
|
||||
5508,68987
|
||||
5508,67570
|
||||
5755,67570
|
||||
5755,66636
|
||||
4802,66636
|
||||
4802,65333
|
||||
4844,65333
|
||||
4844,64195
|
||||
4435,64195
|
||||
4435,63235
|
||||
3393,63235
|
||||
3393,61931
|
||||
3537,61931
|
||||
3537,60707
|
||||
3437,60707
|
||||
3437,59485
|
||||
3372,59485
|
||||
3372,58325
|
||||
3010,58325
|
||||
3010,57128
|
||||
2838,57128
|
||||
2838,55962
|
||||
2421,55962
|
||||
2421,54802
|
||||
1820,54802
|
||||
1820,53521
|
||||
2508,53521
|
||||
2508,52311
|
||||
2539,52311
|
||||
2539,51115
|
||||
2219,51115
|
||||
2219,50089
|
||||
94539,50089
|
||||
94539,48701
|
||||
2499,48701
|
||||
2499,47465
|
||||
1920,47465
|
||||
1920,46251
|
||||
2036,46251
|
||||
2036,45011
|
||||
1902,45011
|
||||
1902,43822
|
||||
2259,43822
|
||||
2259,42693
|
||||
2933,42693
|
||||
2933,41483
|
||||
3024,41483
|
||||
3024,40315
|
||||
3347,40315
|
||||
3347,39095
|
||||
3432,39095
|
||||
3432,37844
|
||||
3437,37844
|
||||
3437,36624
|
||||
3606,36624
|
||||
3606,35415
|
||||
3840,35415
|
||||
3840,34519
|
||||
5004,34519
|
||||
5004,33174
|
||||
4846,33174
|
||||
4846,32153
|
||||
5579,32153
|
||||
5579,31033
|
||||
6045,31033
|
||||
6045,29764
|
||||
6189,29764
|
||||
6189,28954
|
||||
7311,28954
|
||||
7311,27722
|
||||
7558,27722
|
||||
7558,26834
|
||||
8459,26834
|
||||
8459,25657
|
||||
8836,25657
|
||||
8836,24362
|
||||
9050,24362
|
||||
9050,23308
|
||||
9676,23308
|
||||
9676,22505
|
||||
10665,22505
|
||||
10665,21542
|
||||
11408,21542
|
||||
11408,20615
|
||||
12195,20615
|
||||
12195,19321
|
||||
12528,19321
|
||||
12528,18910
|
||||
13931,18910
|
||||
13931,18053
|
||||
14782,18053
|
||||
14782,16947
|
||||
15368,16947
|
||||
15368,15986
|
||||
16122,15986
|
||||
16122,15275
|
||||
17123,15275
|
||||
17123,14036
|
||||
17638,14036
|
||||
17638,13749
|
||||
19005,13749
|
||||
19005,12493
|
||||
19542,12493
|
||||
19542,11719
|
||||
20490,11719
|
||||
20490,11063
|
||||
21528,11063
|
||||
21528,10484
|
||||
22613,10484
|
||||
22613,9632
|
||||
23511,9632
|
||||
23511,9231
|
||||
24702,9231
|
||||
24702,8969
|
||||
25957,8969
|
||||
25957,7711
|
||||
26638,7711
|
||||
26638,7493
|
||||
27904,7493
|
||||
27904,6884
|
||||
28957,6884
|
||||
28957,6354
|
||||
30051,6354
|
||||
30051,5683
|
||||
31086,5683
|
||||
31086,5537
|
||||
32342,5537
|
||||
32342,4632
|
||||
33300,4632
|
||||
33300,4368
|
||||
34504,4368
|
||||
34504,4004
|
||||
35669,4004
|
||||
35669,3899
|
||||
36908,3899
|
||||
36908,3887
|
||||
38158,3887
|
||||
38158,2838
|
||||
39155,2838
|
||||
39155,2664
|
||||
40370,2664
|
||||
40370,2502
|
||||
41585,2502
|
||||
41585,2442
|
||||
42811,2442
|
||||
42811,2202
|
||||
44009,2202
|
||||
44009,1773
|
||||
45193,1773
|
||||
45193,1779
|
||||
46424,1779
|
||||
46424,2113
|
||||
47667,2113
|
||||
47667,2461
|
||||
48890,2461
|
||||
48890,1879
|
||||
50096,1879
|
||||
50096,2150
|
||||
51308,2150
|
||||
51308,2067
|
||||
52526,2067
|
||||
52526,2162
|
||||
53739,2162
|
||||
53739,2567
|
||||
54919,2567
|
||||
54919,2277
|
||||
56174,2277
|
||||
56174,2732
|
||||
57337,2732
|
||||
57337,2982
|
||||
58524,2982
|
||||
58524,2825
|
||||
59792,2825
|
||||
59792,3597
|
||||
60865,3597
|
||||
60865,3656
|
||||
62098,3656
|
||||
62098,4231
|
||||
63195,4231
|
||||
63195,3956
|
||||
64547,3956
|
||||
64547,4822
|
||||
65543,4822
|
||||
65543,5323
|
||||
66648,5323
|
||||
66648,5708
|
||||
67794,5708
|
||||
67794,6332
|
||||
68842,6332
|
||||
68842,6545
|
||||
70071,6545
|
||||
70071,7095
|
||||
71152,7095
|
||||
71152,7819
|
||||
72140,7819
|
||||
72140,8090
|
||||
73371,8090
|
||||
73371,8558
|
||||
74506,8558
|
||||
74506,9355
|
||||
75446,9355
|
||||
75446,9575
|
||||
76758,9575
|
||||
76758,10546
|
||||
77577,10546
|
||||
77577,11065
|
||||
78709,11065
|
||||
78709,12226
|
||||
79360,12226
|
||||
79360,13026
|
||||
80271,13026
|
||||
80271,13333
|
||||
81605,13333
|
||||
81605,14454
|
||||
82243,14454
|
||||
82243,15429
|
||||
82993,15429
|
||||
82993,16448
|
||||
83686,16448
|
||||
83686,16730
|
||||
85140,16730
|
||||
85140,17580
|
||||
86027,17580
|
||||
86027,18788
|
||||
86504,18788
|
||||
86504,19587
|
||||
87450,19587
|
||||
87450,20649
|
||||
88074,20649
|
||||
88074,21621
|
||||
88808,21621
|
||||
88808,22620
|
||||
89504,22620
|
||||
89504,23897
|
||||
89778,23897
|
||||
89778,24528
|
||||
91047,24528
|
||||
91047,25567
|
||||
91695,25567
|
||||
91695,26551
|
||||
92445,26551
|
||||
92445,27672
|
||||
92953,27672
|
||||
92953,29003
|
||||
93020,29003
|
||||
93020,29887
|
||||
94005,29887
|
||||
94005,31018
|
||||
94478,31018
|
||||
94478,32106
|
||||
95057,32106
|
||||
95057,33388
|
||||
95127,33388
|
||||
95127,34671
|
||||
95140,34671
|
||||
95140,35702
|
||||
95891,35702
|
||||
95891,36936
|
||||
96002,36936
|
||||
96002,38053
|
||||
96524,38053
|
||||
96524,39176
|
||||
97068,39176
|
||||
97068,40340
|
||||
97484,40340
|
||||
97484,41668
|
||||
97025,41668
|
||||
97025,42848
|
||||
97316,42848
|
||||
97316,44004
|
||||
97834,44004
|
||||
97834,45193
|
||||
98224,45193
|
||||
98224,46461
|
||||
97726,46461
|
||||
97726,47670
|
||||
97819,47670
|
||||
97819,48871
|
||||
98356,48871
|
||||
98356,50096
|
||||
8
2025/inputs/09_example.txt
Normal file
8
2025/inputs/09_example.txt
Normal file
@@ -0,0 +1,8 @@
|
||||
7,1
|
||||
11,1
|
||||
11,7
|
||||
9,7
|
||||
9,5
|
||||
2,5
|
||||
2,3
|
||||
7,3
|
||||
Reference in New Issue
Block a user