mirror of
https://github.com/Threnklyn/advent-of-code-go.git
synced 2026-06-07 12:45:10 +02:00
2020-day21: so close! 225/127, running joins to find allergens in a list of hashed names
This commit is contained in:
+98
-20
@@ -3,9 +3,9 @@ package main
|
|||||||
import (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/alexchao26/advent-of-code-go/mathutil"
|
|
||||||
"github.com/alexchao26/advent-of-code-go/util"
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,32 +15,110 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
fmt.Println("Running part", part)
|
fmt.Println("Running part", part)
|
||||||
|
|
||||||
|
part1Ans, part2Ans := allergenAssessment(util.ReadFile("./input.txt"))
|
||||||
if part == 1 {
|
if part == 1 {
|
||||||
ans := part1(util.ReadFile("./input.txt"))
|
fmt.Println("Output:", part1Ans)
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
|
||||||
fmt.Println("Output:", ans)
|
|
||||||
} else {
|
} else {
|
||||||
ans := part2(util.ReadFile("./input.txt"))
|
fmt.Println("Output:", part2Ans)
|
||||||
util.CopyToClipboard(fmt.Sprintf("%v", ans))
|
|
||||||
fmt.Println("Output:", ans)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func part1(input string) int {
|
// leaderboard: 225/127, closest yet!
|
||||||
parsed := parseInput(input)
|
func allergenAssessment(input string) (part1Ans int, part2Ans string) {
|
||||||
_ = parsed
|
allergensToPossibleIngredients := map[string][]string{}
|
||||||
|
ingredientCounts := map[string]int{}
|
||||||
|
|
||||||
return 0
|
for _, line := range strings.Split(input, "\n") {
|
||||||
}
|
parts := strings.Split(line, " (contains ")
|
||||||
|
ingredients := strings.Split(parts[0], " ")
|
||||||
|
allergens := strings.Split(strings.Trim(parts[1], ")"), ", ")
|
||||||
|
|
||||||
func part2(input string) int {
|
// count up the appearances for each ingredient
|
||||||
return 0
|
for _, ingred := range ingredients {
|
||||||
}
|
ingredientCounts[ingred]++
|
||||||
|
}
|
||||||
|
|
||||||
func parseInput(input string) (ans []int) {
|
// generate all possible ingredients that could be a particular allergen
|
||||||
lines := strings.Split(input, "\n")
|
for _, a := range allergens {
|
||||||
for _, l := range lines {
|
// if no ingredients are there, set this as the initial list
|
||||||
ans = append(ans, mathutil.StrToInt(l))
|
if allergensToPossibleIngredients[a] == nil {
|
||||||
|
allergensToPossibleIngredients[a] = ingredients
|
||||||
|
} else {
|
||||||
|
// otherwise take the inner join/overlap to eliminate ingredients
|
||||||
|
allergensToPossibleIngredients[a] = innerJoin(allergensToPossibleIngredients[a], ingredients)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return ans
|
|
||||||
|
// iterate through the allergens to possible map and if a slice of length 1
|
||||||
|
// is found, remove that ingredient from all other value slices
|
||||||
|
// do this until every slice has only one possible ingredient
|
||||||
|
for {
|
||||||
|
allSingle := true
|
||||||
|
for allergen, possible := range allergensToPossibleIngredients {
|
||||||
|
if len(possible) != 1 {
|
||||||
|
allSingle = false
|
||||||
|
} else {
|
||||||
|
// remove this name from all lists
|
||||||
|
for otherAllergen, otherIngredients := range allergensToPossibleIngredients {
|
||||||
|
if otherAllergen != allergen {
|
||||||
|
allergensToPossibleIngredients[otherAllergen] = removeFromSlice(otherIngredients, possible[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if allSingle {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove the allergens from the ingredientsCount map
|
||||||
|
for _, hashedName := range allergensToPossibleIngredients {
|
||||||
|
delete(ingredientCounts, hashedName[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// for part 1: count up the total occurrences of non-allergen ingredients
|
||||||
|
var count int
|
||||||
|
for _, ct := range ingredientCounts {
|
||||||
|
count += ct
|
||||||
|
}
|
||||||
|
|
||||||
|
// for part 2: get a list of all allergens, sort them, then in order, add the
|
||||||
|
// hashed name to a canonical dangerous list
|
||||||
|
var names []string
|
||||||
|
for k := range allergensToPossibleIngredients {
|
||||||
|
names = append(names, k)
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
|
||||||
|
var canonical []string
|
||||||
|
for _, n := range names {
|
||||||
|
canonical = append(canonical, allergensToPossibleIngredients[n][0])
|
||||||
|
}
|
||||||
|
|
||||||
|
return count, strings.Join(canonical, ",")
|
||||||
|
}
|
||||||
|
|
||||||
|
func innerJoin(set1, set2 []string) []string {
|
||||||
|
map1 := map[string]bool{}
|
||||||
|
for _, v := range set1 {
|
||||||
|
map1[v] = true
|
||||||
|
}
|
||||||
|
|
||||||
|
var unionSet []string
|
||||||
|
for _, v := range set2 {
|
||||||
|
if map1[v] {
|
||||||
|
unionSet = append(unionSet, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return unionSet
|
||||||
|
}
|
||||||
|
func removeFromSlice(sli []string, strToFind string) []string {
|
||||||
|
for i, v := range sli {
|
||||||
|
if v == strToFind {
|
||||||
|
sli[i] = sli[len(sli)-1]
|
||||||
|
sli = sli[:len(sli)-1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sli
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-24
@@ -2,37 +2,27 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/alexchao26/advent-of-code-go/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Test_part1(t *testing.T) {
|
func Test_allergenAssessment(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
want int
|
wantPart1 int
|
||||||
|
wantPart2 string
|
||||||
}{
|
}{
|
||||||
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER},
|
{"actual", util.ReadFile("input.txt"), 1815, "kllgt,jrnqx,ljvx,zxstb,gnbxs,mhtc,hfdxb,hbfnkq"},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
if got := part1(tt.input); got != tt.want {
|
gotPart1, gotPart2 := allergenAssessment(tt.input)
|
||||||
t.Errorf("part1() = %v, want %v", got, tt.want)
|
if gotPart1 != tt.wantPart1 {
|
||||||
}
|
t.Errorf("allergenAssessment() = %v, want %v", gotPart1, tt.wantPart1)
|
||||||
})
|
}
|
||||||
}
|
if gotPart2 != tt.wantPart2 {
|
||||||
}
|
t.Errorf("allergenAssessment() = %v, want %v", gotPart2, tt.wantPart2)
|
||||||
|
|
||||||
func Test_part2(t *testing.T) {
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
input string
|
|
||||||
want int
|
|
||||||
}{
|
|
||||||
// {"actual", util.ReadFile("input.txt"), ACTUAL_ANSWER},
|
|
||||||
}
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
if got := part2(tt.input); got != tt.want {
|
|
||||||
t.Errorf("part2() = %v, want %v", got, tt.want)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 185 KiB |
Reference in New Issue
Block a user