Skip to content
Open

Dev #117

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions gcany0n_fizzymagics_ineluctable_coin_puzzle/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
2 changes: 1 addition & 1 deletion gcb41b3_analyze_this/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down
2 changes: 1 addition & 1 deletion ipieces/ipieces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
1 change: 1 addition & 0 deletions util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 16 additions & 0 deletions util/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
}
14 changes: 14 additions & 0 deletions util/math_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
9 changes: 0 additions & 9 deletions util/text.go
Original file line number Diff line number Diff line change
@@ -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{}
Expand Down