Day 6 (Golang)
This commit is contained in:
parent
bf3bdc2302
commit
bd2e041bd7
6 changed files with 244 additions and 28 deletions
58
.github/README.md
vendored
58
.github/README.md
vendored
|
@ -7,36 +7,38 @@ Go solutions are near direct copies of the Python solutions and may be added a f
|
|||
**Key:** ![Completed][check] is completed, ![Incomplete][cross] is incomplete, ![Partially complete][partial] is partially complete (does not have both languages) and ![Not yet attempted][pending] is released but not yet attempted.
|
||||
|
||||
<!-- PARSE START -->
|
||||
| Day | | Python | Go |
|
||||
|-----|---------------------|--------|----|
|
||||
| 1 | ![Completed][check] | [Link](/01-reportRepair/python) | [Link](/01-reportRepair/go) |
|
||||
| 2 | ![Completed][check] | [Link](/02-passwordPhilosophy/python) | [Link](/02-passwordPhilosophy/go) |
|
||||
| 3 | ![Completed][check] | [Link](/03-tobogganTrajectory/python) | [Link](/03-tobogganTrajectory/go) |
|
||||
| 4 | ![Completed][check] | [Link](/04-passportProcessing/python) | [Link](/04-passportProcessing/go) |
|
||||
| 5 | ![Completed][check] | [Link](/05-binaryBoarding/python) | [Link](/05-binaryBoarding/go) |
|
||||
| 6 | ![Partially complete][partial] | [Link](/06-customCustoms/python) | |
|
||||
| 7 | | | |
|
||||
| 8 | | | |
|
||||
| 9 | | | |
|
||||
| 10 | | | |
|
||||
| 11 | | | |
|
||||
| 12 | | | |
|
||||
| 13 | | | |
|
||||
| 14 | | | |
|
||||
| 15 | | | |
|
||||
| 16 | | | |
|
||||
| 17 | | | |
|
||||
| 18 | | | |
|
||||
| 19 | | | |
|
||||
| 20 | | | |
|
||||
| 21 | | | |
|
||||
| 22 | | | |
|
||||
| 23 | | | |
|
||||
| 24 | | | |
|
||||
| 25 | | | |
|
||||
|
||||
| Day | | Python | Go |
|
||||
| --------------------------- | ------------------- | ------------------------------------- | --------------------------------- |
|
||||
| [1](/01-reportRepair) | ![Completed][check] | [Link](/01-reportRepair/python) | [Link](/01-reportRepair/go) |
|
||||
| [2](/02-passwordPhilosophy) | ![Completed][check] | [Link](/02-passwordPhilosophy/python) | [Link](/02-passwordPhilosophy/go) |
|
||||
| [3](/03-tobogganTrajectory) | ![Completed][check] | [Link](/03-tobogganTrajectory/python) | [Link](/03-tobogganTrajectory/go) |
|
||||
| [4](/04-passportProcessing) | ![Completed][check] | [Link](/04-passportProcessing/python) | [Link](/04-passportProcessing/go) |
|
||||
| [5](/05-binaryBoarding) | ![Completed][check] | [Link](/05-binaryBoarding/python) | [Link](/05-binaryBoarding/go) |
|
||||
| [6](/06-customCustoms) | ![Completed][check] | [Link](/06-customCustoms/python) | [Link](/06-customCustoms/go) |
|
||||
| 7 | | | |
|
||||
| 8 | | | |
|
||||
| 9 | | | |
|
||||
| 10 | | | |
|
||||
| 11 | | | |
|
||||
| 12 | | | |
|
||||
| 13 | | | |
|
||||
| 14 | | | |
|
||||
| 15 | | | |
|
||||
| 16 | | | |
|
||||
| 17 | | | |
|
||||
| 18 | | | |
|
||||
| 19 | | | |
|
||||
| 20 | | | |
|
||||
| 21 | | | |
|
||||
| 22 | | | |
|
||||
| 23 | | | |
|
||||
| 24 | | | |
|
||||
| 25 | | | |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
[check]: https://github.com/codemicro/adventOfCode/blob/master/.github/check.jpg?raw=true
|
||||
[cross]: https://github.com/codemicro/adventOfCode/blob/master/.github/cross.jpg?raw=true
|
||||
[partial]: https://github.com/codemicro/adventOfCode/blob/master/.github/partial.jpg?raw=true
|
||||
[pending]: https://github.com/codemicro/adventOfCode/blob/master/.github/asterisk.jpg?raw=true
|
||||
[pending]: https://github.com/codemicro/adventOfCode/blob/master/.github/asterisk.jpg?raw=true
|
||||
|
|
|
@ -105,6 +105,18 @@ Test cases
|
|||
1.1 pass
|
||||
2.1 pass
|
||||
|
||||
Answers
|
||||
Part 1: 6310
|
||||
Part 2: 3193
|
||||
|
||||
❯ go run .\go\
|
||||
AoC 2020: day 6 - Custom Customs
|
||||
Go go1.15.2
|
||||
|
||||
Test cases
|
||||
1.1 pass
|
||||
2.1 pass
|
||||
|
||||
Answers
|
||||
Part 1: 6310
|
||||
Part 2: 3193
|
||||
|
|
21
06-customCustoms/go/challenge/common.go
Normal file
21
06-customCustoms/go/challenge/common.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package challenge
|
||||
|
||||
import "strings"
|
||||
|
||||
func parse(instr string) []string {
|
||||
return strings.Split(strings.TrimSpace(instr), "\n\n")
|
||||
}
|
||||
|
||||
type Group struct {
|
||||
Questions []rune
|
||||
NumPax int
|
||||
}
|
||||
|
||||
func IsRuneInSlice(r rune, s []rune) bool {
|
||||
for _, v := range s {
|
||||
if v == r {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
30
06-customCustoms/go/challenge/partOne.go
Normal file
30
06-customCustoms/go/challenge/partOne.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package challenge
|
||||
|
||||
import "strings"
|
||||
|
||||
func PartOne(instr string) int {
|
||||
newGroup := func(instr string) Group {
|
||||
g := Group{}
|
||||
|
||||
individualPax := strings.Split(instr, "\n")
|
||||
g.NumPax = len(individualPax)
|
||||
|
||||
for _, pax := range individualPax {
|
||||
for _, char := range pax {
|
||||
if !IsRuneInSlice(char, g.Questions) {
|
||||
g.Questions = append(g.Questions, char)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
var questionTotal int
|
||||
|
||||
for _, x := range parse(instr) {
|
||||
questionTotal += len(newGroup(x).Questions)
|
||||
}
|
||||
|
||||
return questionTotal
|
||||
}
|
51
06-customCustoms/go/challenge/partTwo.go
Normal file
51
06-customCustoms/go/challenge/partTwo.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package challenge
|
||||
|
||||
import "strings"
|
||||
|
||||
func PartTwo(instr string) int {
|
||||
|
||||
checkChar := func(aq map[int][]rune, char rune) (isInAll bool) {
|
||||
|
||||
isInAll = true
|
||||
for _, val := range aq {
|
||||
if !IsRuneInSlice(char, val) {
|
||||
isInAll = false
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
newGroup := func(instr string) Group {
|
||||
g := Group{}
|
||||
|
||||
individualPax := strings.Split(instr, "\n")
|
||||
g.NumPax = len(individualPax)
|
||||
|
||||
paxQuestions := make(map[int][]rune)
|
||||
for i, pax := range individualPax {
|
||||
paxQuestions[i] = []rune(pax)
|
||||
}
|
||||
|
||||
for _, val := range paxQuestions {
|
||||
for _, char := range val {
|
||||
if checkChar(paxQuestions, char) {
|
||||
if !IsRuneInSlice(char, g.Questions) {
|
||||
g.Questions = append(g.Questions, char)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return g
|
||||
}
|
||||
|
||||
var questionTotal int
|
||||
|
||||
for _, x := range parse(instr) {
|
||||
questionTotal += len(newGroup(x).Questions)
|
||||
}
|
||||
|
||||
return questionTotal
|
||||
}
|
100
06-customCustoms/go/main.go
Normal file
100
06-customCustoms/go/main.go
Normal file
|
@ -0,0 +1,100 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"adventOfCode/06-customCustoms/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()
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue