Skip to content

Commit

Permalink
A small refactor to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
devries committed Dec 5, 2023
1 parent b137845 commit 90b39c1
Showing 1 changed file with 34 additions and 57 deletions.
91 changes: 34 additions & 57 deletions day05p2/solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,69 +48,14 @@ func Solve(r io.Reader) any {
conversions = append(conversions, c)

case ln == "":
// Conversion array complete, calculate conversions
sort.Slice(conversions, func(i, j int) bool { return conversions[i].Start < conversions[j].Start })
newvalues := []int64{}

for i := 0; i < len(values); i += 2 {
// For each input value and range we convert to a new value and range.
// If the range is longer than the valid interval of the conversion we split up the range into two intervals
// and then convert the second value and range as well... if that one is longer than the valid interval we repeat
start, length := values[i], values[i+1]

for {
delta, interval := getDeltaInterval(conversions, start)
newvalues = append(newvalues, start+delta)
if length <= interval || interval == 0 { // 0 interval means the rest of the numbers follow that delta
// The length of the input value range is less than the conversion interval
newvalues = append(newvalues, length)
break
} else {
// The length of the input value range is greater than the remaining
// conversion interval, we need to split the solution up into multiple
// ranges
newvalues = append(newvalues, interval)
start = start + interval
length = length - interval
}
}
}
values = newvalues
values = doConversion(conversions, values)

conversions = []Conversion{}
}
}

if len(conversions) > 0 {
// Do last conversion
sort.Slice(conversions, func(i, j int) bool { return conversions[i].Start < conversions[j].Start })
newvalues := []int64{}

for i := 0; i < len(values); i += 2 {
// For each input value and range we convert to a new value and range.
// If the range is longer than the valid interval of the conversion we split up the range into two intervals
// and then convert the second value and range as well... if that one is longer than the valid interval we repeat
start, length := values[i], values[i+1]

for {
delta, interval := getDeltaInterval(conversions, start)
newvalues = append(newvalues, start+delta)
if length <= interval || interval == 0 { // 0 interval means the rest of the numbers follow that delta
// The length of the input value range is less than the conversion interval
newvalues = append(newvalues, length)
break
} else {
// The length of the input value range is greater than the remaining
// conversion interval, we need to split the solution up into multiple
// ranges
newvalues = append(newvalues, interval)
start = start + interval
length = length - interval
}
}
}

values = newvalues
values = doConversion(conversions, values)
}

min := values[0]
Expand Down Expand Up @@ -158,3 +103,35 @@ func getDeltaInterval(arr []Conversion, val int64) (int64, int64) {
return 0, 0
}
}

// Run conversions and return new values array
func doConversion(conversions []Conversion, values []int64) []int64 {
// Conversion array complete, calculate conversions
sort.Slice(conversions, func(i, j int) bool { return conversions[i].Start < conversions[j].Start })
newvalues := []int64{}

for i := 0; i < len(values); i += 2 {
// For each input value and range we convert to a new value and range.
// If the range is longer than the valid interval of the conversion we split up the range into two intervals
// and then convert the second value and range as well... if that one is longer than the valid interval we repeat
start, length := values[i], values[i+1]

for {
delta, interval := getDeltaInterval(conversions, start)
newvalues = append(newvalues, start+delta)
if length <= interval || interval == 0 { // 0 interval means the rest of the numbers follow that delta
// The length of the input value range is less than the conversion interval
newvalues = append(newvalues, length)
break
} else {
// The length of the input value range is greater than the remaining
// conversion interval, we need to split the solution up into multiple
// ranges
newvalues = append(newvalues, interval)
start = start + interval
length = length - interval
}
}
}
return newvalues
}

0 comments on commit 90b39c1

Please sign in to comment.