-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joel Smith
committed
Mar 8, 2019
1 parent
21a1412
commit e2ad97a
Showing
4 changed files
with
207 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Disc #1 has 17 positions; at time=0, it is at position 5. | ||
Disc #2 has 19 positions; at time=0, it is at position 8. | ||
Disc #3 has 7 positions; at time=0, it is at position 1. | ||
Disc #4 has 13 positions; at time=0, it is at position 7. | ||
Disc #5 has 5 positions; at time=0, it is at position 1. | ||
Disc #6 has 3 positions; at time=0, it is at position 0. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
type Disc struct { | ||
CurrentPosition int | ||
TotalPositions int | ||
ClearPosition int | ||
} | ||
|
||
func (d *Disc) advance(amt int) { | ||
d.CurrentPosition = (d.CurrentPosition + amt) % d.TotalPositions | ||
} | ||
|
||
func main() { | ||
logic() | ||
} | ||
|
||
func logic() { | ||
// discs := puzzleTestInput() | ||
discs := puzzleInput() | ||
t := 0 | ||
for ; ; t++ { | ||
clear, _, _ := isClear(discs, t) | ||
if clear { | ||
break | ||
} | ||
// advancement := 1 | ||
// if collidedDisc > 0 { | ||
// advancement = fakeLCM(discs[:collidedDisc]) | ||
// } | ||
|
||
// fmt.Println(colidedDiscIndex, discPosition) | ||
// guess next number: | ||
// Position + lcm(passed discs) + | ||
|
||
} | ||
fmt.Println("Drop in at t =", t) | ||
} | ||
|
||
func deepCopyDiscs(discs []Disc) []Disc { | ||
copy := make([]Disc, len(discs)) | ||
for i := range discs { | ||
copy[i] = Disc{discs[i].CurrentPosition, discs[i].TotalPositions, discs[i].ClearPosition } | ||
} | ||
return copy | ||
} | ||
|
||
func isClear(discs []Disc, t int) (bool, int, int) { | ||
copy := deepCopyDiscs(discs) | ||
|
||
for i := range copy { | ||
copy[i].advance(t) | ||
} | ||
for i := 0; i < len(copy); i++ { | ||
if copy[i].CurrentPosition != copy[i].ClearPosition { | ||
// fmt.Println("Will stop on disc", i+1) | ||
return false, i, copy[i].CurrentPosition | ||
} | ||
} | ||
return true, 0, 0 | ||
} | ||
|
||
// All of my numbers are prime, so the LCM of prime number is going to be their product | ||
func fakeLCM(nums []int) int { | ||
product := 1 | ||
|
||
for _, v := range nums { | ||
product *= v | ||
} | ||
return product | ||
} | ||
|
||
// Opting to hard code here, as parsing is pointless for such a small dataset | ||
func puzzleInput() []Disc { | ||
discs := []Disc{ | ||
{CurrentPosition: 5, TotalPositions: 17}, | ||
{CurrentPosition: 8, TotalPositions: 19}, | ||
{CurrentPosition: 1, TotalPositions: 7}, | ||
{CurrentPosition: 7, TotalPositions: 13}, | ||
{CurrentPosition: 1, TotalPositions: 5}, | ||
{CurrentPosition: 0, TotalPositions: 3}, | ||
{CurrentPosition: 0, TotalPositions: 11}, | ||
} | ||
for i := range discs { | ||
discs[i].ClearPosition = mod(discs[i].TotalPositions-1-i, discs[i].TotalPositions) | ||
} | ||
|
||
return discs | ||
} | ||
|
||
func puzzleTestInput() []Disc { | ||
discs := []Disc{ | ||
{CurrentPosition: 4, TotalPositions: 5}, | ||
{CurrentPosition: 1, TotalPositions: 2}, | ||
} | ||
for i := range discs { | ||
discs[i].ClearPosition = mod(discs[i].TotalPositions-1-i, discs[i].TotalPositions) | ||
} | ||
return discs | ||
} | ||
|
||
func mod(n int, modulo int) int { | ||
if n >= modulo { | ||
return n % modulo | ||
} else if n < 0 { | ||
offset := (-n) % modulo | ||
answer := modulo - offset | ||
if answer == modulo { // we could end up in a situation where offset is 0, then modulo - 0 = modulo = 0 | ||
return 0 | ||
} | ||
return answer | ||
} | ||
return n | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
func main() { | ||
logic() | ||
} | ||
|
||
func logic() { | ||
// input := puzzleTestInput() | ||
// stopAt := 20 | ||
input := puzzleInput() | ||
// stopAt := 272 // problem 1 | ||
stopAt := 35651584 // problem 2 | ||
|
||
for len(input) < stopAt { | ||
input = step(input) | ||
} | ||
input = input[:stopAt] | ||
|
||
fmt.Println(Checksum(input)) | ||
} | ||
|
||
func step(data string) string { | ||
copy := strings.Map(func(letter rune) rune { return letter }, data) | ||
reversed := Reverse(copy) | ||
notted := strings.Map(func(letter rune) rune { | ||
if letter == '0' { | ||
return '1' | ||
} | ||
return '0' | ||
}, reversed) | ||
return fmt.Sprintf("%v0%v", data, notted) | ||
} | ||
|
||
func Checksum(data string) string { | ||
checksum := make([]rune, len(data)/2) | ||
recurse := true | ||
if (len(checksum) % 2) == 1 { | ||
recurse = false | ||
} | ||
for i := 0; i < len(checksum); i++ { | ||
if data[2*i] == data[2*i+1] { | ||
checksum[i] = '1' | ||
} else { | ||
checksum[i] = '0' | ||
} | ||
} | ||
|
||
if recurse { | ||
return Checksum(string(checksum)) | ||
} else { | ||
return string(checksum) | ||
} | ||
} | ||
|
||
// borrowed from https://stackoverflow.com/questions/1752414/how-to-reverse-a-string-in-go | ||
func Reverse(s string) string { | ||
r := []rune(s) | ||
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { | ||
r[i], r[j] = r[j], r[i] | ||
} | ||
return string(r) | ||
} | ||
|
||
// Opting to hard code here, as parsing is pointless for such a small dataset | ||
func puzzleInput() string { | ||
return "01000100010010111" | ||
} | ||
|
||
func puzzleTestInput() string { | ||
return "10000" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters