heh
d6p2
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/inp.txt | 251 | ||||
| -rw-r--r-- | src/main.rs | 97 | ||||
| -rw-r--r-- | src/util.rs | 158 |
4 files changed, 171 insertions, 336 deletions
@@ -7,7 +7,6 @@ edition = "2021" [dependencies] itertools = "0.12.0" -rayon = "1.8.0" [profile.release] lto = true codegen-units = 1 diff --git a/src/inp.txt b/src/inp.txt index d612f11..1404bf2 100644 --- a/src/inp.txt +++ b/src/inp.txt @@ -1,249 +1,2 @@ -seeds: 763445965 78570222 1693788857 146680070 1157620425 535920936 3187993807 180072493 1047354752 20193861 2130924847 274042257 20816377 596708258 950268560 11451287 3503767450 182465951 3760349291 265669041 - -seed-to-soil map: -0 1894195346 315486903 -1184603419 2977305241 40929361 -1225532780 597717 4698739 -1988113706 1603988885 78481073 -679195301 529385087 505408118 -1781158512 2285166785 39457705 -352613463 2324624490 326581838 -1820616217 1738931330 104130014 -2066594779 2671974456 78036460 -1288754536 1682469958 56461372 -1371340411 3442489267 409818101 -3341036988 1092718505 511270380 -315486903 1857068786 37126560 -1924746231 2209682249 49360033 -1345215908 2259042282 26124503 -2917167497 2651206328 20768128 -1230231519 1034793205 57925300 -2144631239 3421335965 21153302 -2689873172 2750010916 227294325 -1974106264 1843061344 14007442 -2165784541 5296456 524088631 -1288156819 0 597717 -2937935625 3018234602 403101363 - -soil-to-fertilizer map: -3849355959 3101885274 170573538 -3006688391 3025445580 66663107 -1656285214 0 92188755 -2361256790 4033128077 180329908 -2073349245 3676064680 287907545 -3458323699 4213457985 81509311 -2607585697 2380345050 99923790 -2541586698 3332501375 61727497 -3789313396 3272458812 60042563 -1858319884 917612384 162600389 -404885474 510011966 407600418 -4019929497 3603127472 72937208 -812485892 1190058688 253881561 -2603314195 2073349245 4271502 -1469824760 92188755 186460454 -0 278649209 231362757 -1249323571 1443940249 220501189 -4285190709 3092108687 9776587 -1066367453 1817839802 182956118 -3257369783 2724171240 184379320 -2707509487 2480268840 111317034 -2818826521 2192483180 187861870 -1748473969 1080212773 109845915 -3441749103 3394228872 16574596 -3142507350 2077620747 114862433 -3539833010 2908550560 116895020 -231362757 1664441438 153398364 -384761121 2000795920 20124353 -3656728030 2591585874 132585366 -3073351498 3963972225 69155852 -4092866705 3410803468 192324004 - -fertilizer-to-water map: -2289944373 962149121 35670423 -3187295333 3295892606 34862460 -3798694276 3187295333 48036047 -3633796172 3644673845 164898104 -3222157793 3909959418 311250910 -922447685 2248438960 142418952 -648510907 2390857912 2187794 -3533408703 3809571949 100387469 -650698701 1840321280 271748984 -464371310 553340958 30176545 -3907291549 3330755066 313918779 -2791296727 461310335 25110510 -397451197 486420845 66920113 -3846730323 3235331380 60561226 -2575399801 2393045706 96151163 -1064866637 583517503 106955439 -1308190772 822897256 139251865 -0 236670371 224639964 -2671550964 0 119745763 -1171822076 2112070264 136368696 -224639964 690472942 132424314 -494547855 2489196869 153963052 -2498862112 160132682 76537689 -1447442637 997819544 842501736 -357064278 119745763 40386919 -2325614796 2643159921 173247316 - -water-to-light map: -3181220930 3836164803 159006567 -1405924027 2473049234 39930303 -1275636734 1411161599 10830099 -3831476263 1580605379 266491658 -1570444205 1294782382 116379217 -3133505036 2271845049 47715894 -1305612481 2512979537 100311546 -1286466833 1275636734 19145648 -167024508 145118817 64336111 -92584517 615923894 40913901 -2227655152 2098041405 130620812 -2181794752 2613291083 34326669 -433451288 209454928 64553504 -4223684323 2228662217 43182832 -3527642272 3532330812 303833991 -2855274869 2647617752 119788915 -4266867155 2319560943 28100141 -231360619 1167459710 18705935 -2136463524 3995171370 45331228 -2978600831 1421991698 154904205 -4097967921 2825698355 78160170 -0 489813287 92584517 -1686823422 3023927616 328136942 -2014960364 2903858525 116532044 -1512152517 2767406667 58291688 -395185371 1129193793 38265917 -498004792 274008432 215804855 -2975063784 3020390569 3537047 -3340227497 3352064558 180266254 -3524203227 4040502598 3439045 -713809647 656837795 472355998 -1445854330 2395217316 66298187 -3520493751 1576895903 3709476 -4176128091 2347661084 47556232 -2216121421 2461515503 11533731 -2604330501 1847097037 250944368 -2358275964 4043941643 246054537 -133498418 582397804 33526090 -2131492408 4289996180 4971116 -250066554 0 145118817 - -light-to-temperature map: -1212130960 1395832798 58636484 -542019293 812004214 195851408 -1706861165 2688952641 3198757 -3097592274 3018951608 116723898 -1148165381 3135675506 2936711 -938881022 300892136 26815610 -2884354982 3318488806 103553821 -789335095 421331130 149545927 -3270968199 1036156523 69989778 -1142047574 1030038716 6117807 -1710059922 1367448764 28384034 -3961474833 1257923390 109525374 -300892136 570877057 241127157 -2564647329 3422042627 200129932 -3534803741 2001828127 16273450 -1738443956 1454469282 486329977 -3717055162 2018101577 244419671 -4071000207 2781125469 223967089 -1030038716 3005092558 13859050 -737870701 358179343 20992797 -3388483069 4209077325 84901810 -3551077191 3622172559 62988195 -3614065386 2769635501 11489968 -3215304333 3262824940 55663866 -2278270544 2395185346 286376785 -1151102092 1940799259 61028868 -3625555354 4044566844 91499808 -2987908803 1106146301 109683471 -1134657064 2681562131 7390510 -2236365744 2353280546 41904800 -1270767444 3685160754 311880998 -1043897766 2262521248 90759298 -2806870879 2692151398 77484103 -3340957977 3997041752 47525092 -3214316172 4293979135 988161 -2764777261 1215829772 42093618 -965696632 379172140 42158990 -3473384879 4147658463 61418862 -2224773933 4136066652 11591811 -1582648442 3201806221 61018719 -758863498 327707746 30471597 -1643667161 3138612217 63194004 - -temperature-to-humidity map: -3893671309 2897869925 193525827 -1232495182 999263760 191357681 -2440090529 1477966061 163000786 -3554817869 2745268041 3258240 -2845194314 3091395752 30017739 -4087197136 1397536284 33207741 -131252236 827398049 11627380 -2183328852 2476037434 27127608 -2603091315 2503165042 242102999 -2875212053 1778930312 348370034 -1427151141 891672290 77843721 -1642958327 3928570372 200739395 -3610489285 3219006125 283182024 -3277129360 3580618829 128344865 -2233175686 1190621441 206914843 -891672290 4129309767 165657529 -0 205218291 35419402 -3584280491 3551111757 26208794 -4247745260 1430744025 47222036 -2210456460 3502188149 21171921 -1843697722 2136406304 209010485 -1423852863 3577320551 3298278 -348097907 336470527 490927522 -1057329819 3708963694 175165363 -2231628381 3549564452 1547305 -3268023402 2127300346 9105958 -142879616 0 205218291 -3558076109 3523360070 26204382 -2052708207 2345416789 130620645 -4120404877 969516011 29747749 -35419402 240637693 95832834 -4150152626 3121413491 97592634 -3223582087 3884129057 44441315 -1504994862 1640966847 137963465 -3405474225 2748526281 149343644 - -humidity-to-location map: -3554305993 3441523152 94289982 -4052786034 3015714705 242181262 -491965155 895492593 72049890 -2437667299 3726944956 38441480 -0 387558480 141314689 -3811971022 3765386436 101972362 -3648595975 3535813134 51583790 -1318420824 1500487572 135197760 -230758172 30039204 163596268 -3097413876 4221249785 73717511 -3041068351 3257895967 56345525 -1454254795 641271247 181430537 -394354440 1139828795 97610715 -3215016925 2618366793 339289068 -827848339 193635472 113349295 -150184459 306984767 80573713 -1778801395 2099048921 519317872 -1176891800 1379491771 111489820 -1288381620 0 30039204 -3171131387 3891749067 43885538 -564015045 528873169 112398078 -3913943384 3935634605 11560990 -2767014161 3947195595 274054190 -941197634 822701784 72790809 -1453618584 1499851361 636211 -2298119267 3587396924 139548032 -3700179765 3867358798 24390269 -3742730647 1778801395 11181531 -1013988443 967542483 20851096 -141314689 1490981591 8869770 -3925504374 3314241492 127281660 -2476108779 2088802749 10246172 -676413123 988393579 151435216 -3753912178 2957655861 58058844 -2486354951 1789982926 280659210 -3724570034 2070642136 18160613 -1034839539 1237439510 142052261 +Time: 40 70 98 79 +Distance: 215 1051 2147 1005 diff --git a/src/main.rs b/src/main.rs index 52d3b33..5fa28ae 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,98 +1,23 @@ +#![allow(confusable_idents, uncommon_codepoints)] #![feature(array_windows, test, slice_as_chunks, array_chunks)] extern crate test; -pub mod prelude { - pub use itertools::izip; - pub use itertools::Itertools; - pub use std::{ - collections::{HashMap, HashSet, VecDeque}, - fmt::{Debug, Display}, - hint::black_box as boxd, - iter, - ops::Range, - }; -} -pub use prelude::*; -use rayon::iter::{ParallelBridge, ParallelIterator}; +mod util; +pub use util::prelude::*; const S: char = ' '; fn solve(i: &str) -> impl Display { let mut lines = i.lines(); - let seeds: Box<[u64]> = lines - .next() - .unwrap() - .split_once(':') - .unwrap() - .1 - .split_ascii_whitespace() - .map(|x| x.parse::<u64>().unwrap()) - .collect(); - lines.next().unwrap(); - macro_rules! section { - ($of:ident) => { - let mut $of = Vec::with_capacity(10); - for line in lines.by_ref().skip(1) { - if line == "" { - println!("finished section {}", stringify!($of)); - break; - } - $of.push( - line.split_ascii_whitespace() - .map(|x| x.parse::<u64>().unwrap()) - .collect_tuple() - .map(|(s, d, r)| (s..s + r, d..d + r)) - .unwrap(), - ); - } - }; - } - section!(seed2soil); - section!(soil2fertilizer); - section!(fertilizer2water); - section!(water2light); - section!(light2temperature); - section!(temperature2humidity); - section!(humidity2location); - fn index(v: &[(Range<u64>, Range<u64>)], get: u64) -> u64 { - for (a, b) in v.iter().cloned() { - for (a, b) in izip!(a, b) { - if b == get { - return a; - } - } + let time = lines.Δ().μ1(':').ͷ().fold(0, |acc, n| acc * 10 + n as u64); + let distance = lines.Δ().μ1(':').ͷ().fold(0, |acc, n| acc * 10 + n as u64); + let mut works = 0; + for held in 0..time { + let gone = held * (time - held); + if gone > distance { + works += 1; } - get } - seeds - .array_chunks::<2>() - .par_bridge() - .flat_map(|&[s, r]| s..s + r) - .map(|seed| { - // print!("seed {seed} "); - let soil = index(&seed2soil, seed); - // print!("soil {soil} "); - let fertilizer = index(&soil2fertilizer, soil); - // print!("fertilizer {fertilizer} "); - let water = index(&fertilizer2water, fertilizer); - // print!("water {water} "); - let light = index(&water2light, water); - // print!("light {light} "); - let temperature = index(&light2temperature, light); - // print!("temp {temperature} "); - let humidity = index(&temperature2humidity, temperature); - // print!("humidity {humidity} "); - let location = index(&humidity2location, humidity); - // println!("location {location}"); - location - }) - .min() - .unwrap() - - // lines - // .map(|x| { - // // - // }) - // .sum::<u64>() + works } fn main() { diff --git a/src/util.rs b/src/util.rs new file mode 100644 index 0000000..35fbc0a --- /dev/null +++ b/src/util.rs @@ -0,0 +1,158 @@ +#![allow(non_snake_case)] +use std::str::FromStr; + +pub mod prelude { + pub use super::{GreekTools, IterͶ, Ͷ, Α, Κ, Λ, Μ}; + pub use itertools::izip; + pub use itertools::Itertools; + pub use std::{ + collections::{HashMap, HashSet, VecDeque}, + fmt::{Debug, Display}, + hint::black_box as boxd, + iter, + ops::Range, + }; +} + +pub trait Λ<T> +where + T: FromStr, +{ + fn λ(&self) -> T + where + <T as FromStr>::Err: std::fmt::Display; +} + +impl<T: FromStr> Λ<T> for &str { + /// parse, unwrap + fn λ(&self) -> T + where + <T as FromStr>::Err: std::fmt::Display, + { + match self.parse() { + Ok(v) => v, + Err(e) => { + panic!( + "{e}: {self} should parse into {}", + std::any::type_name::<T>() + ); + } + } + } +} +pub trait Κ { + fn κ<T: FromStr>(self) -> impl Iterator<Item = T> + where + <T as FromStr>::Err: std::fmt::Display; +} + +impl Κ for &str { + fn κ<T: FromStr>(self) -> impl Iterator<Item = T> + where + <T as FromStr>::Err: std::fmt::Display, + { + self.split_ascii_whitespace().map(|x| x.λ()) + } +} + +pub trait Α<T> { + fn α(self) -> T; +} + +impl<T, E: std::fmt::Display> Α<T> for Result<T, E> { + #[track_caller] + fn α(self) -> T { + match self { + Ok(v) => v, + Err(e) => { + panic!("unwrap failed: {e}"); + } + } + } +} +impl<T> Α<T> for Option<T> { + #[track_caller] + fn α(self) -> T { + match self { + Some(v) => v, + None => panic!("nothingness!"), + } + } +} + +pub trait Ͷ { + fn ͷ(self) -> impl Iterator<Item = u8>; +} + +impl Ͷ for u64 { + fn ͷ(self) -> impl Iterator<Item = u8> { + let digits = (self.ilog10() + 1) as u8; + (0..digits) + .rev() + .map(move |n| ((self / 10u64.pow(u32::from(n))) % 10) as u8) + } +} + +pub trait Μ where + Self: Sized, +{ + fn μ(self, d: char) -> (Self, Self); + fn μκ<T: FromStr>(self, d: char) -> impl Iterator<Item = (T, T)> + where + <T as FromStr>::Err: std::fmt::Display; + fn μ1<T: FromStr>(self, d: char) -> impl Iterator<Item = T> + where + <T as FromStr>::Err: std::fmt::Display; + fn μ0<T: FromStr>(self, d: char) -> impl Iterator<Item = T> + where + <T as FromStr>::Err: std::fmt::Display; +} + +impl Μ for &str { + fn μ(self, d: char) -> (Self, Self) { + self.split_once(d) + .unwrap_or_else(|| panic!("{self} should split at {d} fine")) + } + + fn μκ<T: FromStr>(self, d: char) -> impl Iterator<Item = (T, T)> + where + <T as FromStr>::Err: std::fmt::Display, + { + let (α, β) = self.μ(d); + α.κ::<T>().zip(β.κ::<T>()) + } + + fn μ1<T: FromStr>(self, d: char) -> impl Iterator<Item = T> + where + <T as FromStr>::Err: std::fmt::Display, + { + self.μ(d).1.κ() + } + + fn μ0<T: FromStr>(self, d: char) -> impl Iterator<Item = T> + where + <T as FromStr>::Err: std::fmt::Display, + { + self.μ(d).0.κ() + } +} + +pub trait IterͶ { + fn ͷ(self) -> impl Iterator<Item = u8>; +} + +impl<I: Iterator<Item = u64>> IterͶ for I { + fn ͷ(self) -> impl Iterator<Item = u8> { + self.flat_map(Ͷ::ͷ) + } +} + +pub trait GreekTools<T> { + fn Δ(&mut self) -> T; +} + +impl<T, I: Iterator<Item = T>> GreekTools<T> for I { + fn Δ(&mut self) -> T { + self.next().α() + } +} |