day 5 completed
This commit is contained in:
39
2023/day5/Cargo.lock
generated
Normal file
39
2023/day5/Cargo.lock
generated
Normal file
@@ -0,0 +1,39 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "advent-of-code"
|
||||
version = "2022.0.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa9b6f396f926720a5233bf0bc661245d5325f3b5ad33d7946bf74e491a14af1"
|
||||
|
||||
[[package]]
|
||||
name = "cute"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "45e700c2d1c3feea9b695e79b2dfeeb93040556a58c556fae23f71b1e6b449fd"
|
||||
|
||||
[[package]]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"advent-of-code",
|
||||
"cute",
|
||||
"itertools",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0"
|
||||
|
||||
[[package]]
|
||||
name = "itertools"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
||||
dependencies = [
|
||||
"either",
|
||||
]
|
||||
9
2023/day5/Cargo.toml
Normal file
9
2023/day5/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "day5"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
advent-of-code = "2022.0.66"
|
||||
cute = "0.3.0"
|
||||
itertools = "0.13.0"
|
||||
139
2023/day5/README.md
Normal file
139
2023/day5/README.md
Normal file
@@ -0,0 +1,139 @@
|
||||
|
||||
Advent of Code
|
||||
|
||||
[About][Events][Shop][Settings][Log Out]
|
||||
|
||||
SpaceMoehre 10*
|
||||
0xffff&2023
|
||||
|
||||
[Calendar][AoC++][Sponsors][Leaderboard][Stats]
|
||||
|
||||
Our sponsors help make Advent of Code possible:
|
||||
Accenture Federal Services - Technology & ingenuity moving missions forward – come solve problems with us. Hiring software engineers, developers, and more now. Refer someone to earn up to $20K.
|
||||
--- Day 5: If You Give A Seed A Fertilizer ---
|
||||
|
||||
You take the boat and find the gardener right where you were told he would be: managing a giant "garden" that looks more to you like a farm.
|
||||
|
||||
"A water source? Island Island is the water source!" You point out that Snow Island isn't receiving any water.
|
||||
|
||||
"Oh, we had to stop the water because we ran out of sand to filter it with! Can't make snow with dirty water. Don't worry, I'm sure we'll get more sand soon; we only turned off the water a few days... weeks... oh no." His face sinks into a look of horrified realization.
|
||||
|
||||
"I've been so busy making sure everyone here has food that I completely forgot to check why we stopped getting more sand! There's a ferry leaving soon that is headed over in that direction - it's much faster than your boat. Could you please go check it out?"
|
||||
|
||||
You barely have time to agree to this request when he brings up another. "While you wait for the ferry, maybe you can help us with our food production problem. The latest Island Island Almanac just arrived and we're having trouble making sense of it."
|
||||
|
||||
The almanac (your puzzle input) lists all of the seeds that need to be planted. It also lists what type of soil to use with each kind of seed, what type of fertilizer to use with each kind of soil, what type of water to use with each kind of fertilizer, and so on. Every type of seed, soil, fertilizer and so on is identified with a number, but numbers are reused by each category - that is, soil 123 and fertilizer 123 aren't necessarily related to each other.
|
||||
|
||||
For example:
|
||||
|
||||
seeds: 79 14 55 13
|
||||
|
||||
seed-to-soil map:
|
||||
50 98 2
|
||||
52 50 48
|
||||
|
||||
soil-to-fertilizer map:
|
||||
0 15 37
|
||||
37 52 2
|
||||
39 0 15
|
||||
|
||||
fertilizer-to-water map:
|
||||
49 53 8
|
||||
0 11 42
|
||||
42 0 7
|
||||
57 7 4
|
||||
|
||||
water-to-light map:
|
||||
88 18 7
|
||||
18 25 70
|
||||
|
||||
light-to-temperature map:
|
||||
45 77 23
|
||||
81 45 19
|
||||
68 64 13
|
||||
|
||||
temperature-to-humidity map:
|
||||
0 69 1
|
||||
1 0 69
|
||||
|
||||
humidity-to-location map:
|
||||
60 56 37
|
||||
56 93 4
|
||||
|
||||
The almanac starts by listing which seeds need to be planted: seeds 79, 14, 55, and 13.
|
||||
|
||||
The rest of the almanac contains a list of maps which describe how to convert numbers from a source category into numbers in a destination category. That is, the section that starts with seed-to-soil map: describes how to convert a seed number (the source) to a soil number (the destination). This lets the gardener and his team know which soil to use with which seeds, which water to use with which fertilizer, and so on.
|
||||
|
||||
Rather than list every source number and its corresponding destination number one by one, the maps describe entire ranges of numbers that can be converted. Each line within a map contains three numbers: the destination range start, the source range start, and the range length.
|
||||
|
||||
Consider again the example seed-to-soil map:
|
||||
|
||||
50 98 2
|
||||
52 50 48
|
||||
|
||||
The first line has a destination range start of 50, a source range start of 98, and a range length of 2. This line means that the source range starts at 98 and contains two values: 98 and 99. The destination range is the same length, but it starts at 50, so its two values are 50 and 51. With this information, you know that seed number 98 corresponds to soil number 50 and that seed number 99 corresponds to soil number 51.
|
||||
|
||||
The second line means that the source range starts at 50 and contains 48 values: 50, 51, ..., 96, 97. This corresponds to a destination range starting at 52 and also containing 48 values: 52, 53, ..., 98, 99. So, seed number 53 corresponds to soil number 55.
|
||||
|
||||
Any source numbers that aren't mapped correspond to the same destination number. So, seed number 10 corresponds to soil number 10.
|
||||
|
||||
So, the entire list of seed numbers and their corresponding soil numbers looks like this:
|
||||
|
||||
seed soil
|
||||
0 0
|
||||
1 1
|
||||
... ...
|
||||
48 48
|
||||
49 49
|
||||
50 52
|
||||
51 53
|
||||
... ...
|
||||
96 98
|
||||
97 99
|
||||
98 50
|
||||
99 51
|
||||
|
||||
With this map, you can look up the soil number required for each initial seed number:
|
||||
|
||||
Seed number 79 corresponds to soil number 81.
|
||||
Seed number 14 corresponds to soil number 14.
|
||||
Seed number 55 corresponds to soil number 57.
|
||||
Seed number 13 corresponds to soil number 13.
|
||||
|
||||
The gardener and his team want to get started as soon as possible, so they'd like to know the closest location that needs a seed. Using these maps, find the lowest location number that corresponds to any of the initial seeds. To do this, you'll need to convert each seed number through other categories until you can find its corresponding location number. In this example, the corresponding types are:
|
||||
|
||||
Seed 79, soil 81, fertilizer 81, water 81, light 74, temperature 78, humidity 78, location 82.
|
||||
Seed 14, soil 14, fertilizer 53, water 49, light 42, temperature 42, humidity 43, location 43.
|
||||
Seed 55, soil 57, fertilizer 57, water 53, light 46, temperature 82, humidity 82, location 86.
|
||||
Seed 13, soil 13, fertilizer 52, water 41, light 34, temperature 34, humidity 35, location 35.
|
||||
|
||||
So, the lowest location number in this example is 35.
|
||||
|
||||
What is the lowest location number that corresponds to any of the initial seed numbers?
|
||||
|
||||
Your puzzle answer was 3374647.
|
||||
--- Part Two ---
|
||||
|
||||
Everyone will starve if you only plant such a small number of seeds. Re-reading the almanac, it looks like the seeds: line actually describes ranges of seed numbers.
|
||||
|
||||
The values on the initial seeds: line come in pairs. Within each pair, the first value is the start of the range and the second value is the length of the range. So, in the first line of the example above:
|
||||
|
||||
seeds: 79 14 55 13
|
||||
|
||||
This line describes two ranges of seed numbers to be planted in the garden. The first range starts with seed number 79 and contains 14 values: 79, 80, ..., 91, 92. The second range starts with seed number 55 and contains 13 values: 55, 56, ..., 66, 67.
|
||||
|
||||
Now, rather than considering four seed numbers, you need to consider a total of 27 seed numbers.
|
||||
|
||||
In the above example, the lowest location number can be obtained from seed number 82, which corresponds to soil 84, fertilizer 84, water 84, light 77, temperature 45, humidity 46, and location 46. So, the lowest location number is 46.
|
||||
|
||||
Consider all of the initial seed numbers listed in the ranges on the first line of the almanac. What is the lowest location number that corresponds to any of the initial seed numbers?
|
||||
|
||||
Your puzzle answer was 6082852.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||
|
||||
At this point, you should return to your Advent calendar and try another puzzle.
|
||||
|
||||
If you still want to see it, you can get your puzzle input.
|
||||
|
||||
You can also [Shareon Twitter Mastodon] this puzzle.
|
||||
256
2023/day5/input
Normal file
256
2023/day5/input
Normal file
@@ -0,0 +1,256 @@
|
||||
seeds: 280775197 7535297 3229061264 27275209 77896732 178275214 2748861189 424413807 3663093536 130341162 613340959 352550713 1532286286 1115055792 1075412586 241030710 3430371306 138606714 412141395 146351614
|
||||
|
||||
seed-to-soil map:
|
||||
2328878418 2173757269 55676388
|
||||
1604614185 284259424 18300254
|
||||
871339571 2660737044 103656521
|
||||
0 2281891142 53219493
|
||||
3600371492 4267529956 27437340
|
||||
1755732868 600174302 280911746
|
||||
225053611 2335110635 5908609
|
||||
3686191373 3693094019 157260119
|
||||
2301308430 2341019244 27569988
|
||||
2249924082 2817678844 51384348
|
||||
3843451492 3390401982 116711122
|
||||
866920588 2368589232 4418983
|
||||
319632173 0 10243502
|
||||
2384554806 302559678 74545000
|
||||
2036644614 2373008215 45489224
|
||||
480632565 1433058022 386288023
|
||||
3448897781 4166650940 100879016
|
||||
2082133838 377104678 167790244
|
||||
2644445775 2418497439 242239605
|
||||
1305360177 881086048 292964881
|
||||
3960162614 3638948449 54145570
|
||||
385155055 1819346045 95477510
|
||||
4021970860 3919567265 19798429
|
||||
3085107497 3850354138 69213127
|
||||
230962220 195589471 88669953
|
||||
1261037103 2129434195 44323074
|
||||
3627808832 3085107497 52096478
|
||||
329875675 544894922 55279380
|
||||
1622914439 2229433657 52457485
|
||||
1675371924 1352697078 80360944
|
||||
974996092 1914823555 107394862
|
||||
3549776797 3574405015 50594695
|
||||
3679905310 3632662386 6286063
|
||||
1082390954 1174050929 178646149
|
||||
2459099806 10243502 185345969
|
||||
160435271 2764393565 53285279
|
||||
3154320624 3939365694 227285246
|
||||
53219493 2022218417 107215778
|
||||
3381605870 3507113104 67291911
|
||||
4041769289 3137203975 253198007
|
||||
4014308184 3624999710 7662676
|
||||
213720550 2875352319 11333061
|
||||
1598325058 2869063192 6289127
|
||||
|
||||
soil-to-fertilizer map:
|
||||
3389090999 2596751746 608341779
|
||||
105086589 871525822 220821758
|
||||
3187814092 3748571797 34038948
|
||||
1063982526 822144230 49381592
|
||||
4071580733 3394766608 36570558
|
||||
1969786851 1283647913 135669504
|
||||
2338688657 3782610745 100105665
|
||||
2105456355 1092347580 1733094
|
||||
1113364118 0 435632229
|
||||
325908347 1524404006 161994939
|
||||
2438794322 4084049871 210917425
|
||||
4108151291 3471527086 26197923
|
||||
3997432778 2273224151 74147955
|
||||
3161148447 2395583294 26665645
|
||||
621898776 435632229 386512001
|
||||
4134349214 3882716410 46400134
|
||||
2649711747 3205093525 86087105
|
||||
4256353957 2384855243 10728051
|
||||
487903286 1149652423 133995490
|
||||
0 1419317417 105086589
|
||||
4218232485 3356645136 38121472
|
||||
4180749348 2347372106 37483137
|
||||
2910301659 3497725009 250846788
|
||||
2735798852 2422248939 23521261
|
||||
2273224151 3291180630 65464506
|
||||
3262042960 3957001832 127048039
|
||||
1548996347 1686398945 420790504
|
||||
4267082008 3929116544 27885288
|
||||
2759320113 2445770200 150981546
|
||||
1008410777 1094080674 55571749
|
||||
3221853040 3431337166 40189920
|
||||
|
||||
fertilizer-to-water map:
|
||||
2007324874 2683611319 23475372
|
||||
1313988767 1367341468 459297221
|
||||
1773285988 1938578086 234038886
|
||||
257353189 1257656713 109684755
|
||||
221312488 2981571133 36040701
|
||||
3464645820 3633779898 112824320
|
||||
1132250794 2637724313 45887006
|
||||
1178137800 0 135850967
|
||||
810899989 2295076898 321350805
|
||||
3706800774 4118877604 99172458
|
||||
3805973232 4218050062 17186653
|
||||
3823159885 3746604218 37073582
|
||||
2305284688 135850967 719035066
|
||||
3146779680 1826638689 111939397
|
||||
3024319754 2172616972 122459926
|
||||
3959767492 3783677800 335199804
|
||||
3934534290 4235236715 25233202
|
||||
408129309 854886033 402770680
|
||||
3258719077 2616427703 21296610
|
||||
3577470140 3504449264 129330634
|
||||
367037944 3238924322 41091365
|
||||
3860233467 4260469917 34497379
|
||||
3894730846 3464645820 39803444
|
||||
0 3017611834 221312488
|
||||
2030800246 2707086691 274484442
|
||||
|
||||
water-to-light map:
|
||||
3878354467 2554575322 61538919
|
||||
280412900 1377596407 93760042
|
||||
684284359 195116588 99127467
|
||||
4078572373 3977755696 216394923
|
||||
2619535971 3527291682 385503365
|
||||
1493593206 1581399685 135608385
|
||||
3693719871 2668857297 184634596
|
||||
2554575322 3912795047 64960649
|
||||
3236793954 2853491893 456925917
|
||||
817630982 1044576172 333020235
|
||||
1205196262 294244055 132442996
|
||||
1739244827 1012243273 32332899
|
||||
1771577726 0 101384292
|
||||
0 101384292 4968095
|
||||
101989723 448809000 178423177
|
||||
4019116409 4194150619 21593654
|
||||
4040710063 3489429372 37862310
|
||||
4968095 627232177 97021628
|
||||
374172942 724253805 287989468
|
||||
783411826 106352387 34219156
|
||||
3005039336 2616114241 52743056
|
||||
3939893386 4215744273 79223023
|
||||
1629201591 1471356449 110043236
|
||||
3057782392 3310417810 179011562
|
||||
1337639258 1717008070 155953948
|
||||
1150651217 140571543 54545045
|
||||
662162410 426687051 22121949
|
||||
|
||||
light-to-temperature map:
|
||||
2762906378 2204259687 134492279
|
||||
106349111 681820034 721307054
|
||||
997408643 115890239 153244527
|
||||
1512335036 1822350940 381908747
|
||||
3858505517 4045949291 249018005
|
||||
1894243783 3569882248 437693111
|
||||
3820131585 4007575359 38373932
|
||||
1505707247 3508506612 6627789
|
||||
2693538653 3439138887 69367725
|
||||
3029056394 2470409703 186112104
|
||||
54501260 269134766 51847851
|
||||
1452421821 3193053968 53285426
|
||||
850537566 0 115890239
|
||||
4107523522 3246339394 159303690
|
||||
4266827212 3164913884 28140084
|
||||
0 447698702 54501260
|
||||
3450202466 1452421821 369929119
|
||||
2897398657 2338751966 131657737
|
||||
966427805 650839196 30980838
|
||||
2331936894 3515134401 54747847
|
||||
2420180544 2656521807 273358109
|
||||
1206941949 502199962 83686237
|
||||
1150653170 320982617 56288779
|
||||
1361055492 585886199 42071596
|
||||
1290628186 377271396 70427306
|
||||
2386684741 3405643084 33495803
|
||||
827656165 627957795 22881401
|
||||
3215168498 2929879916 235033968
|
||||
|
||||
temperature-to-humidity map:
|
||||
3512511508 3713321076 246696808
|
||||
1575177202 3115625465 474937809
|
||||
4090629460 2000274085 135040194
|
||||
1141835951 1250511056 29255284
|
||||
151257068 40127528 247347323
|
||||
796868343 1046685126 203825930
|
||||
32926673 475089348 115925678
|
||||
3091489330 2135314279 421022178
|
||||
4225669654 1871071039 45515305
|
||||
4018596644 1533302854 72032816
|
||||
2105871498 1332898874 176621643
|
||||
398604391 732156704 170521927
|
||||
2475375726 4289723131 5244165
|
||||
4271184959 1509520517 23782337
|
||||
2480619891 2698929267 337643768
|
||||
3911645962 1605335670 55526138
|
||||
3759208316 3590563274 122757802
|
||||
3058110291 3082246426 33379039
|
||||
1404278872 1916586344 83687741
|
||||
15821415 1279766340 17105258
|
||||
0 902678631 15821415
|
||||
148852351 943795712 2404717
|
||||
2966942025 2607761001 91168266
|
||||
1308993408 3036573035 45673391
|
||||
1000694273 591015026 141141678
|
||||
756740815 0 40127528
|
||||
3881966118 1308993408 23905466
|
||||
1171091235 918500046 25295666
|
||||
1539650216 3960017884 35526986
|
||||
1354666799 4047228473 49612073
|
||||
3905871584 1865296661 5774378
|
||||
3967172100 2556336457 51424544
|
||||
2050115011 1809540174 55756487
|
||||
1487966613 3995544870 51683603
|
||||
2818263659 1660861808 148678366
|
||||
569126318 287474851 187614497
|
||||
2282493141 4096840546 192882585
|
||||
1196386901 946200429 100484697
|
||||
|
||||
humidity-to-location map:
|
||||
1176010827 433228953 11431675
|
||||
3343304446 3212529205 243926878
|
||||
3019400776 2809960450 121184456
|
||||
598353273 717816068 6343029
|
||||
2345678711 3761800910 44017519
|
||||
2389696230 4104193148 30873153
|
||||
334421220 1167819222 181559786
|
||||
2083857214 2364759797 169609191
|
||||
169788889 1665299576 164632331
|
||||
1547379326 837819247 83916771
|
||||
636928438 1349379008 3985237
|
||||
1997878235 4242793968 52173328
|
||||
4036700740 3456456083 70103553
|
||||
1701551461 724159097 55714896
|
||||
2050051563 4072991568 31201580
|
||||
1495464492 0 51914834
|
||||
1771229473 779873993 57945254
|
||||
1757266357 444660628 13963116
|
||||
640913675 1829931907 79566117
|
||||
604696302 1135587086 32232136
|
||||
2790976533 2058196402 571198
|
||||
0 183116767 169788889
|
||||
581107897 458623744 17245376
|
||||
1032415123 921736018 143595704
|
||||
2880695042 3109061793 3719369
|
||||
3596511671 3547168702 214632208
|
||||
1989658157 4064771490 8220078
|
||||
2791547731 1989658157 68538245
|
||||
1631296097 1065331722 70255364
|
||||
2253466405 2606294055 92212306
|
||||
2591363195 3865158152 199613338
|
||||
2532023472 3805818429 59339723
|
||||
1253517544 475869120 241946948
|
||||
3811143879 2058767600 225556861
|
||||
3334473574 2931144906 8830872
|
||||
720479792 1353364245 311935331
|
||||
2420569383 2698506361 111454089
|
||||
2081253143 2603689984 2604071
|
||||
3140585232 2939975778 159805668
|
||||
2860085976 3526559636 20609066
|
||||
1829174727 352905656 80323297
|
||||
515981006 117989876 65126891
|
||||
3587231324 3099781446 9280347
|
||||
1187442502 51914834 66075042
|
||||
2953735407 3112781162 65665369
|
||||
4187239629 4135066301 107727667
|
||||
3300390900 3178446531 34082674
|
||||
2884414411 2534368988 69320996
|
||||
4106804293 2284324461 80435336
|
||||
150
2023/day5/src/main.rs
Normal file
150
2023/day5/src/main.rs
Normal file
@@ -0,0 +1,150 @@
|
||||
use std::fs;
|
||||
|
||||
#[macro_use(c)]
|
||||
extern crate cute;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Conversion {
|
||||
to: String,
|
||||
conv: Vec<(i64, i64, i64)>, //mod, start, end
|
||||
}
|
||||
|
||||
impl Conversion {
|
||||
fn New(to: String, conv: Vec<(i64, i64, i64)>) -> Conversion {
|
||||
Conversion {
|
||||
to: to,
|
||||
conv: conv
|
||||
.iter()
|
||||
.map(|x| ((x.0 as i64 - x.1 as i64) as i64, x.1, x.1 + x.2))
|
||||
.collect::<Vec<(i64, i64, i64)>>(),
|
||||
}
|
||||
}
|
||||
fn convert(mut self, seed: i64) -> i64{
|
||||
for con in self.conv{
|
||||
if (con.1..con.2).contains(&seed){
|
||||
return seed+con.0
|
||||
}
|
||||
}
|
||||
return seed;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let content = fs::read_to_string("input").expect("Could not read the file");
|
||||
|
||||
let part1 = part1(content.clone());
|
||||
println!("{}", part1);
|
||||
|
||||
let part2 = part2(content.clone());
|
||||
println!("{}", part2);
|
||||
return;
|
||||
}
|
||||
|
||||
fn part1(content: String) -> i64 {
|
||||
let categories_lst = content.split("\n\n").collect::<Vec<_>>();
|
||||
let categories = c! {
|
||||
cat.split("-").collect::<Vec<_>>()[0] =>
|
||||
Conversion::New(
|
||||
cat.split("-").collect::<Vec<_>>()[2].split(" ").collect::<Vec<_>>()[0].to_string(),
|
||||
c![
|
||||
(line.split(" ").collect::<Vec<_>>()[0].parse::<i64>().unwrap(),
|
||||
line.split(" ").collect::<Vec<_>>()[1].parse::<i64>().unwrap(),
|
||||
line.split(" ").collect::<Vec<_>>()[2].parse::<i64>().unwrap()),
|
||||
for line in cat.split("\n").collect::<Vec<_>>()[1..].to_vec()
|
||||
])
|
||||
,for cat in categories_lst[1..].to_vec()
|
||||
};
|
||||
let mut seeds = categories_lst
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.split(":")
|
||||
.collect::<Vec<_>>()[1]
|
||||
.split(" ")
|
||||
.into_iter()
|
||||
.filter(|x| !x.is_empty())
|
||||
.collect::<Vec<_>>()
|
||||
.iter()
|
||||
.map(|x| x.parse::<i64>().unwrap())
|
||||
.collect::<Vec<_>>();
|
||||
let mut cat = "seed";
|
||||
while cat != "location" {
|
||||
seeds = c![categories.get(cat).unwrap().clone().convert(x), for x in seeds];
|
||||
cat = categories.get(cat).unwrap().to.as_str();
|
||||
}
|
||||
return *seeds.iter().min().unwrap();
|
||||
}
|
||||
|
||||
fn part2(content: String) -> i64 {
|
||||
let categories_lst = content.split("\n\n").collect::<Vec<_>>();
|
||||
let seeds = categories_lst
|
||||
.get(0)
|
||||
.unwrap()
|
||||
.split(":")
|
||||
.collect::<Vec<_>>()[1]
|
||||
.split(" ")
|
||||
.into_iter()
|
||||
.filter(|x| !x.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
let mut intervals: Vec<(i64, i64, &str)> = c![(
|
||||
x[0].parse::<i64>().unwrap(),
|
||||
x[0].parse::<i64>().unwrap() + x[1].parse::<i64>().unwrap(),
|
||||
"seed"),
|
||||
for x in seeds.chunks(2).collect::<Vec<_>>()];
|
||||
|
||||
let categories = c! {
|
||||
cat.split("-").collect::<Vec<_>>()[0] =>
|
||||
Conversion::New(
|
||||
cat.split("-").collect::<Vec<_>>()[2].split(" ").collect::<Vec<_>>()[0].to_string(),
|
||||
c![
|
||||
(line.split(" ").collect::<Vec<_>>()[0].parse::<i64>().unwrap(),
|
||||
line.split(" ").collect::<Vec<_>>()[1].parse::<i64>().unwrap(),
|
||||
line.split(" ").collect::<Vec<_>>()[2].parse::<i64>().unwrap()),
|
||||
for line in cat.split("\n").collect::<Vec<_>>()[1..].to_vec()
|
||||
])
|
||||
,for cat in categories_lst[1..].to_vec()
|
||||
};
|
||||
let mut loc_lst = Vec::new();
|
||||
while let Some(top) = intervals.pop() {
|
||||
let mut x1 = top.0;
|
||||
let mut x2 = top.1;
|
||||
let cat = top.2;
|
||||
if cat == "location" {
|
||||
loc_lst.push(x1);
|
||||
continue;
|
||||
}
|
||||
let current_cat = categories.get(cat).unwrap();
|
||||
let to_cat = current_cat.to.as_str();
|
||||
let mut converted = false;
|
||||
for convers in ¤t_cat.conv {
|
||||
let diff = convers.0;
|
||||
let y1 = convers.1;
|
||||
let y2 = convers.2;
|
||||
|
||||
// if no overlapping -> x starts after y ends or other way around
|
||||
if x1 >= y2 || y1 >= x2 {
|
||||
continue;
|
||||
}
|
||||
|
||||
// partial overlap, x starts outside, ends inside -> push x.start to y.start into intervals, update x.start=y.start
|
||||
if x1 < y1 {
|
||||
intervals.push((x1, y1, cat));
|
||||
x1 = y1;
|
||||
}
|
||||
|
||||
// partial overlap, x starts inside, ends outside -> push y.start to x.end into intervals, update x.end=y.end
|
||||
if y2 < x2 {
|
||||
intervals.push((y2, x2, cat));
|
||||
x2 = y2;
|
||||
}
|
||||
|
||||
// perfect match remains, convert
|
||||
intervals.push((x1 + diff, x2 + diff, to_cat));
|
||||
converted = true;
|
||||
}
|
||||
if !converted {
|
||||
intervals.push((x1, x2, to_cat));
|
||||
}
|
||||
}
|
||||
|
||||
return *loc_lst.iter().min().unwrap();
|
||||
}
|
||||
Reference in New Issue
Block a user