Skip to content

Commit 7663f4c

Browse files
committed
Day 5 part 1
1 parent 3518a8b commit 7663f4c

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed

2023/Day05/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import input from './input.js';
2+
3+
const almanac = input.split('\n\n').map(input => input.split('\n'));
4+
5+
// Extract seeds and mappings
6+
const seeds = almanac[0][0].split(':')[1].trim().split(' ').map(number => parseInt(number));
7+
const maps = almanac.slice(1).map(section => section.slice(1).map(item => item.split(' ').map(number => parseInt(number))));
8+
const locations = [];
9+
10+
// Function to find location from seed
11+
const findLocationFromSeed = (seed) => {
12+
let current = seed;
13+
for (const map of maps) {
14+
for (const [destinationStart, sourceStart, rangeLength] of map) {
15+
const maxSeed = sourceStart + rangeLength;
16+
const minSeed = sourceStart;
17+
18+
// console.log(`current: ${current}, min: ${minSeed}, max: ${maxSeed}`);
19+
if (current >= minSeed && current <= maxSeed) {
20+
current = destinationStart + (current - minSeed);
21+
break;
22+
}
23+
}
24+
}
25+
return current;
26+
}
27+
28+
for (const seed of seeds) {
29+
locations.push(findLocationFromSeed(seed));
30+
}
31+
32+
const min = Math.min(...locations);
33+
console.log(min);

2023/Day05/input.js

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
const input = `seeds: 364807853 408612163 302918330 20208251 1499552892 200291842 3284226943 16030044 2593569946 345762334 3692780593 17215731 1207118682 189983080 2231594291 72205975 3817565407 443061598 2313976854 203929368
2+
3+
seed-to-soil map:
4+
2069473506 3732587455 1483883
5+
3235691256 2348990120 6550341
6+
3547561069 1392195671 747406227
7+
3264251584 3734071338 283309485
8+
391285622 257757572 195552540
9+
1645243555 3166958320 377191689
10+
335002083 512210869 56283539
11+
3242241597 897735089 22009987
12+
77244511 0 257757572
13+
989159646 4172023334 122943962
14+
605476380 3544150009 188437446
15+
0 568494408 18343754
16+
2700122696 4050276683 121746651
17+
2022435244 2139601898 47038262
18+
2227672101 919745076 95840269
19+
1112103608 2633818373 533139947
20+
826809686 2186640160 162349960
21+
3100147259 762191092 135543997
22+
18343754 453310112 58900757
23+
2323512370 1015585345 282396986
24+
2605909356 1297982331 94213340
25+
2821869347 2355540461 278277912
26+
793913826 4017380823 32895860
27+
2070957389 605476380 156714712
28+
29+
soil-to-fertilizer map:
30+
2700214958 2743391193 363795571
31+
1484584575 1440072796 24660284
32+
927520818 435059068 191969051
33+
1588488926 1434420334 5652462
34+
1423277199 141187887 5443857
35+
1594141388 1350997453 83422881
36+
1986188257 3933008893 120750463
37+
1509244859 146631744 79093544
38+
3712482038 4220862006 74105290
39+
3948206286 1986188257 277570873
40+
291046304 281588807 153470261
41+
1119489869 918224946 303787330
42+
1677564269 1321192605 29804848
43+
2309878676 2336743687 390336282
44+
3079951473 3306332300 449116691
45+
444516565 1222012276 99180329
46+
543696894 1464733080 383823924
47+
3895169406 3771389935 53036880
48+
3529068164 4053759356 167102650
49+
0 627178642 291046304
50+
3696170814 2727079969 16311224
51+
3855550220 3824426815 39619186
52+
2106938720 3107186764 199145536
53+
1428721056 225725288 55863519
54+
1707369117 0 64378064
55+
1771747181 64378064 76809823
56+
3064010529 3755448991 15940944
57+
2306084256 2332949267 3794420
58+
4225777159 2263759130 69190137
59+
3786587328 3864046001 68962892
60+
1588338403 627028119 150523
61+
62+
fertilizer-to-water map:
63+
2299879115 39069388 7889905
64+
514481680 504392888 101474410
65+
3448524168 0 25428313
66+
13641075 1832356728 472401611
67+
0 25428313 13641075
68+
1842445520 108629584 395763304
69+
486042686 3445513487 28438994
70+
2307769020 2304758339 1140755148
71+
2238208824 46959293 61670291
72+
615956090 605867298 1226489430
73+
74+
water-to-light map:
75+
1318826171 2010420436 223477535
76+
2278894745 2233897971 671603259
77+
988189854 447584401 27746374
78+
2132052210 300741866 146842535
79+
0 1279660741 97125596
80+
3531244480 3147213622 507810286
81+
257581844 3816963790 101424269
82+
1298609589 3918388059 20216582
83+
3317726838 1072550929 21856732
84+
3065323607 1254863909 4121973
85+
97125596 1094407661 160456248
86+
359006113 1057194484 15356445
87+
374362558 1636971609 104335413
88+
4039054766 475330775 9209679
89+
1038424317 1376786337 260185272
90+
878530050 3938604641 109659804
91+
1784016098 3738041092 78922698
92+
3152462764 0 165264074
93+
1862938796 1741307022 269113414
94+
497536930 676201364 380993120
95+
3069445580 3655023908 83017184
96+
2950498004 165264074 114825603
97+
1015936228 1258985882 1835900
98+
478697971 1260821782 18838959
99+
1017772128 280089677 20652189
100+
1542303706 2905501230 241712392
101+
3339583570 484540454 191660910
102+
103+
light-to-temperature map:
104+
2827696039 489007811 183207687
105+
1480301347 3744628626 306791400
106+
695239418 130668965 358338846
107+
1297125534 2232912413 183175813
108+
3979319170 1917264287 315648126
109+
3010903726 948848843 968415444
110+
130668965 2663473525 564570453
111+
1053578264 4051420026 243547270
112+
2303677395 672215498 276633345
113+
1787092747 3228043978 516584648
114+
2580310740 2416088226 247385299
115+
116+
temperature-to-humidity map:
117+
4161466647 3871737509 133500649
118+
2423686895 2864370860 72123408
119+
1983529997 0 320533964
120+
3184295196 2695571092 41928210
121+
0 822932241 605870242
122+
3557076981 3267347843 604389666
123+
3226223406 2936494268 330853575
124+
2495810303 2737499302 126871558
125+
1108268519 1428802483 491674128
126+
605870242 320533964 502398277
127+
2622681861 2423686895 271884197
128+
2894566058 4005238158 289729138
129+
1599942647 1920476611 383587350
130+
131+
humidity-to-location map:
132+
2945628300 1864953738 334378942
133+
3467273713 3579654586 715312710
134+
975015905 1356290883 508662855
135+
1483678760 2498980024 1080674562
136+
3443998409 2199332680 23275304
137+
3280007242 2222607984 163991167
138+
4182586423 2386599151 112380873
139+
2564353322 975015905 381274978`;
140+
141+
export default input;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy