Skip to content

Commit fb55d76

Browse files
committed
2023 Day 5.
1 parent e1bd4a6 commit fb55d76

File tree

9 files changed

+390
-0
lines changed

9 files changed

+390
-0
lines changed

2023/05/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "y2023d05"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
parse-display.workspace = true
8+
parse-display-with.workspace = true

2023/05/data/example1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
seeds: 79 14 55 13
2+
3+
seed-to-soil map:
4+
50 98 2
5+
52 50 48
6+
7+
soil-to-fertilizer map:
8+
0 15 37
9+
37 52 2
10+
39 0 15
11+
12+
fertilizer-to-water map:
13+
49 53 8
14+
0 11 42
15+
42 0 7
16+
57 7 4
17+
18+
water-to-light map:
19+
88 18 7
20+
18 25 70
21+
22+
light-to-temperature map:
23+
45 77 23
24+
81 45 19
25+
68 64 13
26+
27+
temperature-to-humidity map:
28+
0 69 1
29+
1 0 69
30+
31+
humidity-to-location map:
32+
60 56 37
33+
56 93 4

2023/05/data/input

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
seeds: 1636419363 608824189 3409451394 227471750 12950548 91466703 1003260108 224873703 440703838 191248477 634347552 275264505 3673953799 67839674 2442763622 237071609 3766524590 426344831 1433781343 153722422
2+
3+
seed-to-soil map:
4+
2067746708 2321931404 124423068
5+
2774831547 3357841131 95865403
6+
3776553292 3323317283 34523848
7+
4167907733 3453706534 116376261
8+
1190847573 767701596 554806188
9+
2870696950 1975607604 173919437
10+
1980384731 2612856575 87361977
11+
3380570559 2987564153 335753130
12+
3044616387 2451131599 21188806
13+
3909556885 2167390152 154541252
14+
3811077140 2149527041 17863111
15+
4077167815 3804196813 90739918
16+
2528751611 4222771775 72195521
17+
4064098137 3894936731 13069678
18+
4284283994 2700218552 10683302
19+
2468832075 2472320405 59919536
20+
3716323689 3570082795 60229603
21+
1085396685 662250708 105450888
22+
1030174777 1322507784 22912174
23+
1975607604 2446354472 4777127
24+
3828940251 2532239941 80616634
25+
584992388 1930412346 7315040
26+
592307428 0 437867349
27+
1745653761 437867349 224383359
28+
0 1345419958 584992388
29+
2192169776 2710901854 276662299
30+
2600947132 3630312398 173884415
31+
1053086951 1937727386 32309734
32+
3065805193 3908006409 314765366
33+
34+
soil-to-fertilizer map:
35+
4148533839 3658735071 146433457
36+
656556737 471425735 68165409
37+
751630557 539591144 462446129
38+
724722146 1002037273 26908411
39+
0 24799538 226917727
40+
4049709448 3143711443 98824391
41+
412048729 251717265 219708470
42+
3321838617 2645077606 72145759
43+
3869354568 2464722726 180354880
44+
2044900648 1380852911 42721698
45+
226917727 1028945684 185131002
46+
3111204505 2717223365 210634112
47+
2129227343 1423574609 981977162
48+
1380852911 2927857477 215853966
49+
3393984376 2405551771 59170955
50+
3453155331 3242535834 416199237
51+
1596706877 3805168528 448193771
52+
2087622346 4253362299 41604997
53+
631757199 0 24799538
54+
55+
fertilizer-to-water map:
56+
2679101382 2898500255 208361454
57+
3672966601 3700867560 107718031
58+
1764241275 4242224976 41519499
59+
1018987051 346629037 49456831
60+
518817716 396085868 3722249
61+
1224466235 306138732 40490305
62+
1068679130 947256497 122607155
63+
1328820253 625833852 156797976
64+
222363356 782631828 128136603
65+
4106081288 2497628211 144953761
66+
2887462836 1577782207 70654427
67+
2453719866 2077230479 225381516
68+
2177283137 3415053348 9638645
69+
1485618229 910768431 36488066
70+
1805760774 2833622399 32168430
71+
482104460 0 36713256
72+
4065926174 2480721173 16907038
73+
118194361 213050410 93088322
74+
1869091498 3106861709 308191639
75+
1068443882 399808117 235248
76+
2958117263 3926610020 315614956
77+
1577782207 2324396806 156324367
78+
1270677460 36713256 58142793
79+
1264956540 586932982 5720920
80+
1847306687 2302611995 21784811
81+
843178107 411124038 175808944
82+
1191286285 592653902 33179950
83+
4251035049 4283744475 11222821
84+
4262257870 2865790829 32709426
85+
4082833212 2810374323 23248076
86+
3273732219 2642581972 167792351
87+
350499959 1069863652 131604501
88+
3780684632 1859496740 217733739
89+
3441524570 3808585591 118024429
90+
3998418371 1791988937 67507803
91+
1837929204 3691490077 9377483
92+
1734106574 1761854236 30134701
93+
211282683 400043365 11080673
94+
2186921782 3424691993 266798084
95+
522539965 1201468153 320638142
96+
3559548999 1648436634 113417602
97+
0 94856049 118194361
98+
99+
water-to-light map:
100+
487890089 1253174910 48217379
101+
1162866447 2295971038 331509140
102+
3115016077 4085918002 209049294
103+
3600618057 2743705059 694349239
104+
3021490874 3712826169 26810261
105+
2743705059 3739636430 3013944
106+
3048301135 4019203060 66714942
107+
1494375587 0 650888870
108+
167398115 650888870 320491974
109+
78943404 2207516327 88454711
110+
3324065371 3742650374 276552686
111+
881072381 1174877356 78297554
112+
2690077973 2191170718 16345609
113+
2746719003 3438054298 274771871
114+
2145264457 1301392289 544813516
115+
959369935 971380844 203496512
116+
536107468 1846205805 344964913
117+
0 2627480178 78943404
118+
119+
light-to-temperature map:
120+
2934276762 3692860946 134937994
121+
2222730788 3468116804 32924074
122+
2030910720 3501040878 191820068
123+
2876227610 3450265581 17851223
124+
2821863146 1926340324 54364464
125+
2894078833 3827798940 40197929
126+
0 2499885250 950380331
127+
3069214756 1312743837 613596487
128+
950380331 232213448 1080530389
129+
2302682684 2382183979 117701271
130+
3682811243 47027822 185185626
131+
2255654862 0 47027822
132+
2420383955 1980704788 401479191
133+
134+
temperature-to-humidity map:
135+
3474899002 2152529659 335631613
136+
1227362297 2657517973 1047434675
137+
1147289328 4214894327 80072969
138+
3069802422 3704952648 405096580
139+
4233143053 2090705416 61824243
140+
3979887316 1837449679 253255737
141+
3810530615 2488161272 169356701
142+
1042444229 4110049228 104845099
143+
2274796972 1042444229 718141444
144+
2992938416 1760585673 76864006
145+
146+
humidity-to-location map:
147+
2905941546 1669212802 106379169
148+
3490393041 2571512629 24111360
149+
3327134512 896350741 163258529
150+
163044169 321738120 136537257
151+
1794114599 1475899779 31051829
152+
1155727752 771777629 98456450
153+
3514504401 1890601528 199093442
154+
3241757362 1290456090 8146812
155+
3713597843 2595623989 446677438
156+
2865335819 1388663285 15697510
157+
3155553665 2166925308 86203697
158+
26879537 567441866 52236777
159+
421556320 59735378 198122323
160+
4160275281 1059609270 134692015
161+
1072793086 2488577963 82934666
162+
2006477848 3530988938 680918581
163+
1766297705 1528152733 5360386
164+
1405780686 3214887893 74444149
165+
3017788788 1533513119 135699683
166+
977736160 4211907519 83059777
167+
1771658091 2466121455 22456508
168+
2687396429 870234079 26116662
169+
79116314 547394430 20047436
170+
2773054926 1506951608 21201125
171+
1825166428 3289332042 181311420
172+
2713513091 1404360795 59541835
173+
3012320715 1213908249 5468073
174+
3249904174 2089694970 77230338
175+
2794256051 1219376322 71079768
176+
1601513875 2253129005 74723447
177+
3153488471 769712435 2065194
178+
299581426 41300579 18434799
179+
332437267 458275377 89119053
180+
1273791166 2327852452 131989520
181+
1676237322 1298602902 90060383
182+
318016225 26879537 14421042
183+
769712435 3470643462 60345476
184+
2881033329 3042301427 24908217
185+
1480224835 2459841972 6279483
186+
99163750 257857701 63880419
187+
1060795937 1463902630 11997149
188+
830057911 3067209644 147678249
189+
1254184202 1194301285 19606964
190+
1486504318 1775591971 115009557

2023/05/src/data.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#[cfg(test)]
2+
pub const EXAMPLE1: &'static str = include_str!("../data/example1");
3+
4+
#[allow(unused)]
5+
pub const INPUT: &'static str = include_str!("../data/input");

2023/05/src/main.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
mod data;
2+
mod part1;
3+
mod part2;
4+
mod puzzle;
5+
6+
fn main() {
7+
use data::INPUT;
8+
println!("Part 1: {}", part1::run(INPUT));
9+
println!("Part 2: {}", part2::run(INPUT));
10+
}

2023/05/src/part1/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::puzzle::Puzzle;
2+
3+
pub fn run(input: &str) -> u64 {
4+
let puzzle: Puzzle = input.parse().expect("parse failed");
5+
let intervals: Vec<[u64; 2]> = puzzle.seeds.iter().map(|&source| [source, 1]).collect();
6+
puzzle.lookup_min(intervals)
7+
}
8+
9+
#[cfg(test)]
10+
mod test {
11+
use super::*;
12+
use crate::data::EXAMPLE1;
13+
14+
#[test]
15+
fn test1() {
16+
assert_eq!(run(EXAMPLE1), 35);
17+
}
18+
}

2023/05/src/part2/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use crate::puzzle::Puzzle;
2+
3+
pub fn run(input: &str) -> u64 {
4+
let puzzle: Puzzle = input.parse().expect("parse failed");
5+
let (intervals, _) = puzzle.seeds.as_chunks();
6+
puzzle.lookup_min(intervals.into_iter().cloned())
7+
}
8+
9+
#[cfg(test)]
10+
mod test {
11+
use super::*;
12+
use crate::data::EXAMPLE1;
13+
14+
#[test]
15+
fn test1() {
16+
assert_eq!(run(EXAMPLE1), 46);
17+
}
18+
}

2023/05/src/puzzle.rs

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use parse_display::{Display, FromStr};
2+
use parse_display_with::formats::delimiter;
3+
4+
#[derive(Clone, Debug, FromStr)]
5+
#[display("seeds: {seeds}\n\n{maps}")]
6+
pub struct Puzzle {
7+
#[display(with=delimiter(" "))]
8+
pub seeds: Vec<u64>,
9+
#[display(with=delimiter("\n\n"))]
10+
pub maps: Vec<Map>,
11+
}
12+
13+
impl Puzzle {
14+
pub fn lookup_min(&self, intervals: impl IntoIterator<Item = [u64; 2]>) -> u64 {
15+
intervals
16+
.into_iter()
17+
.map(|[source, len]| Map::lookup_min(&self.maps, source, len))
18+
.min()
19+
.unwrap()
20+
}
21+
}
22+
23+
#[derive(Clone, Debug, Display, FromStr)]
24+
#[display("{label} map:\n{entries}")]
25+
#[from_str(new=Map::new(label, entries))]
26+
pub struct Map {
27+
#[allow(unused)]
28+
pub label: Label,
29+
#[display(with=delimiter("\n"))]
30+
pub entries: Vec<MapEntry>,
31+
}
32+
33+
impl Map {
34+
fn new(label: Label, mut entries: Vec<MapEntry>) -> Self {
35+
entries.sort_by_key(|entry| entry.source);
36+
Self { label, entries }
37+
}
38+
39+
fn lookup_interval(&self, source: u64, len: u64) -> (u64, u64) {
40+
let idx = self
41+
.entries
42+
.partition_point(|entry| entry.source + entry.len <= source);
43+
self.entries.get(idx).map_or_else(
44+
|| (source, len),
45+
|entry| {
46+
if entry.source <= source {
47+
let offset = source - entry.source;
48+
(entry.dest + offset, len.min(entry.len - offset))
49+
} else {
50+
(source, len.min(entry.source - source))
51+
}
52+
},
53+
)
54+
}
55+
56+
fn lookup_min(maps: &[Self], mut source: u64, mut len: u64) -> u64 {
57+
match maps {
58+
[] => source,
59+
[map, rest @ ..] => std::iter::from_fn(move || {
60+
if len > 0 {
61+
let interval = map.lookup_interval(source, len);
62+
source += interval.1;
63+
len -= interval.1;
64+
Some(Self::lookup_min(rest, interval.0, interval.1))
65+
} else {
66+
None
67+
}
68+
})
69+
.min()
70+
.unwrap(),
71+
}
72+
}
73+
}
74+
75+
#[derive(Clone, Debug, Display, FromStr, PartialEq, Eq, PartialOrd, Ord, Hash)]
76+
#[display("{source}-to-{dest}")]
77+
pub struct Label {
78+
pub source: String,
79+
pub dest: String,
80+
}
81+
82+
#[derive(Clone, Copy, Debug, Display, FromStr)]
83+
#[display("{dest} {source} {len}")]
84+
pub struct MapEntry {
85+
pub dest: u64,
86+
pub source: u64,
87+
pub len: u64,
88+
}
89+
90+
#[cfg(test)]
91+
mod test {
92+
use super::*;
93+
use crate::data::EXAMPLE1;
94+
95+
#[test]
96+
fn test1() -> Result<(), parse_display::ParseError> {
97+
EXAMPLE1.parse::<Puzzle>()?;
98+
Ok(())
99+
}
100+
}

0 commit comments

Comments
 (0)