Skip to content

Commit

Permalink
feat(2023.05): add solution
Browse files Browse the repository at this point in the history
  • Loading branch information
jyggen committed Dec 5, 2023
1 parent 7056a5a commit 815a8ee
Show file tree
Hide file tree
Showing 3 changed files with 393 additions and 0 deletions.
205 changes: 205 additions & 0 deletions 2023/05-if-you-give-a-seed-a-fertilizer/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
seeds: 304740406 53203352 1080760686 52608146 1670978447 367043978 1445830299 58442414 4012995194 104364808 4123691336 167638723 2284615844 178205532 3164519436 564398605 90744016 147784453 577905361 122056749

seed-to-soil map:
0 699677807 922644641
4174180469 3833727510 120786827
1525682201 2566557266 229511566
3280624601 3954514337 340452959
2228029508 2796068832 310221139
3621077560 3280624601 553102909
2120836342 592484641 107193166
1982514669 227320902 138321673
1755193767 0 227320902
922644641 1622322448 603037560
2538250647 365642575 226842066
2765092713 2225360008 341197258

soil-to-fertilizer map:
1916776044 145070025 3464138
1920240182 0 145070025
706160141 2208005933 115191764
2898492924 830275742 87027483
3489083348 3344594558 103871907
2985520407 148534163 415139950
821351905 917303225 327392865
1148744770 1517236949 182706102
295069722 3448466465 411090419
1816984891 3244803405 99791153
4282585972 4292886644 2080652
3592955255 563674113 266601629
4266462972 4158154511 16123000
1331450872 1244696090 272540859
2715943131 3062253612 182549793
4284666624 4174277511 10300672
4158154511 4184578183 108308461
1603991731 1995012773 212993160
2065310207 2411620688 650632924
0 1699943051 295069722
3400660357 2323197697 88422991

fertilizer-to-water map:
3585244197 3493316345 482900943
2871272496 878061687 456215665
3477664135 4187387234 107580062
845559238 15587711 56716031
121711204 2918313406 409174755
1639718746 0 15587711
530885959 2603640127 314673279
902275269 2435903232 167736895
2635221133 72303742 236051363
1070012164 308355105 569706582
1699846244 1334277352 935374889
4279315086 3477664135 15652210
1655306457 2269652241 44539787
109056711 2423248739 12654493
0 2314192028 109056711
4068145140 3976217288 211169946

water-to-light map:
3841742547 3016842841 17384315
2875021919 2637593760 185450069
3413635232 3588265685 87508205
1311241677 236307150 54007684
3349161906 4276682782 18284514
896790030 1355845673 34430118
3060471988 3835573209 145836645
2741184131 3675773890 133837788
1387754847 947687177 15489861
3785944618 2057196631 55797929
2006585491 2931426646 85416195
3873217816 3809611678 25961531
1667765627 643929130 34884144
2092001686 2434956599 202637161
1001898651 158618769 77688381
3899179347 2253048950 181907649
1786416461 377140410 101956748
0 833901414 113785763
1403244708 479097158 56815029
3859126862 3034227156 14090954
747996464 678813274 31450438
869173795 963177038 27616235
3268502638 2006585491 50611140
113785763 0 148879571
262665334 1511505797 386606610
1187603975 710263712 123637702
3319113778 3987361499 30048128
3367446420 2885237834 46188812
931220148 990793273 15913032
1460059737 1006706305 120880314
1079587032 535912187 108016943
3645890228 2112994560 140054390
3206308633 2823043829 62194005
1888373209 148879571 9739198
3501143437 3443518894 144746791
779446902 1127586619 89726893
947133180 1217313512 54765471
2481910976 4017409627 259273155
1365249361 1390275791 22505486
4087038641 3048318110 207928655
1702649771 1272078983 83766690
649271944 1412781277 98724520
2294638847 3256246765 187272129
4081086996 3981409854 5951645
1580940051 290314834 86825576

light-to-temperature map:
2659452899 3773423191 23529065
1010417677 1830019321 229964714
1506263997 1764304095 65715226
3017023682 3993999178 103632805
3758361154 3931294907 62704271
2513441862 2529586713 106552791
3821065425 3163657189 7959671
3410504451 3191697730 271334719
2500616406 3150831733 12825456
2065874786 2636139504 257698620
4142272690 2382216135 108163002
1377732678 1378901025 61208694
91217027 248578952 8927711
2463617376 3879075083 36999030
3982807123 2315058258 67157877
2323573406 2065874786 97274446
958870382 916323074 51547295
3868386197 3579887474 114420926
931392999 1351423642 27477383
2942753127 3694308400 74270555
1812734437 168620508 79958444
3301364949 2163149232 3197696
2420847852 2166346928 42769524
3829025096 3111470632 39361101
2619994653 2490379137 39207576
1571979223 1523548881 240755214
2927532333 3916074113 15220794
3125500723 4097631983 175864226
1438941372 10080856 67322625
2049903179 0 10080856
3304562645 2209116452 105941806
1976132043 1277652506 73771136
2659202229 3171616860 250670
4256036535 3463032449 38930761
1240382391 257506663 137350287
0 77403481 91217027
3120656487 3768578955 4844236
100144738 967870369 309782137
409926875 394856950 521466124
2682981964 4273496209 21471087
2704453051 3501963210 77924264
2802207515 2893838124 125324818
3681839170 3796952256 76521984
4250435692 3873474240 5600843
1892692881 1440109719 83439162
4049965000 3019162942 92307690
2782377315 3171867530 19830200

temperature-to-humidity map:
1281293605 2434144353 57731817
3534843655 3623804479 36539813
1516028925 367078655 499627624
3340374639 3427302148 25514722
1176213912 2491876170 105079693
3872645852 3827818849 188531931
508302359 1375008638 300832898
0 866706279 508302359
4146417618 3475254801 148549678
4083438506 3660344292 62979112
3365889361 3745584127 82234722
4061177783 3723323404 22260723
2015656549 1675841536 348405327
1056134836 246999579 120079076
3448124083 3452816870 22437931
3321587434 3408514943 18787205
3470562014 4016350780 64281641
3571383468 3321587434 86927509
1339025422 2024246863 177003503
809135257 0 246999579
2364061876 2596955863 115651453
3658310977 4080632421 214334875
2479713329 2201250366 232893987

humidity-to-location map:
2408792839 708984436 12070437
3916327360 4103567762 90492800
2136669394 2902458135 226099404
1414655297 721054873 722014097
2462136308 3514619416 2467233
1254861475 3327498132 98562162
2362768798 569836962 46024041
4185175199 3916327360 8885363
421054090 234463197 201173738
2497827912 1550759989 35404865
849065671 0 224309687
37059832 615861003 93123433
4006820160 3925212723 178355039
2863253575 1705311678 653833074
622227828 1443068970 107691019
2464603541 2869233764 33224371
1353423637 3128557539 61231660
1073375358 3426060294 47286090
2725544642 3189789199 137708933
26906322 224309687 10153510
2420863276 3473346384 41273032
729918847 1586164854 119146824
2533232777 2676921899 192311865
0 2359144752 26906322
1120661448 435636935 134200027
130183265 2386051074 290870825
129 changes: 129 additions & 0 deletions 2023/05-if-you-give-a-seed-a-fertilizer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package main

import (
"fmt"
"math"
"os"
"strconv"

"github.com/jyggen/advent-of-go/internal/solver"
"github.com/jyggen/advent-of-go/internal/utils"
)

func main() {
p1, p2, err := solver.SolveFromFile(os.Stdin, SolvePart1, SolvePart2)
if err != nil {
panic(err)
}

fmt.Println(p1)
fmt.Println(p2)
}

func parseMap(section string) [][3]int {
numbers := utils.ToOptimisticIntSlice(section, false)
numbersMap := make([][3]int, 0, len(numbers)/3)

for i := 0; i < len(numbers); i += 3 {
numbersMap = append(numbersMap, [3]int{numbers[i], numbers[i+1], numbers[i+2]})
}

return numbersMap
}

func parseSeedMap(section string) [][2]int {
numbers := utils.ToOptimisticIntSlice(section, false)
numbersMap := make([][2]int, 0, len(numbers)/2)

for i := 0; i < len(numbers); i += 2 {
numbersMap = append(numbersMap, [2]int{numbers[i], numbers[i+1]})
}

return numbersMap
}

func parsePart1(input string) ([]int, [][3]int, [][3]int, [][3]int, [][3]int, [][3]int, [][3]int, [][3]int) {
sections := utils.ToStringSlice(input, "\n\n")
seeds := utils.ToOptimisticIntSlice(sections[0], false)
seedToSoil := parseMap(sections[1])
soilToFertilizer := parseMap(sections[2])
fertilizerToWater := parseMap(sections[3])
waterToLight := parseMap(sections[4])
lightToTemperature := parseMap(sections[5])
temperatureToHumidity := parseMap(sections[6])
humidityToLocation := parseMap(sections[7])

return seeds, seedToSoil, soilToFertilizer, fertilizerToWater, waterToLight, lightToTemperature, temperatureToHumidity, humidityToLocation
}

func parsePart2(input string) ([][2]int, [][3]int, [][3]int, [][3]int, [][3]int, [][3]int, [][3]int, [][3]int) {
sections := utils.ToStringSlice(input, "\n\n")
seeds := parseSeedMap(sections[0])
seedToSoil := parseMap(sections[1])
soilToFertilizer := parseMap(sections[2])
fertilizerToWater := parseMap(sections[3])
waterToLight := parseMap(sections[4])
lightToTemperature := parseMap(sections[5])
temperatureToHumidity := parseMap(sections[6])
humidityToLocation := parseMap(sections[7])

return seeds, seedToSoil, soilToFertilizer, fertilizerToWater, waterToLight, lightToTemperature, temperatureToHumidity, humidityToLocation
}

func resolvePart1(source int, sourceDestinationMap [][3]int) int {
for _, v := range sourceDestinationMap {
if source >= v[1] && source < v[1]+v[2] {
return (v[0] + v[2]) - ((v[1] + v[2]) - source)
}
}

return source
}

func resolvePart2(destination int, sourceDestinationMap [][3]int) int {
for _, v := range sourceDestinationMap {
if destination >= v[0] && destination < v[0]+v[2] {
return (v[1] + v[2]) - ((v[0] + v[2]) - destination)
}
}

return destination
}

func SolvePart1(input string) (string, error) {
seeds, seedToSoil, soilToFertilizer, fertilizerToWater, waterToLight, lightToTemperature, temperatureToHumidity, humidityToLocation := parsePart1(input)
lowest := math.MaxInt

for _, seed := range seeds {
soil := resolvePart1(seed, seedToSoil)
fertilizer := resolvePart1(soil, soilToFertilizer)
water := resolvePart1(fertilizer, fertilizerToWater)
light := resolvePart1(water, waterToLight)
temperature := resolvePart1(light, lightToTemperature)
humidity := resolvePart1(temperature, temperatureToHumidity)
location := resolvePart1(humidity, humidityToLocation)
lowest = min(location, lowest)
}

return strconv.Itoa(lowest), nil
}

func SolvePart2(input string) (string, error) {
seeds, seedToSoil, soilToFertilizer, fertilizerToWater, waterToLight, lightToTemperature, temperatureToHumidity, humidityToLocation := parsePart2(input)

for i := 0; ; i++ {
humidity := resolvePart2(i, humidityToLocation)
temperature := resolvePart2(humidity, temperatureToHumidity)
light := resolvePart2(temperature, lightToTemperature)
water := resolvePart2(light, waterToLight)
fertilizer := resolvePart2(water, fertilizerToWater)
soil := resolvePart2(fertilizer, soilToFertilizer)
seed := resolvePart2(soil, seedToSoil)

for _, s := range seeds {
if seed >= s[0] && seed <= s[0]+s[1] {
return strconv.Itoa(i), nil
}
}
}
}
59 changes: 59 additions & 0 deletions 2023/05-if-you-give-a-seed-a-fertilizer/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"strconv"
"testing"

"github.com/jyggen/advent-of-go/internal/solver"
)

var testCases = []*solver.TestCase{
{
Input: "seeds: 79 14 55 13\n\nseed-to-soil map:\n50 98 2\n52 50 48\n\nsoil-to-fertilizer map:\n0 15 37\n37 52 2\n39 0 15\n\nfertilizer-to-water map:\n49 53 8\n0 11 42\n42 0 7\n57 7 4\n\nwater-to-light map:\n88 18 7\n18 25 70\n\nlight-to-temperature map:\n45 77 23\n81 45 19\n68 64 13\n\ntemperature-to-humidity map:\n0 69 1\n1 0 69\n\nhumidity-to-location map:\n60 56 37\n56 93 4",
Solvers: []*solver.TestCaseSolver{
{
Solver: SolvePart1,
Output: "35",
},
{
Solver: SolvePart2,
Output: "46",
},
},
},
{
Input: solver.InputFromFile("input.txt"),
Short: true,
Solvers: []*solver.TestCaseSolver{
{
Solver: SolvePart1,
Output: "993500720",
},
{
Solver: SolvePart2,
Output: "4917124",
},
},
},
}

func BenchmarkSolvers(b *testing.B) {
for i, testCase := range testCases {
b.Run(strconv.Itoa(i), func(subtest *testing.B) {
testCase.Benchmark(subtest)
})
}
}

func TestSolvers(t *testing.T) {
t.Parallel()

for i, testCase := range testCases {
testCase := testCase

t.Run(strconv.Itoa(i), func(subtest *testing.T) {
subtest.Parallel()
testCase.Test(subtest)
})
}
}

0 comments on commit 815a8ee

Please sign in to comment.