Day 15 (Golang)

This commit is contained in:
akp 2020-12-15 20:11:08 +00:00
parent 1962d90d22
commit fb867cec67
No known key found for this signature in database
GPG key ID: D3E7EAA31B39637E
5 changed files with 174 additions and 0 deletions

View file

@ -20,6 +20,23 @@ Test cases
1.6 pass
2.1 pass
Answers
Part 1: 206
Part 2: 955
go run .\go\
AoC 2020: day 15 - Rambunctious Recitation
Go go1.15.2
Test cases
1.1 pass
1.2 pass
1.3 pass
1.4 pass
1.5 pass
1.6 pass
2.1 pass
Answers
Part 1: 206
Part 2: 955

View file

@ -0,0 +1,47 @@
package challenge
import (
"strconv"
"strings"
)
func findValueN(instr string, threshold int) int {
var inp []int
for _, x := range strings.Split(strings.TrimSpace(instr), ",") {
i, err := strconv.Atoi(x)
if err != nil {
panic(err)
}
inp = append(inp, i)
}
indexes := make(map[int]int)
for i, n := range inp {
indexes[n] = i
}
for len(inp) < threshold {
c := len(inp) - 1
previousNumber := inp[c]
var previousOccuranceIndex int
val, found := indexes[previousNumber]
if !found {
previousOccuranceIndex = -1
} else {
previousOccuranceIndex = val
}
var newNumber int
if previousOccuranceIndex != -1 {
newNumber = c - previousOccuranceIndex
}
indexes[previousNumber] = c
inp = append(inp, newNumber)
}
return inp[len(inp)-1]
}

View file

@ -0,0 +1,5 @@
package challenge
func PartOne(instr string) int {
return findValueN(instr, 2020)
}

View file

@ -0,0 +1,5 @@
package challenge
func PartTwo(instr string) int {
return findValueN(instr, 30000000)
}

View file

@ -0,0 +1,100 @@
package main
import (
"adventOfCode/15-rambunctiousRecitation/go/challenge"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"github.com/fatih/color"
)
func main() {
var infoStruct info
{
inb, err := ioutil.ReadFile("info.json")
if err != nil {
fmt.Println("Error: could not open info.json")
os.Exit(-1)
}
err = json.Unmarshal(inb, &infoStruct)
if err != nil {
fmt.Println("Error: could not parse info.json")
os.Exit(-1)
}
}
var (
year = infoStruct.Year
day = infoStruct.Day
title = infoStruct.Title
)
fmt.Fprintf(color.Output, "%s: day %s - %s\n", color.YellowString("AoC "+year), color.BlueString(day), title)
fmt.Fprintf(color.Output, "Go %s\n\n", color.BlueString(runtime.Version()))
var challengeInput string
{
inb, err := ioutil.ReadFile("input.txt")
if err != nil {
fmt.Println("Error: could not open input.txt")
os.Exit(-1)
}
challengeInput = string(inb)
}
runTests(infoStruct)
fmt.Println("Answers")
fmt.Fprintf(color.Output, "Part %s: %s\n", color.BlueString("1"), color.BlueString(strconv.Itoa(challenge.PartOne(challengeInput))))
fmt.Fprintf(color.Output, "Part %s: %s\n", color.BlueString("2"), color.BlueString(strconv.Itoa(challenge.PartTwo(challengeInput))))
}
type tc struct {
Input string `json:"input"`
Expected int `json:"expected"`
}
type info struct {
Year string `json:"year"`
Day string `json:"day"`
Title string `json:"title"`
TestCases struct {
One []tc `json:"one"`
Two []tc `json:"two"`
} `json:"testCases"`
}
func runTests(infoStruct info) {
if len(infoStruct.TestCases.One) == 0 && len(infoStruct.TestCases.Two) == 0 {
fmt.Println("Info: no test cases specified. Skipping tests")
fmt.Println()
return
}
fmt.Println("Test cases")
rt := func(tcs []tc, f func(string) int, n string) {
for i, tc := range tcs {
fmt.Fprintf(color.Output, "%s ", color.BlueString(n+"."+strconv.Itoa(i+1)))
result := f(tc.Input)
if result == tc.Expected {
fmt.Fprintf(color.Output, "%s", color.GreenString("pass"))
} else {
fmt.Fprintf(color.Output, "%s (got %s, expected %s)", color.RedString("fail"), color.BlueString(strconv.Itoa(result)), color.BlueString(strconv.Itoa(tc.Expected)))
}
fmt.Println()
}
}
rt(infoStruct.TestCases.One, challenge.PartOne, "1")
rt(infoStruct.TestCases.Two, challenge.PartTwo, "2")
fmt.Println()
}