mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-25 03:40:49 +00:00
92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"log"
|
|
"os"
|
|
"sort"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
// readlines reads lines from stdin.
|
|
// Then it returns them as an array of strings
|
|
func readlines() []string {
|
|
lines := []string{}
|
|
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
for scanner.Scan() {
|
|
lines = append(lines, scanner.Text())
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return lines
|
|
}
|
|
|
|
func parselines(lines []string) {
|
|
for _, line := range lines {
|
|
splitted := strings.Split(line, " ")
|
|
n, _ := strconv.Atoi(splitted[1])
|
|
hands = append(hands, newHand(splitted[0], n))
|
|
}
|
|
}
|
|
|
|
// matches checks for number of matching cards in hand
|
|
func matches(cards string) map[rune]int {
|
|
m := map[rune]int{}
|
|
for _, r := range cards {
|
|
_, ok := m[r]
|
|
if !ok {
|
|
m[r] = strings.Count(cards, string(r))
|
|
}
|
|
}
|
|
|
|
return m
|
|
}
|
|
|
|
// contains returns true if a slice of elements contains a given element
|
|
func contains[T comparable](elems []T, v T) bool {
|
|
for _, s := range elems {
|
|
if v == s {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// indexOf returns the index of an element in a slice
|
|
func indexOf(collection []rune, elem rune) int {
|
|
for i, x := range collection {
|
|
if x == elem {
|
|
return i
|
|
}
|
|
}
|
|
return -1
|
|
}
|
|
|
|
func containsChar(s string, c string) bool {
|
|
for _, v := range s {
|
|
if c == string(v) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func sortByKindAndStrength(strength []rune) {
|
|
sort.Slice(hands, func(i, j int) bool {
|
|
if hands[i].kind() == hands[j].kind() {
|
|
for k := range hands[i].cards {
|
|
if hands[i].cards[k] == hands[j].cards[k] {
|
|
continue
|
|
}
|
|
return indexOf(strength, rune(hands[i].cards[k])) > indexOf(strength, rune(hands[j].cards[k]))
|
|
}
|
|
}
|
|
return hands[i].kind() < hands[j].kind()
|
|
})
|
|
}
|