Skip to content

Commit

Permalink
day 2 part two
Browse files Browse the repository at this point in the history
  • Loading branch information
angristan committed Dec 5, 2023
1 parent 25be763 commit 628bb91
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 2 deletions.
117 changes: 115 additions & 2 deletions 05/05.go
Expand Up @@ -13,10 +13,13 @@ import (
func main() {
input := utils.ParseInput("input.txt")

cards := ConvertInputToAlmanac(input)
part1Score := cards.GetLowestLocationNumber()
almanacV1 := ConvertInputToAlmanac(input)
part1Score := almanacV1.GetLowestLocationNumber()
fmt.Printf("Part 1: %d\n", part1Score)

almanacV2 := ConvertInputToAlmanacV2(input)
part2score := almanacV2.GetLowestLocationNumber()
fmt.Printf("Part 2: %d\n", part2score)
}

type Range struct {
Expand Down Expand Up @@ -123,3 +126,113 @@ func (almanac Almanac) GetLowestLocationNumber() int {

return locations[0]
}

type SeedV2 struct {
Number int
Range int
}

type AlmanacV2 struct {
Maps []Map
Seeds []SeedV2
}

var numberPairRegex = regexp.MustCompile(`(\d+)\s+(\d+)`)

func ConvertInputToAlmanacV2(input []string) AlmanacV2 {
seeds := []SeedV2{}

seedPairsString := numberPairRegex.FindAllString(input[0], -1)

for _, seedPairString := range seedPairsString {
seedPair := numberRegex.FindAllString(seedPairString, -1)
seedNumber, err := strconv.Atoi(seedPair[0])
if err != nil {
panic(err)
}

seedRange, err := strconv.Atoi(seedPair[1])
if err != nil {
panic(err)
}

seeds = append(seeds, SeedV2{
Number: seedNumber,
Range: seedRange,
})
}

maps := []Map{}

currentMap := Map{}
for _, line := range input[2:] {
if strings.Contains(line, "map") || line == "" {
if len(currentMap.Ranges) > 0 {
maps = append(maps, currentMap)
currentMap = Map{}
}
continue
}

indices := numberRegex.FindAllString(line, -1)
destinationIndex, err := strconv.Atoi(indices[0])
if err != nil {
panic(err)
}

sourceIndex, err := strconv.Atoi(indices[1])
if err != nil {
panic(err)
}

rangeLength, err := strconv.Atoi(indices[2])
if err != nil {
panic(err)
}

currentMap.Ranges = append(currentMap.Ranges, Range{
DestinationIndex: destinationIndex,
SourceIndex: sourceIndex,
RangeLength: rangeLength,
})
}

maps = append(maps, currentMap)

return AlmanacV2{
Maps: maps,
Seeds: seeds,
}
}

func (almanac AlmanacV2) GetSeedsLocations() []int {
locations := make([]int, 0)

for _, seed := range almanac.Seeds {
fmt.Printf("Seed: %d\n", seed.Number)
for i := seed.Number; i < seed.Number+seed.Range; i++ {
nextIndex := i
nextMap:
for _, m := range almanac.Maps {
for _, r := range m.Ranges {
if nextIndex >= r.SourceIndex && nextIndex < r.SourceIndex+r.RangeLength {
nextIndex = r.DestinationIndex + int(nextIndex) - r.SourceIndex
continue nextMap
}
}
}

location := nextIndex
locations = append(locations, location)
}
}

return locations
}

func (almanac AlmanacV2) GetLowestLocationNumber() int {
locations := almanac.GetSeedsLocations()
slices.Sort(locations)

return locations[0]
}
177 changes: 177 additions & 0 deletions 05/05_test.go
Expand Up @@ -173,3 +173,180 @@ func TestGetLowestLocationNumber(t *testing.T) {

assert.Equal(t, alamanac.GetLowestLocationNumber(), expectedLowestLocationNumber)
}

func TestConvertInputToAlmanacV2(t *testing.T) {
input := []string{
"seeds: 79 14 55 13",
"",
"seed-to-soil map:",
"50 98 2",
"52 50 48",
"",
"soil-to-fertilizer map:",
"0 15 37",
"37 52 2",
"39 0 15",
"",
"fertilizer-to-water map:",
"49 53 8",
"0 11 42",
"42 0 7",
"57 7 4",
"",
"water-to-light map:",
"88 18 7",
"18 25 70",
"",
"light-to-temperature map:",
"45 77 23",
"81 45 19",
"68 64 13",
"",
"temperature-to-humidity map:",
"0 69 1",
"1 0 69",
"",
"humidity-to-location map:",
"60 56 37",
"56 93 4",
}

want := AlmanacV2{
Seeds: []SeedV2{
{Number: 79, Range: 14},
{Number: 55, Range: 13},
},
Maps: []Map{
{Ranges: []Range{
{DestinationIndex: 50, SourceIndex: 98, RangeLength: 2},
{DestinationIndex: 52, SourceIndex: 50, RangeLength: 48}},
},
{Ranges: []Range{
{DestinationIndex: 0, SourceIndex: 15, RangeLength: 37},
{DestinationIndex: 37, SourceIndex: 52, RangeLength: 2},
{DestinationIndex: 39, SourceIndex: 0, RangeLength: 15}},
},
{Ranges: []Range{
{DestinationIndex: 49, SourceIndex: 53, RangeLength: 8},
{DestinationIndex: 0, SourceIndex: 11, RangeLength: 42},
{DestinationIndex: 42, SourceIndex: 0, RangeLength: 7},
{DestinationIndex: 57, SourceIndex: 7, RangeLength: 4}},
},
{Ranges: []Range{
{DestinationIndex: 88, SourceIndex: 18, RangeLength: 7},
{DestinationIndex: 18, SourceIndex: 25, RangeLength: 70}},
},
{Ranges: []Range{
{DestinationIndex: 45, SourceIndex: 77, RangeLength: 23},
{DestinationIndex: 81, SourceIndex: 45, RangeLength: 19},
{DestinationIndex: 68, SourceIndex: 64, RangeLength: 13}},
},
{Ranges: []Range{
{DestinationIndex: 0, SourceIndex: 69, RangeLength: 1},
{DestinationIndex: 1, SourceIndex: 0, RangeLength: 69}},
},
{Ranges: []Range{
{DestinationIndex: 60, SourceIndex: 56, RangeLength: 37},
{DestinationIndex: 56, SourceIndex: 93, RangeLength: 4}},
},
},
}

almanac := ConvertInputToAlmanacV2(input)

assert.Equal(t, want, almanac)
}

func TestGetLowestLocationNumberV2(t *testing.T) {
alamanac := AlmanacV2{
Seeds: []SeedV2{
{Number: 79, Range: 14},
{Number: 55, Range: 13},
},
Maps: []Map{
{Ranges: []Range{
{DestinationIndex: 50, SourceIndex: 98, RangeLength: 2},
{DestinationIndex: 52, SourceIndex: 50, RangeLength: 48}},
},
{Ranges: []Range{
{DestinationIndex: 0, SourceIndex: 15, RangeLength: 37},
{DestinationIndex: 37, SourceIndex: 52, RangeLength: 2},
{DestinationIndex: 39, SourceIndex: 0, RangeLength: 15}},
},
{Ranges: []Range{
{DestinationIndex: 49, SourceIndex: 53, RangeLength: 8},
{DestinationIndex: 0, SourceIndex: 11, RangeLength: 42},
{DestinationIndex: 42, SourceIndex: 0, RangeLength: 7},
{DestinationIndex: 57, SourceIndex: 7, RangeLength: 4}},
},
{Ranges: []Range{
{DestinationIndex: 88, SourceIndex: 18, RangeLength: 7},
{DestinationIndex: 18, SourceIndex: 25, RangeLength: 70}},
},
{Ranges: []Range{
{DestinationIndex: 45, SourceIndex: 77, RangeLength: 23},
{DestinationIndex: 81, SourceIndex: 45, RangeLength: 19},
{DestinationIndex: 68, SourceIndex: 64, RangeLength: 13}},
},
{Ranges: []Range{
{DestinationIndex: 0, SourceIndex: 69, RangeLength: 1},
{DestinationIndex: 1, SourceIndex: 0, RangeLength: 69}},
},
{Ranges: []Range{
{DestinationIndex: 60, SourceIndex: 56, RangeLength: 37},
{DestinationIndex: 56, SourceIndex: 93, RangeLength: 4}},
},
},
}

expectedLowestLocationNumber := 46

assert.Equal(t, alamanac.GetLowestLocationNumber(), expectedLowestLocationNumber)
}
func BenchmarkGetLowestLocationNumberV2(b *testing.B) {
alamanac := AlmanacV2{
Seeds: []SeedV2{
{Number: 79, Range: 14},
{Number: 55, Range: 13},
},
Maps: []Map{
{Ranges: []Range{
{DestinationIndex: 50, SourceIndex: 98, RangeLength: 2},
{DestinationIndex: 52, SourceIndex: 50, RangeLength: 48}},
},
{Ranges: []Range{
{DestinationIndex: 0, SourceIndex: 15, RangeLength: 37},
{DestinationIndex: 37, SourceIndex: 52, RangeLength: 2},
{DestinationIndex: 39, SourceIndex: 0, RangeLength: 15}},
},
{Ranges: []Range{
{DestinationIndex: 49, SourceIndex: 53, RangeLength: 8},
{DestinationIndex: 0, SourceIndex: 11, RangeLength: 42},
{DestinationIndex: 42, SourceIndex: 0, RangeLength: 7},
{DestinationIndex: 57, SourceIndex: 7, RangeLength: 4}},
},
{Ranges: []Range{
{DestinationIndex: 88, SourceIndex: 18, RangeLength: 7},
{DestinationIndex: 18, SourceIndex: 25, RangeLength: 70}},
},
{Ranges: []Range{
{DestinationIndex: 45, SourceIndex: 77, RangeLength: 23},
{DestinationIndex: 81, SourceIndex: 45, RangeLength: 19},
{DestinationIndex: 68, SourceIndex: 64, RangeLength: 13}},
},
{Ranges: []Range{
{DestinationIndex: 0, SourceIndex: 69, RangeLength: 1},
{DestinationIndex: 1, SourceIndex: 0, RangeLength: 69}},
},
{Ranges: []Range{
{DestinationIndex: 60, SourceIndex: 56, RangeLength: 37},
{DestinationIndex: 56, SourceIndex: 93, RangeLength: 4}},
},
},
}

for i := 0; i < b.N; i++ {
alamanac.GetLowestLocationNumber()
}

}

0 comments on commit 628bb91

Please sign in to comment.