Skip to content

Commit

Permalink
5b + 6
Browse files Browse the repository at this point in the history
  • Loading branch information
jpastoor committed Dec 13, 2016
1 parent b682c24 commit 15c48f5
Show file tree
Hide file tree
Showing 3 changed files with 683 additions and 3 deletions.
18 changes: 15 additions & 3 deletions day5b/day5b.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import (
"fmt"
"strconv"
"strings"
"time"
)

func main() {

start := time.Now()
input := "ojvtpuvg"
output := make([]string, 8)
found := 0
for i := 0; found < 8; i++ {
hashStr := fmt.Sprintf("%x", md5.Sum([]byte(input+strconv.Itoa(i))))
if hashStr[:5] == "00000" {
sum := md5.Sum([]byte(input + strconv.Itoa(i)))

// Speeding up a bit
if sum[0] != 0 {
continue
}

hashStr := fmt.Sprintf("%x", sum)
if hashStr[:1] == "0" && hashStr[:5] == "00000" {
fmt.Print()
pos := string(hashStr[5])
posInt, err := strconv.Atoi(pos)

Expand All @@ -32,4 +40,8 @@ func main() {
}

fmt.Println(strings.Join(output, ""))

end := time.Now()

fmt.Printf("Took: %v", end.Sub(start))
}
70 changes: 70 additions & 0 deletions day6/day6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package main

import (
"sort"
"strings"

"fmt"

"github.com/jpastoor/advent-of-code-2016/aoc2016utils"
)

func main() {
loader := aoc2016utils.AssignmentLoader{}
input := loader.Load(6)

data := make([][]string, 8)
for columnIndex := 0; columnIndex < 8; columnIndex++ {
data[columnIndex] = make([]string, 0)
}

for _, line := range strings.Split(input, "\n") {

for columnIndex, ch := range line {
data[columnIndex] = append(data[columnIndex], string(ch))
}
}

for columnIndex := 0; columnIndex < 8; columnIndex++ {
// Count occurrences per character
occ := map[string]int{}
for _, ch := range data[columnIndex] {
value, _ := occ[string(ch)]
occ[string(ch)] = value + 1
}

sortedPairs := rankByWordCount(occ)
fmt.Print(sortedPairs[0].Key)
}

}

type Pair struct {
Key string
Value int
}

type PairList []Pair

func (p PairList) Len() int { return len(p) }
func (p PairList) Less(i, j int) bool {
if p[i].Value != p[j].Value {
return p[i].Value < p[j].Value
} else {
// with ties broken by alphabetization
return p[i].Key > p[j].Key
}
}
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }

func rankByWordCount(wordFrequencies map[string]int) PairList {
pl := make(PairList, len(wordFrequencies))
i := 0
for k, v := range wordFrequencies {
pl[i] = Pair{k, v}
i++
}
// FOR A: sort.Sort(sort.Reverse(pl))
sort.Sort(pl)
return pl
}
Loading

0 comments on commit 15c48f5

Please sign in to comment.