use central shtools module instead of copied aoclib

This commit is contained in:
Stefan Harmuth 2020-12-26 10:26:29 +01:00
parent a3abcfd0c5
commit 14a6f70b97
13 changed files with 48 additions and 147 deletions

View File

@ -1,99 +0,0 @@
package aoclib
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"time"
)
type Puzzle struct {
FromFile string
RawInput []string
RawInputInt []int
}
func (p *Puzzle) ReadInputFromFile() {
fh, err := os.Open(p.FromFile)
if err != nil {
log.Fatal("fopen error:", err)
}
defer fh.Close()
p.RawInput = []string{}
scanner := bufio.NewScanner(fh)
for scanner.Scan() {
p.RawInput = append(p.RawInput, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal("scanner error:", err)
}
}
func (p *Puzzle) GetInputArray() []string {
if len(p.RawInput) == 0 && len(p.FromFile) != 0 {
p.ReadInputFromFile()
}
return p.RawInput
}
func (p *Puzzle) GetInputArrayInt() []int {
if len(p.RawInputInt) == 0 {
for _, v := range p.GetInputArray() {
value, err := strconv.Atoi(v)
if err != nil {
log.Fatal("Cannot convert string to int:", v)
} else {
p.RawInputInt = append(p.RawInputInt, value)
}
}
}
return p.RawInputInt
}
func (p *Puzzle) GetInput2DArrayInt() [][]int {
var ret [][]int
for _, v := range p.GetInputArray() {
var thisIntLine []int
for _, c := range v {
value, err := strconv.Atoi(string(c))
if err != nil {
log.Fatalf("Cannot convert string to int: (%c in %s)", c, v)
} else {
thisIntLine = append(thisIntLine, value)
}
}
ret = append(ret, thisIntLine)
}
return ret
}
func (p Puzzle) Solve(partFunction func(p Puzzle) interface{}, day int, part int, timeit bool, timeitNumber int) {
var result interface{}
if !timeit {
result = partFunction(p)
fmt.Printf(
"Result for day %d, part %d: %#v\n",
day, part, result,
)
} else {
start := time.Now()
for i := 0; i < timeitNumber; i++ {
result = partFunction(p)
}
elapsed := time.Duration(int64(time.Since(start)) / int64(timeitNumber))
fmt.Printf(
"Result for day %d, part %d: %#v (avg over %d runs: %s)\n",
day, part, result, timeitNumber, elapsed,
)
}
}

View File

@ -1,8 +1,8 @@
package day01
import "aoc2015/aoclib"
import tools "shtools"
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
line := puzzle.GetInputArray()[0]
answer := 0
for _, c := range line {
@ -16,7 +16,7 @@ func Part1(puzzle aoclib.Puzzle) interface{} {
return answer
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
line := puzzle.GetInputArray()[0]
answer := 0
for x, c := range line {

View File

@ -1,12 +1,12 @@
package day02
import (
"aoc2015/aoclib"
tools "shtools"
"strconv"
"strings"
)
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
sqm := 0
for _, line := range puzzle.GetInputArray() {
dim := strings.Split(line, "x")
@ -33,7 +33,7 @@ func Part1(puzzle aoclib.Puzzle) interface{} {
return sqm
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
length := 0
for _, line := range puzzle.GetInputArray() {
dim := strings.Split(line, "x")

View File

@ -1,10 +1,10 @@
package day03
import (
"aoc2015/aoclib"
tools "shtools"
)
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
x, y := 0, 0
houses := make(map[int]map[int]bool)
@ -35,7 +35,7 @@ func Part1(puzzle aoclib.Puzzle) interface{} {
return houseCount
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
x, y := [2]int{0, 0}, [2]int{0, 0}
turn := 0
houses := make(map[int]map[int]bool)

View File

@ -1,9 +1,9 @@
package day04
import (
"aoc2015/aoclib"
"crypto/md5"
"encoding/hex"
tools "shtools"
"strconv"
"strings"
)
@ -25,13 +25,13 @@ func findMd5SumStartsWith(secretStart string, start string) int {
return -1
}
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
secretStart := puzzle.GetInputArray()[0]
return findMd5SumStartsWith(secretStart, "00000")
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
secretStart := puzzle.GetInputArray()[0]
return findMd5SumStartsWith(secretStart, "000000")

View File

@ -1,8 +1,8 @@
package day05
import (
"aoc2015/aoclib"
"bytes"
tools "shtools"
)
var vowels = []byte{'a', 'e', 'i', 'o', 'u'}
@ -57,7 +57,7 @@ func stringIsNiceP2(toTest []byte) bool {
return spacedChar && doubleRepeat
}
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
niceCount := 0
for _, toCheck := range puzzle.GetInputArray() {
if stringIsNiceP1([]byte(toCheck)) {
@ -68,7 +68,7 @@ func Part1(puzzle aoclib.Puzzle) interface{} {
return niceCount
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
niceCount := 0
for _, toCheck := range puzzle.GetInputArray() {
if stringIsNiceP2([]byte(toCheck)) {

View File

@ -1,12 +1,12 @@
package day06
import (
"aoc2015/aoclib"
tools "shtools"
"strconv"
"strings"
)
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
grid := make(map[int]map[int]bool)
for y := 0; y < 1000; y++ {
grid[y] = make(map[int]bool)
@ -65,7 +65,7 @@ func Part1(puzzle aoclib.Puzzle) interface{} {
return onCount
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
grid := make(map[int]map[int]int)
for y := 0; y < 1000; y++ {
grid[y] = make(map[int]int)

View File

@ -1,7 +1,7 @@
package day07
import (
"aoc2015/aoclib"
tools "shtools"
"strconv"
"strings"
)
@ -85,14 +85,14 @@ func calcWires(wires map[string]uint16, input []string, ignoreInputWire string)
return wires
}
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
input := puzzle.GetInputArray()
wires := make(map[string]uint16)
wires = calcWires(wires, input, "")
return int(wires["a"])
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
input := puzzle.GetInputArray()
wires := make(map[string]uint16)
wires = calcWires(wires, input, "")

View File

@ -1,11 +1,11 @@
package day08
import (
"aoc2015/aoclib"
"encoding/hex"
tools "shtools"
)
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
answer := 0
for _, line := range puzzle.GetInputArray() {
@ -33,7 +33,7 @@ func Part1(puzzle aoclib.Puzzle) interface{} {
return answer
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
answer := 0
for _, line := range puzzle.GetInputArray() {

View File

@ -1,8 +1,8 @@
package day09
import (
"aoc2015/aoclib"
"math"
tools "shtools"
"strconv"
"strings"
)
@ -79,14 +79,14 @@ func getMinMaxDistances(permutations [][]string, distances map[string]map[string
return minDistance, maxDistance
}
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
cities, distances := getDistanceMap(puzzle.GetInputArray())
minDistance, _ := getMinMaxDistances(cityPermutations(cities), distances)
return minDistance
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
cities, distances := getDistanceMap(puzzle.GetInputArray())
_, maxDistance := getMinMaxDistances(cityPermutations(cities), distances)

View File

@ -1,7 +1,7 @@
package day10
import (
"aoc2015/aoclib"
tools "shtools"
)
func lookAndSay(look []int) (say []int) {
@ -22,7 +22,7 @@ func lookAndSay(look []int) (say []int) {
return say
}
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
look := puzzle.GetInput2DArrayInt()[0]
for i := 0; i < 40; i++ {
@ -32,7 +32,7 @@ func Part1(puzzle aoclib.Puzzle) interface{} {
return len(look)
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
look := puzzle.GetInput2DArrayInt()[0]
for i := 0; i < 50; i++ {

View File

@ -1,8 +1,8 @@
package day11
import (
"aoc2015/aoclib"
"fmt"
tools "shtools"
)
func incrementPassword(password []byte) {
@ -46,7 +46,7 @@ func checkPasswordValid(password []byte) bool {
return ascendingTripple && doubleDouble
}
func Part1(puzzle aoclib.Puzzle) interface{} {
func Part1(puzzle tools.Puzzle) interface{} {
currentPassword := []byte(puzzle.GetInputArray()[0])
for !checkPasswordValid(currentPassword) {
@ -56,7 +56,7 @@ func Part1(puzzle aoclib.Puzzle) interface{} {
return fmt.Sprintf("%s", string(currentPassword))
}
func Part2(puzzle aoclib.Puzzle) interface{} {
func Part2(puzzle tools.Puzzle) interface{} {
currentPassword := []byte(Part1(puzzle).(string))
incrementPassword(currentPassword)

30
main.go
View File

@ -1,7 +1,6 @@
package main
import (
"aoc2015/aoclib"
"aoc2015/day01"
"aoc2015/day02"
"aoc2015/day03"
@ -17,9 +16,10 @@ import (
"fmt"
"os"
"path/filepath"
tools "shtools"
)
var dayFunctions map[int]map[int]func(puzzle aoclib.Puzzle) interface{}
var dayFunctions map[int]map[int]func(puzzle tools.Puzzle) interface{}
var day int
var part int
var test bool
@ -28,44 +28,44 @@ var timeitNumber int
var myDir, _ = filepath.Abs(filepath.Dir(os.Args[0]))
func initDayFunctions() {
dayFunctions = make(map[int]map[int]func(puzzle aoclib.Puzzle) interface{}, 25)
dayFunctions[1] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions = make(map[int]map[int]func(puzzle tools.Puzzle) interface{}, 25)
dayFunctions[1] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[1][1] = day01.Part1
dayFunctions[1][2] = day01.Part2
dayFunctions[2] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[2] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[2][1] = day02.Part1
dayFunctions[2][2] = day02.Part2
dayFunctions[3] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[3] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[3][1] = day03.Part1
dayFunctions[3][2] = day03.Part2
dayFunctions[4] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[4] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[4][1] = day04.Part1
dayFunctions[4][2] = day04.Part2
dayFunctions[5] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[5] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[5][1] = day05.Part1
dayFunctions[5][2] = day05.Part2
dayFunctions[6] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[6] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[6][1] = day06.Part1
dayFunctions[6][2] = day06.Part2
dayFunctions[7] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[7] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[7][1] = day07.Part1
dayFunctions[7][2] = day07.Part2
dayFunctions[8] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[8] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[8][1] = day08.Part1
dayFunctions[8][2] = day08.Part2
dayFunctions[9] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[9] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[9][1] = day09.Part1
dayFunctions[9][2] = day09.Part2
dayFunctions[10] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[10] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[10][1] = day10.Part1
dayFunctions[10][2] = day10.Part2
dayFunctions[11] = make(map[int]func(puzzle aoclib.Puzzle) interface{}, 2)
dayFunctions[11] = make(map[int]func(puzzle tools.Puzzle) interface{}, 2)
dayFunctions[11][1] = day11.Part1
dayFunctions[11][2] = day11.Part2
}
func execute(thisDay int) {
p := aoclib.Puzzle{}
p := tools.Puzzle{}
if test {
p.FromFile = fmt.Sprintf("%s/inputs/%d_test", myDir, thisDay)
} else {