diff --git a/gcany0n_fizzymagics_ineluctable_coin_puzzle/main.go b/gcany0n_fizzymagics_ineluctable_coin_puzzle/main.go new file mode 100644 index 0000000..21ec1d8 --- /dev/null +++ b/gcany0n_fizzymagics_ineluctable_coin_puzzle/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" + "strconv" + "strings" + + "github.com/bitlux/caches/util" +) + +const numbers = `19 23 30 7 75 38 60 69 80 + 6 74 28 37 19 8 5 26 2 + 5 32 5 23 19 2 3 11 34 + 8 68 64 29 29 23 13 22 5 +22 61 22 98 16 25 96 8 29 +17 23 29 48 5 99 4 16 32 +17 36 99 22 36 28 23 30 28 + 2 61 32 18 6 5 25 23 38 +19 33 8 22 69 26 32 8 18` + +func main() { + for i, str := range strings.Fields(numbers) { + n, err := strconv.Atoi(str) + util.Must(err) + fmt.Printf("%c", util.A1Decode(util.CollatzStoppingTime(n))) + if (i+1)%9 == 0 { + fmt.Println() + } + } +} diff --git a/gcb41b3_analyze_this/main.go b/gcb41b3_analyze_this/main.go index fa8d01c..26c64d5 100644 --- a/gcb41b3_analyze_this/main.go +++ b/gcb41b3_analyze_this/main.go @@ -30,7 +30,7 @@ If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea -- let's do more of those!` func main() { - histo := util.Histogram(strings.ToLower(text)) + histo := util.RuneCount(strings.ToLower(text)) fmt.Println(histo['m'] + histo['u']) fmt.Println(histo['x'] + histo['y']) fmt.Println(histo['x'] + histo['p']) diff --git a/ipieces/ipieces.go b/ipieces/ipieces.go index 9d0d957..80776f6 100644 --- a/ipieces/ipieces.go +++ b/ipieces/ipieces.go @@ -266,5 +266,5 @@ func (p Puzzle) Run() { port = "8080" } fmt.Println("Listening on port", port) - log.Fatal(http.ListenAndServe(":8080", nil)) + log.Fatal(http.ListenAndServe(":"+port, nil)) } diff --git a/util/file.go b/util/file.go index 2287239..82697f0 100644 --- a/util/file.go +++ b/util/file.go @@ -10,6 +10,7 @@ const maxBuf = 1024 * 1024 * 1024 // 1 GiB func readLines(file string, size int) ([]string, error) { f, err := os.Open(file) Must(err) + defer f.Close() buf := make([]byte, size) scanner := bufio.NewScanner(f) diff --git a/util/math.go b/util/math.go index c6e6af7..a2af435 100644 --- a/util/math.go +++ b/util/math.go @@ -58,3 +58,19 @@ func FromDigitsBase[T constraints.Integer](digits []T, base int) int { func FromDigits[T constraints.Integer](digits []T) int { return FromDigitsBase(digits, 10) } + +// CollatzStoppingTime returns the number of steps in the Collatz (3n+1) sequence before reaching 1. +func CollatzStoppingTime(i int) int { + n := 0 + for { + if i == 1 { + return n + } + if i%2 == 0 { + i /= 2 + } else { + i = 3*i + 1 + } + n++ + } +} diff --git a/util/math_test.go b/util/math_test.go index 4a8bb98..6ac4ca7 100644 --- a/util/math_test.go +++ b/util/math_test.go @@ -66,3 +66,17 @@ func TestFromDigits(t *testing.T) { } } } + +func TestCollatzStoppingTime(t *testing.T) { + for _, tc := range []struct { + input, want int + }{ + {1, 0}, + {19, 20}, + {11, 14}, + } { + if got := CollatzStoppingTime(tc.input); got != tc.want { + t.Errorf("CollatzStoppingTime(%d) = %d, want %d", tc.input, got, tc.want) + } + } +} diff --git a/util/text.go b/util/text.go index 71fbef9..fa66f25 100644 --- a/util/text.go +++ b/util/text.go @@ -1,14 +1,5 @@ package util -// Histogram produces a count of all of the letters that appear in s. -func Histogram(s string) map[rune]int { - histo := map[rune]int{} - for _, r := range s { - histo[r]++ - } - return histo -} - // RuneCount returns a map containing each rune in s and how many times it occurs. func RuneCount(s string) map[rune]int { ret := map[rune]int{}