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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

30
main.go
View File

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