Compare commits

...

4 commits

Author SHA1 Message Date
Xiretza 29537d25aa 2023 day5: add data 2023-12-05 19:40:53 +00:00
Xiretza b1e402180f 2023 day5/rust: add solution 2023-12-05 19:40:53 +00:00
Xiretza 61b478ca0e common: add more methods to SectionRange 2023-12-05 19:40:53 +00:00
Xiretza 90d2e1636a common/rust: add Grid type 2023-12-05 19:40:53 +00:00
9 changed files with 529 additions and 0 deletions

2
2023/data/day5.expected Normal file
View file

@ -0,0 +1,2 @@
214922730
148041808

250
2023/data/day5.input Normal file
View file

@ -0,0 +1,250 @@
seeds: 3640772818 104094365 1236480411 161072229 376099792 370219099 1590268366 273715765 3224333694 68979978 2070154278 189826014 3855332650 230434913 3033760782 82305885 837883389 177854788 2442602612 571881366
seed-to-soil map:
496269031 1203272644 52136246
548405277 496269031 457095898
1005501175 953364929 249907715
soil-to-fertilizer map:
217408321 2086205436 25053699
2604208456 1670861921 31003781
1631572552 0 258383552
129225554 3768288787 36192668
2421205388 2905533654 126666762
242462020 3399542287 357404885
866152503 3032200416 253960559
2039921781 2262442546 381283607
2635212237 2714844607 190689047
3613008578 1753855801 23976114
3636984692 1503365158 167496763
1340671861 2111259135 274956
1889956104 806620565 149965677
0 3286160975 113381312
2987089260 956586242 546778916
2547872150 3756947172 11341615
3846919647 3807789063 43277850
3533868176 258383552 79140402
165418222 1701865702 51990099
1120113062 586061766 220558799
2559213765 2217447855 44994691
3807789063 4255836712 39130584
3890197497 3851066913 404769799
1340946817 1777831915 42087923
2881175496 2111534091 105913764
113381312 2643726153 15844242
1383034740 337523954 248537812
2825901284 2659570395 55274212
599866905 1819919838 266285598
fertilizer-to-water map:
3950520280 1751042330 139651634
936578795 3912173308 42397072
3553681000 1722281506 28760824
697953317 651809140 90189394
3582441824 876081661 368078456
978975867 2358439651 252255693
1495879532 2678320518 199775133
1910380638 3308279888 122339216
3355092099 1561901004 91630618
3187667509 2033753243 70292073
2752202873 3816184128 41568037
648600286 479585511 49353031
3149600631 2356473769 1965882
1695654665 2629130810 49189708
201901143 385568770 94016741
536376004 741998534 3182157
2472303091 3954570380 279899782
539558161 257732262 15304877
3131165165 2610695344 18435466
3446722717 1890693964 106958283
2793770910 1653531622 40823934
9849113 65680232 192052030
2878736712 2104045316 252428453
1792836692 2878095651 117543946
2172585320 1244160117 273599019
2060645804 3884012463 28160845
3257959582 3719051611 97132517
408449515 745180691 127926489
876081661 4234470162 60497134
1231231560 3043631916 264647972
3151566513 1997652247 36100996
2446184339 3430619104 26118752
803816626 582518586 69290554
554863038 9849113 40157204
2146325022 3857752165 26260298
788142711 50006317 15673915
295917884 273037139 112531631
2834594844 1517759136 44141868
4090171914 3456737856 204795382
2088806649 3661533238 57518373
1744844373 2995639597 47992319
2032719854 1694355556 27925950
595020242 528938542 53580044
water-to-light map:
556810106 840812947 14926117
2598413684 2184905392 114045192
2130064037 1600958027 248227533
1271028210 1253957270 39538107
3521286912 4262821917 32145379
1930562940 1944404618 64932992
4252373354 3302720391 42593942
1109026743 279496091 162001467
725777554 2565853410 3969864
2712458876 1330352326 185715465
4039742261 3173278185 129442206
0 2064417497 120487895
299311037 855739064 257499069
729747418 1849185560 95219058
1373001379 767226476 66231296
571736223 688212171 79014305
1878676528 2890412515 51886412
2898174341 682278717 5933454
1781236499 441497558 97440029
3090089298 3673052565 50139248
1365646204 833457772 7355175
1738475707 2412172480 42760792
1995495932 2569823274 134568105
3193548680 3476912261 196140304
824966476 538937587 143341130
3553432291 3723191813 486309970
3140228546 4209501783 53320134
968307606 1113238133 140719137
4169184467 3090089298 83188887
650750528 204469065 75027026
1439232675 2704391379 186021136
2511835025 1516067791 84890236
1625253811 2298950584 113221896
3389688984 3345314333 131597928
2378291570 0 22623317
2596725261 2942298927 1688423
1310566317 2009337610 55079887
2940964744 201446459 3022606
120487895 22623317 178823142
2904107795 1293495377 36856949
2400914887 2454933272 110920138
light-to-temperature map:
1244459013 624435822 80444775
2608592263 3309263777 172991510
3165402867 2278806547 335097905
292819381 1643978777 105413752
704475267 462426854 15399493
3696584161 2678497330 345840247
2891254573 3613542439 34162874
1894523870 281665589 180761265
1706897891 1456352798 187625979
398233133 1877125477 198159658
4176101046 3482255287 104541624
2278806547 3662029939 329785716
3500500772 4098883907 196083389
4280642670 3647705313 14324626
4149492660 3024337577 11203522
85784517 0 207034864
2925417447 3231118601 78145176
0 1749392529 85784517
4160696182 3215713737 15404864
4042424408 3991815655 107068252
1129076520 207034864 74630725
596392791 1348270322 108082476
3100809989 2613904452 64592878
1326100451 1114082357 234187965
2864509045 3586796911 26745528
2781583773 3132788465 82925272
1560288416 477826347 146609475
3003562623 3035541099 97247366
719874760 704880597 409201760
1324903788 1875928814 1196663
1203707245 1835177046 40751768
temperature-to-humidity map:
2622049454 736812858 79169969
3979548277 2854489162 116161222
0 2175018874 84480806
567798788 2400631546 48501534
3627076350 2837901836 16587326
1459224370 1289368272 21397154
3018037189 3252694507 93925363
4095709499 4063232797 21404553
1675574530 2560175285 695067
650203851 328659590 327259736
3143763337 3475937023 49576
1591893253 2091337597 83681277
3655324442 3516768137 39649206
1125784995 2259499680 94582805
1082589333 280432563 43195662
1676269597 1011614859 3965286
3955996260 4039680780 23552017
2701219423 684725863 52086995
977463587 2387091631 13539915
3643663676 3346619870 11660766
3143812913 3556417343 483263437
2907111949 3358280636 110925240
2293944099 2615629615 132579811
991003502 920029028 91585831
1513630670 1015580145 78262583
196718861 1310765426 316320664
1680234883 1638774240 331394507
2011629390 815982827 1882146
2874635247 4084637350 32476702
3701704795 2998403042 254291465
2426523910 1093842728 195525544
2837901836 3475986599 8980753
3694973648 3469205876 6731147
3111962552 3484967352 31800785
616300322 662956301 21769562
1220367800 1627086090 10492300
1480621524 2354082485 33009146
2846882589 2970650384 27752658
1352028950 323628225 5031365
2753306418 655919326 7036975
513039525 2560870352 54759263
84480806 1637578390 1195850
2013511536 0 280432563
85676656 2449133080 111042205
1357060315 817864973 102164055
638069884 2748209426 12133967
1230860100 1970168747 121168850
humidity-to-location map:
3071447765 3790677895 35519893
501148922 1470714761 60946444
949413779 3960084356 1114317
2276139972 547813284 42132370
261623667 0 220957931
0 220957931 207965683
2629055810 2988733812 367963097
936813255 2507216386 12600524
3106967658 2402339659 33240399
909841910 3356696909 11084951
1030029700 920191219 341387512
2503236334 3572469232 125819476
1371417212 1666455982 273052538
2997018907 3367781860 25674024
3311094548 2519816910 468916902
2021404744 1531661205 83389724
3140208057 1939508520 59879232
259387264 428923614 2236403
950528096 1334878052 79501604
2446901229 1414379656 56335105
1644469750 3393455884 140294896
3022692931 3698288708 48754834
4042169428 1999387752 252797868
1805974329 3981490698 215430415
1784764646 501148922 21209683
889549885 3961198673 20292025
3780011450 522358605 10399946
2104794468 1261578731 73299321
3790411396 3842084182 118000174
2318272342 2252185620 113574154
4003450976 3533750780 38718452
2178093789 4196921113 98046183
3908411570 3747043542 43634353
562095366 2435580058 71636328
2431846496 532758551 15054733
920926861 3826197788 15886394
3952045923 1615050929 51405053
207965683 431160017 51421581
633731694 2365759774 36579885
670311579 700952913 219238306
3200087289 589945654 111007259

View file

@ -0,0 +1,9 @@
[package]
name = "rust_2023_05"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
aoc = { path = "../../../common/rust" }

163
2023/day5/rust/src/main.rs Normal file
View file

@ -0,0 +1,163 @@
#![warn(clippy::pedantic)]
use std::{io::stdin, ops::ControlFlow};
use aoc::{SectionRange, UpToTwo};
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
struct MapRange {
source: SectionRange<usize>,
dest_start: usize,
}
impl MapRange {
/// Returns overlapping (mapped) and non-overlapping (unmapped) ranges
pub fn map(
&self,
source_range: SectionRange<usize>,
) -> (Option<SectionRange<usize>>, UpToTwo<SectionRange<usize>>) {
if let Some(overlap) = self.source & source_range {
let non_overlapping = source_range - overlap;
let overlap_offset = overlap
.offset_by_neg(*self.source.start())
.offset_by(self.dest_start);
(Some(overlap_offset), non_overlapping)
} else {
(None, UpToTwo::One(source_range))
}
}
pub fn parse(s: &str) -> Self {
let &[dest_start, source_start, len] = s
.split(' ')
.map(|n| n.parse().unwrap())
.collect::<Vec<_>>()
.as_slice()
else {
panic!("Invalid input: {s}");
};
Self {
source: SectionRange::try_new(source_start, source_start + len - 1).unwrap(),
dest_start,
}
}
}
struct IdMapping {
ranges: Vec<MapRange>,
}
impl IdMapping {
pub fn new(ranges: Vec<MapRange>) -> Self {
for window in ranges.windows(2) {
let [first, second] = window else { panic!() };
assert!(
(first.source & second.source).is_none(),
"source ranges overlap"
);
}
Self { ranges }
}
pub fn map(&self, source_range: SectionRange<usize>) -> Vec<SectionRange<usize>> {
let result = self.ranges.iter().try_fold(
(vec![], vec![source_range]),
|(mut mapped, unmapped), range| {
if unmapped.is_empty() {
return ControlFlow::Break(mapped);
}
let mut remaining_unmapped = Vec::new();
for non_mapped in unmapped {
let (overlapping, non_overlapping) = range.map(non_mapped);
mapped.extend(overlapping);
remaining_unmapped.extend(non_overlapping);
}
ControlFlow::Continue((mapped, remaining_unmapped))
},
);
match result {
ControlFlow::Continue((mut mapped, unmapped)) => {
// unmapped ranges stay as-is
mapped.extend(unmapped);
mapped
}
ControlFlow::Break(mapped) => mapped,
}
}
}
fn map_ranges_once(
ranges: &[SectionRange<usize>],
mapping: &IdMapping,
) -> Vec<SectionRange<usize>> {
ranges
.iter()
.flat_map(|&range| mapping.map(range))
.collect()
}
fn map_ranges_completely(
init_ranges: Vec<SectionRange<usize>>,
mappings: &[IdMapping],
) -> Vec<SectionRange<usize>> {
mappings
.iter()
.fold(init_ranges, |ranges, map| map_ranges_once(&ranges, map))
}
fn minimum_result(init_ranges: Vec<SectionRange<usize>>, mappings: &[IdMapping]) -> usize {
*map_ranges_completely(init_ranges, mappings)
.iter()
.min()
.unwrap()
.start()
}
fn main() {
let mut lines = stdin().lines().map(Result::unwrap);
let seeds: Vec<usize> = lines
.next()
.unwrap()
.split(' ')
.skip(1)
.map(|n| n.parse().unwrap())
.collect();
let mut maps = Vec::new();
while let Some(line) = lines.next() {
if line.is_empty() {
lines.next();
continue;
}
let ranges = lines
.by_ref()
.take_while(|l| !l.is_empty())
.map(|l| MapRange::parse(&l))
.collect();
maps.push(IdMapping::new(ranges));
}
let single_seeds: Vec<_> = seeds
.iter()
.map(|&seed| SectionRange::try_new(seed, seed).unwrap())
.collect();
println!("{}", minimum_result(single_seeds, &maps));
let seed_ranges: Vec<_> = seeds
.chunks(2)
.map(|pair| {
let &[start, len] = pair else {
panic!("Not a pair: {pair:?}");
};
SectionRange::try_new(start, start + len - 1).unwrap()
})
.collect();
println!("{}", minimum_result(seed_ranges, &maps));
}

7
Cargo.lock generated
View file

@ -663,6 +663,13 @@ dependencies = [
"enum-map",
]
[[package]]
name = "rust_2023_05"
version = "0.1.0"
dependencies = [
"aoc",
]
[[package]]
name = "rustversion"
version = "1.0.11"

View file

@ -29,4 +29,5 @@ members = [
"2022/day18/rust",
"2023/day1/rust",
"2023/day2/rust",
"2023/day5/rust",
]

62
common/rust/src/grid.rs Normal file
View file

@ -0,0 +1,62 @@
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Error {
BadWidth { expected: usize, got: usize },
}
/// A two-dimensional grid of values.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Grid<T> {
width: usize,
height: usize,
rows: Vec<Vec<T>>,
}
impl<T> Grid<T> {
/// Creates a new grid with a given `width` and `height`. The data is given as a [`Vec`] of
/// rows, each row a [`Vec`] of values.
///
/// # Errors
///
/// Returns an error if the given data does not match up with the dimensions.
pub fn from_rows(rows: Vec<Vec<T>>) -> Result<Self, Error> {
let row_len = rows.get(0).map_or(0, Vec::len);
if let Some(err) = rows.iter().map(Vec::len).find_map(|l| {
(l != row_len).then_some(Error::BadWidth {
expected: row_len,
got: l,
})
}) {
return Err(err);
}
Ok(Self {
width: rows.get(0).map_or(0, std::vec::Vec::len),
height: rows.len(),
rows,
})
}
pub fn rows(&self) -> impl Iterator<Item = impl Iterator<Item = &T>> {
self.rows.iter().map(|row| row.iter())
}
pub fn cols(&self) -> impl Iterator<Item = impl Iterator<Item = &T>> {
(0..self.width).map(|i| self.rows.iter().map(move |r| &r[i]))
}
pub fn rows_mut(&mut self) -> impl Iterator<Item = impl Iterator<Item = &mut T>> {
self.rows.iter_mut().map(|row| row.iter_mut())
}
/*
pub fn cols_mut(&mut self) -> impl Iterator<Item = impl Iterator<Item = &mut T>> {
(0..self.width).map(move |i| {
self.rows.iter_mut().map(move |r| {
let i: usize = i;
let x: &mut T = &mut r[i];
x
})
})
}
*/
}

View file

@ -3,6 +3,7 @@
mod section_range;
pub mod vec2;
pub mod vecn;
pub mod grid;
use std::{mem, path::Path};

View file

@ -81,6 +81,40 @@ impl<T: Ord + Copy> SectionRange<T> {
};
intersection == *other
}
/// Returns the start of the range.
#[must_use]
pub fn start(&self) -> &T {
&self.start
}
/// Returns the end of the range.
#[must_use]
pub fn end(&self) -> &T {
&self.end
}
}
impl<T: Add<Output = T> + Clone> SectionRange<T> {
/// Offsets both the start and end of the range by the given offset.
#[must_use]
pub fn offset_by(self, offset: T) -> SectionRange<T> {
Self {
start: self.start + offset.clone(),
end: self.end + offset,
}
}
}
impl<T: Sub<Output = T> + Clone> SectionRange<T> {
/// Offsets both the start and end of the range by the given offset in the negative direction.
#[must_use]
pub fn offset_by_neg(self, offset: T) -> SectionRange<T> {
Self {
start: self.start - offset.clone(),
end: self.end - offset,
}
}
}
impl<T: Ord + Copy + Sub<Output = L>, L: TryInto<usize>> SectionRange<T> {