diff --git a/aoclib/aoclib.go b/aoclib/aoclib.go deleted file mode 100644 index 71dcf31..0000000 --- a/aoclib/aoclib.go +++ /dev/null @@ -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, - ) - } - -} diff --git a/day01/day01.go b/day01/day01.go index 73bc0a8..dc4b01c 100644 --- a/day01/day01.go +++ b/day01/day01.go @@ -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 { diff --git a/day02/day02.go b/day02/day02.go index 0d09be2..c378f5a 100644 --- a/day02/day02.go +++ b/day02/day02.go @@ -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") diff --git a/day03/day03.go b/day03/day03.go index 87f65b7..f807d72 100644 --- a/day03/day03.go +++ b/day03/day03.go @@ -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) diff --git a/day04/day04.go b/day04/day04.go index 36ac4f9..5745bf7 100644 --- a/day04/day04.go +++ b/day04/day04.go @@ -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") diff --git a/day05/day05.go b/day05/day05.go index b3ff8b1..88527f7 100644 --- a/day05/day05.go +++ b/day05/day05.go @@ -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)) { diff --git a/day06/day06.go b/day06/day06.go index c022d39..18da9a1 100644 --- a/day06/day06.go +++ b/day06/day06.go @@ -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) diff --git a/day07/day07.go b/day07/day07.go index 23d26b6..b2773d1 100644 --- a/day07/day07.go +++ b/day07/day07.go @@ -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, "") diff --git a/day08/day08.go b/day08/day08.go index 08c5e59..5006204 100644 --- a/day08/day08.go +++ b/day08/day08.go @@ -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() { diff --git a/day09/day09.go b/day09/day09.go index 51b7ef0..7b8ceb0 100644 --- a/day09/day09.go +++ b/day09/day09.go @@ -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) diff --git a/day10/day10.go b/day10/day10.go index f554dc8..63514ca 100644 --- a/day10/day10.go +++ b/day10/day10.go @@ -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++ { diff --git a/day11/day11.go b/day11/day11.go index 3ff1822..ded9579 100644 --- a/day11/day11.go +++ b/day11/day11.go @@ -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) diff --git a/main.go b/main.go index b4e5fd8..ded0cc7 100644 --- a/main.go +++ b/main.go @@ -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 {