mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
2020-day13:oof modular arithmetic that I do not know...
This commit is contained in:
+42
-14
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/mathutil"
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -16,36 +17,63 @@ func main() {
|
|||||||
|
|
||||||
if part == 1 {
|
if part == 1 {
|
||||||
ans := part1(util.ReadFile("./input.txt"))
|
ans := part1(util.ReadFile("./input.txt"))
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
|
||||||
fmt.Println("Output:", ans)
|
fmt.Println("Output:", ans)
|
||||||
} else {
|
} else {
|
||||||
ans := part2(util.ReadFile("./input.txt"))
|
ans := part2(util.ReadFile("./input.txt"))
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
|
||||||
fmt.Println("Output:", ans)
|
fmt.Println("Output:", ans)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func part1(input string) int {
|
func part1(input string) int {
|
||||||
parsed := parseInput(input)
|
estimate, busses := parseInput(input)
|
||||||
_ = parsed
|
|
||||||
|
|
||||||
return 0
|
var busID, waited int
|
||||||
|
for timeValue := estimate; busID == 0; timeValue++ {
|
||||||
|
for _, inter := range busses {
|
||||||
|
if timeValue%inter[1] == 0 {
|
||||||
|
busID = inter[1]
|
||||||
|
waited = timeValue - estimate
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return busID * waited
|
||||||
|
}
|
||||||
|
|
||||||
|
// i didn't come up with this myself... generally speaking i understand it...
|
||||||
func part2(input string) int {
|
func part2(input string) int {
|
||||||
parsed := parseInput(input)
|
_, busses := parseInput(input)
|
||||||
_ = parsed
|
|
||||||
|
|
||||||
return 0
|
var timeValue int
|
||||||
|
runningProduct := 1
|
||||||
|
for _, bus := range busses {
|
||||||
|
index, busID := bus[0], bus[1]
|
||||||
|
// this for loop adjusts the time until the constaint for this bus is met
|
||||||
|
// i.e. ensure (time + index) is divisible by the busID to ensure the bus arrives
|
||||||
|
for (timeValue+index)%busID != 0 {
|
||||||
|
// running product is used to increment because it will not affect
|
||||||
|
// the modulo of any of the previously scheduled busses, we've found
|
||||||
|
// the frequency to match them.
|
||||||
|
// e.g. if busID: 5 & index: 2, min timeValue is 3 b/c (3+2)%5 == 0
|
||||||
|
// if the running product were 5, adding 5 means (8+2)%5 == 0
|
||||||
|
// and (3 + 5x + 2) % 5 == 0 for any x
|
||||||
|
timeValue += runningProduct
|
||||||
|
}
|
||||||
|
runningProduct *= busID
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseInput(input string) []int {
|
return timeValue
|
||||||
var ans []int
|
}
|
||||||
|
|
||||||
|
// busses are [2]int{index, busID}, not the best way to parse stuff but it works
|
||||||
|
func parseInput(input string) (estimate int, busses [][2]int) {
|
||||||
lines := strings.Split(input, "\n")
|
lines := strings.Split(input, "\n")
|
||||||
for _, l := range lines {
|
estimate = mathutil.StrToInt(lines[0])
|
||||||
ans = append(ans, mathutil.StrToInt(l))
|
for index, busID := range strings.Split(lines[1], ",") {
|
||||||
|
if busID != "x" {
|
||||||
|
busses = append(busses, [2]int{index, mathutil.StrToInt(busID)})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return ans
|
return estimate, busses
|
||||||
}
|
}
|
||||||
|
|||||||
+20
-15
@@ -1,18 +1,24 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
var tests1 = []struct {
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
|
)
|
||||||
|
|
||||||
|
var example = `939
|
||||||
|
7,13,x,x,59,x,31,19`
|
||||||
|
|
||||||
|
func Test_part1(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
want int
|
|
||||||
input string
|
input string
|
||||||
// add extra args if needed
|
want int
|
||||||
}{
|
}{
|
||||||
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
|
{"example", example, 295},
|
||||||
|
{"actual", util.ReadFile("input.txt"), 2092},
|
||||||
}
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
func TestPart1(t *testing.T) {
|
|
||||||
for _, tt := range tests1 {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := part1(tt.input); got != tt.want {
|
if got := part1(tt.input); got != tt.want {
|
||||||
t.Errorf("part1() = %v, want %v", got, tt.want)
|
t.Errorf("part1() = %v, want %v", got, tt.want)
|
||||||
@@ -21,17 +27,16 @@ func TestPart1(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var tests2 = []struct {
|
func Test_part2(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
want int
|
|
||||||
input string
|
input string
|
||||||
// add extra args if needed
|
want int
|
||||||
}{
|
}{
|
||||||
// {"actual", ACTUAL_ANSWER, util.ReadFile("input.txt")},
|
{"example", example, 1068781},
|
||||||
|
{"actual", util.ReadFile("input.txt"), 702970661767766},
|
||||||
}
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
func TestPart2(t *testing.T) {
|
|
||||||
for _, tt := range tests2 {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := part2(tt.input); got != tt.want {
|
if got := part2(tt.input); got != tt.want {
|
||||||
t.Errorf("part2() = %v, want %v", got, tt.want)
|
t.Errorf("part2() = %v, want %v", got, tt.want)
|
||||||
|
|||||||
Reference in New Issue
Block a user