Skip to content

Commit 1a789e0

Browse files
author
Ale Paredes
committed
Add 5
1 parent 315d2e5 commit 1a789e0

File tree

2 files changed

+376
-0
lines changed

2 files changed

+376
-0
lines changed

2023/day5/inp.txt

+243
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
seeds: 1514493331 295250933 3793791524 105394212 828589016 654882197 658370118 49359719 4055197159 59237418 314462259 268880047 2249227634 74967914 2370414906 38444198 3291001718 85800943 2102534948 5923540
2+
3+
seed-to-soil map:
4+
3352941879 1247490906 129850502
5+
1738919961 2189748071 56658550
6+
1795578511 292133467 518088747
7+
1519757661 1666834550 130335907
8+
1650093568 133993362 88826393
9+
2813914030 2262539545 40894545
10+
2698412916 2661705133 115501114
11+
2854808575 810222214 437268692
12+
410530961 1545057218 121777332
13+
0 242661076 10731898
14+
532308293 2303434090 61476099
15+
3292077267 1797170457 2282798
16+
2313667258 0 133993362
17+
2447660620 2410952837 250752296
18+
3294360065 253392974 38740493
19+
10731898 1377341408 167715810
20+
1457582089 2364910189 46042648
21+
593784392 3009289500 824592362
22+
3333100558 222819755 19841321
23+
1418376754 1799453255 39205335
24+
1503624737 2246406621 16132924
25+
178447708 2777206247 232083253
26+
3482792381 1838658590 351089481
27+
28+
soil-to-fertilizer map:
29+
3513795976 4258851234 36116062
30+
3393453635 4148223693 110627541
31+
3504081176 3494735450 6350258
32+
2671742993 3596285367 235915393
33+
991290653 256764866 25867175
34+
2907658386 3330719253 68855819
35+
3336496635 4091266693 56957000
36+
3161141476 2536943456 80523019
37+
1961696534 304660310 29551079
38+
812996514 1560772632 178294139
39+
1562163347 2321959023 78904062
40+
31289107 1039870587 339682886
41+
1520029818 1928417376 42133529
42+
3510431434 3832200760 3364542
43+
3549912038 3272523584 58195669
44+
0 149071717 31289107
45+
3608107707 3501085708 5732766
46+
503523937 1739066771 189350605
47+
3071674583 3506818474 89466893
48+
3241664495 3996434553 94832140
49+
1708133363 454333361 253563171
50+
3813493721 3097084118 175439466
51+
1991247613 1970550905 351408118
52+
3613840473 2897430870 199653248
53+
2536943456 2762631333 134799537
54+
1641067409 244319429 12445437
55+
3994753216 3913095788 83338765
56+
2976514205 3399575072 95160378
57+
1354883134 0 143118415
58+
1349131883 2400863085 5751251
59+
1498001549 282632041 22028269
60+
2342655731 180360824 63958605
61+
370971993 1379553473 126598642
62+
4149802438 2617466475 145164858
63+
1017157828 707896532 331974055
64+
692874542 334211389 120121972
65+
497570635 143118415 5953302
66+
3988933187 3907275759 5820029
67+
4078091981 3835565302 71710457
68+
1653512846 1506152115 54620517
69+
70+
fertilizer-to-water map:
71+
3053686523 2028998994 1241280773
72+
1492748555 1562401968 269616514
73+
554432178 1000324407 562077561
74+
2944878746 3270279767 108807777
75+
2501520804 3379087544 52288887
76+
1762365069 20686928 69653413
77+
2028998994 3431376431 472521810
78+
1431309984 814973200 3653900
79+
409832614 0 20686928
80+
430519542 818627100 123912636
81+
1116509739 500172955 314800245
82+
2553809691 3903898241 391069055
83+
0 90340341 409832614
84+
1434963884 942539736 57784671
85+
86+
water-to-light map:
87+
2774754469 1598606098 15160294
88+
3832622498 1469118874 129487224
89+
4125818569 3997047227 169148727
90+
1108418694 1130695768 196125912
91+
637654660 517892123 26551592
92+
2519230072 3399515763 135968347
93+
2751000257 3126996880 23754212
94+
861800165 884077239 88532605
95+
1469118874 2566660966 63929427
96+
2789914763 3150751092 63425583
97+
2853340346 2561960449 4700517
98+
1692681911 3397592997 1922766
99+
2858040863 1773257241 287341147
100+
2655198419 2851614649 15098309
101+
950332770 972609844 158085924
102+
591049922 471287385 46604738
103+
3482333989 3849276196 108901632
104+
1694604677 1613766392 159490849
105+
3591235621 3958177828 38869399
106+
3630105020 4166195954 128771342
107+
3205039781 3535484110 277294208
108+
2706794606 2060598388 44205651
109+
386607659 544443715 110595292
110+
3962109722 2540862463 21097986
111+
697311924 0 92263281
112+
558450415 404456802 11447837
113+
2409200012 3214176675 110030060
114+
569898252 862925569 21151670
115+
1354525884 415904639 55382746
116+
789575205 790700609 72224960
117+
3832262624 2690248164 359874
118+
163757080 181606223 222850579
119+
1533048301 2967363270 159633610
120+
497202951 784452458 6248151
121+
2670296728 3812778318 36497878
122+
3145382010 2630590393 59657771
123+
1854095526 2104804039 436058424
124+
0 655039007 74414138
125+
2390804262 2690608038 18395750
126+
1304544606 1326821680 49981278
127+
3758876362 3324206735 73386262
128+
664206252 1376802958 33105672
129+
74414138 92263281 89342942
130+
3983207708 2709003788 142610861
131+
503451102 729453145 54999313
132+
2290153950 2866712958 100650312
133+
134+
light-to-temperature map:
135+
2054128675 422374783 216418447
136+
3729049939 3132111492 565917357
137+
524183620 1261361039 34450583
138+
723901655 638793230 74616934
139+
304496246 795175951 115896188
140+
1128506994 1008723417 50927515
141+
958650763 2074746732 3056214
142+
0 1224918384 36442655
143+
558634203 1059650932 165267452
144+
2052385426 713410164 1743249
145+
833053864 1953862956 120883776
146+
36442655 715153413 80022538
147+
1455705971 1506174459 386316618
148+
1842022589 1295811622 210362837
149+
2270547122 2077802946 24477429
150+
116465193 2102280375 188031053
151+
1179434509 945607414 63116003
152+
798518589 911072139 34535275
153+
3373647615 3893335786 195498774
154+
3696979816 3698028849 32070123
155+
1242550512 1892491077 43410067
156+
3051130577 2566857633 322517038
157+
3002478931 3844684140 48651646
158+
2311039190 3730098972 114585168
159+
2425624358 2311039190 187630064
160+
2819387158 2889374671 183091773
161+
1285960579 103791186 85466201
162+
1371426780 338095592 84279191
163+
1110545182 1935901144 17961812
164+
953937640 2290311428 4713123
165+
420392434 0 103791186
166+
961706977 189257387 148838205
167+
3637334768 3072466444 59645048
168+
3569146389 2498669254 68188379
169+
2613254422 4088834560 206132736
170+
171+
temperature-to-humidity map:
172+
2032423062 2486277941 26281270
173+
333062067 2316624216 6051173
174+
1716048249 1385455997 91409968
175+
460397469 2512559211 69041956
176+
25538975 668468772 15388105
177+
2090913379 324344034 71221218
178+
2804805674 3018690414 130533369
179+
529439425 908821722 476634275
180+
1106765273 448560048 219908724
181+
2162134597 1476865965 445565302
182+
0 395565252 25538975
183+
4004204691 4019550481 43244121
184+
2058704332 0 32209047
185+
1516375281 2116951248 199672968
186+
3888833667 2764269134 115371024
187+
40927080 32209047 292134987
188+
1490276549 2581601167 26098732
189+
1360818538 2322675389 129458011
190+
1006073700 2043715496 73235752
191+
1079309452 421104227 27455821
192+
1807458217 683856877 224964845
193+
339113240 1922431267 121284229
194+
4155917040 2879640158 139050256
195+
2764269134 4171262830 40536540
196+
4047448812 4062794602 108468228
197+
2935339043 3149223783 870326698
198+
1326673997 2452133400 34144541
199+
3805665741 4211799370 83167926
200+
201+
humidity-to-location map:
202+
3928575650 3147563455 98804874
203+
2357899446 2418187254 26586982
204+
449562184 2261875136 59054833
205+
308294839 60287808 141267345
206+
1872062279 1591999301 10857495
207+
1227084719 574109504 259895254
208+
1980177059 1765508840 13399403
209+
1162615704 1941046629 64469015
210+
2384486428 2654234915 114059261
211+
702311863 1515542756 40659242
212+
1993576462 2005515644 256359492
213+
3047301127 3246368329 27789688
214+
650396000 1713592977 51915863
215+
3701335066 3862068622 181049310
216+
1122863338 1673840611 39752366
217+
3075090815 4174917460 120049836
218+
3882384376 2778717800 46191274
219+
1626219043 201555153 245843236
220+
4027380524 3325898523 267586772
221+
3417979817 4043117932 13866284
222+
3281454033 2925002624 18592540
223+
110359150 1556201998 35797303
224+
2558833497 2444774236 209460679
225+
508617017 834004758 141778983
226+
1574315753 975783741 51903290
227+
2249935954 1027687031 107963492
228+
0 463750354 110359150
229+
2778717800 3593485295 268583327
230+
3195140651 2824909074 86313382
231+
3445626269 2943595164 203968291
232+
3324037823 4080975466 93941994
233+
742971105 1135650523 379892233
234+
146156453 1778908243 39038597
235+
3300046573 4056984216 23991250
236+
1503331938 1602856796 70983815
237+
185195050 1817946840 123099789
238+
3649594560 3274158017 21920411
239+
1486979973 447398389 16351965
240+
3671514971 3296078428 29820095
241+
2498545689 0 60287808
242+
3431846101 2911222456 13780168
243+
1882919774 2320929969 97257285

2023/day5/main.go

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"math"
7+
"os"
8+
"regexp"
9+
"strconv"
10+
"strings"
11+
)
12+
13+
var categories = []string{
14+
"seed-to-soil",
15+
"soil-to-fertilizer",
16+
"fertilizer-to-water",
17+
"water-to-light",
18+
"light-to-temperature",
19+
"temperature-to-humidity",
20+
"humidity-to-location",
21+
}
22+
23+
type Destination struct {
24+
value int
25+
rangel int
26+
}
27+
28+
func getLocation(seed int, maps map[string]map[int]Destination) int {
29+
index := seed
30+
for _, c := range categories {
31+
for source, destination := range maps[c] {
32+
if index >= source && index <= source+destination.rangel {
33+
delta := index - source
34+
index = destination.value + delta
35+
break
36+
}
37+
}
38+
}
39+
40+
return index
41+
}
42+
func getMinLocation(seeds []int, maps map[string]map[int]Destination) (value int) {
43+
minLocation := math.MaxInt64
44+
45+
for _, s := range seeds {
46+
location := getLocation(s, maps)
47+
if location < minLocation {
48+
minLocation = location
49+
}
50+
51+
}
52+
53+
return minLocation
54+
}
55+
56+
func expandRanges(seeds []int, maps map[string]map[int]Destination) int {
57+
minLocation := math.MaxInt64
58+
59+
for i := 0; i < len(seeds); i += 2 {
60+
start := seeds[i]
61+
rangel := seeds[i+1]
62+
for j := start; j < start+rangel; j++ {
63+
location := getLocation(j, maps)
64+
if location < minLocation {
65+
minLocation = location
66+
}
67+
}
68+
69+
}
70+
71+
return minLocation
72+
}
73+
74+
func main() {
75+
76+
file, err := os.Open("inp.txt")
77+
if err != nil {
78+
fmt.Println(err)
79+
}
80+
defer file.Close()
81+
82+
scanner := bufio.NewScanner(file)
83+
84+
var seeds []int
85+
var maps = make(map[string]map[int]Destination)
86+
currentCategory := ""
87+
88+
for scanner.Scan() {
89+
line := scanner.Text()
90+
91+
if strings.Contains(line, "seeds:") {
92+
re := regexp.MustCompile(`\d+`)
93+
94+
for _, s := range re.FindAllString(line, -1) {
95+
n, _ := strconv.Atoi(s)
96+
seeds = append(seeds, n)
97+
}
98+
99+
continue
100+
}
101+
102+
if line == "" {
103+
currentCategory = ""
104+
continue
105+
}
106+
107+
if strings.Contains(line, "-to-") {
108+
category := regexp.MustCompile(`(\w+)-to-(\w+)`).FindAllString(line, -1)[0]
109+
110+
if len(maps[category]) == 0 {
111+
maps[category] = make(map[int]Destination)
112+
}
113+
114+
currentCategory = category
115+
} else {
116+
ranges := regexp.MustCompile(`\d+`).FindAllString(line, -1)
117+
118+
destination, _ := strconv.Atoi(ranges[0])
119+
source, _ := strconv.Atoi(ranges[1])
120+
rangel, _ := strconv.Atoi(ranges[2])
121+
122+
maps[currentCategory][source] = Destination{value: destination, rangel: rangel}
123+
124+
}
125+
}
126+
127+
fmt.Println("Part 1: ", getMinLocation(seeds, maps))
128+
fmt.Println("Part 2:", expandRanges(seeds, maps))
129+
130+
if err := scanner.Err(); err != nil {
131+
fmt.Println(err)
132+
}
133+
}

0 commit comments

Comments
 (0)