mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-15 15:10:49 +00:00
59 lines
1002 B
Go
59 lines
1002 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"bufio"
|
||
|
"log"
|
||
|
"os"
|
||
|
"regexp"
|
||
|
"strconv"
|
||
|
)
|
||
|
|
||
|
// readlines reads lines from stdin.
|
||
|
// 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 getParams(rexp *regexp.Regexp, url string) map[string]string {
|
||
|
match := rexp.FindStringSubmatch(url)
|
||
|
|
||
|
m := make(map[string]string)
|
||
|
for i, name := range rexp.SubexpNames() {
|
||
|
if i > 0 && i <= len(match) {
|
||
|
m[name] = match[i]
|
||
|
}
|
||
|
}
|
||
|
return m
|
||
|
}
|
||
|
|
||
|
// mustConv converts string to int
|
||
|
// it will panic if an error occurs
|
||
|
func mustConv(s string) int {
|
||
|
n, err := strconv.Atoi(s)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return n
|
||
|
}
|
||
|
|
||
|
// mustConv converts string to int
|
||
|
// it will panic if an error occurs
|
||
|
func mustConvHex(s string) int {
|
||
|
n, err := strconv.ParseInt(s, 16, 64)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
return int(n)
|
||
|
}
|