9 Commits bf1bb35406 ... c7d78d16d7

Author SHA1 Message Date
  Piotr Czajkowski c7d78d16d7 Nope 3 days ago
  Piotr Czajkowski c4dab3be9f Added description 3 days ago
  Piotr Czajkowski 7d2b35e949 Probably won't brute force it 3 days ago
  Piotr Czajkowski ce2a8cd9ed Still nothing 3 days ago
  Piotr Czajkowski 77f4232f0d Part2 solved for sample, stuck on input 3 days ago
  Piotr Czajkowski 018afedb75 Preparing for part2 3 days ago
  Piotr Czajkowski 5a370f6b14 Solved part1 3 days ago
  Piotr Czajkowski bdaa683b4a Need better way 3 days ago
  Piotr Czajkowski 435940e878 Able to read input 3 days ago
3 changed files with 233 additions and 0 deletions
  1. 151 0
      19/code.go
  2. 82 0
      19/description.txt
  3. 0 0
      19/input

+ 151 - 0
19/code.go

@@ -0,0 +1,151 @@
+package main
+
+import (
+	"bufio"
+	"fmt"
+	"log"
+	"os"
+	"strings"
+)
+
+const delta byte = 97
+
+func readInput(file *os.File) ([][]string, []string) {
+	scanner := bufio.NewScanner(file)
+	patterns := make([][]string, 25)
+	var towels []string
+
+	var patternsRead bool
+	for scanner.Scan() {
+		line := scanner.Text()
+		if line == "" {
+			patternsRead = true
+			continue
+		}
+
+		if !patternsRead {
+			parts := strings.Split(line, ", ")
+			for _, part := range parts {
+				patterns[part[0]-delta] = append(patterns[part[0]-delta], part)
+			}
+		} else {
+			towels = append(towels, line)
+		}
+	}
+
+	return patterns, towels
+}
+
+func patternAndIndex(pattern string, index int) string {
+	return fmt.Sprintf("%s_%d", pattern, index)
+}
+
+func checkTowel(towel string, index int, patterns [][]string, checked map[string]bool) bool {
+	if index >= len(towel) {
+		return true
+	}
+
+	for _, pattern := range patterns[towel[index]-delta] {
+		if checked[patternAndIndex(pattern, index)] {
+			continue
+		}
+
+		checked[patternAndIndex(pattern, index)] = true
+		patternMatch := true
+		for i := range pattern {
+			if index+i >= len(towel) || pattern[i] != towel[index+i] {
+				patternMatch = false
+				break
+			}
+		}
+
+		if patternMatch && checkTowel(towel, index+len(pattern), patterns, checked) {
+			return true
+		}
+	}
+
+	return false
+}
+
+func getPossibleTowers(patterns [][]string, towels []string) []string {
+	var possible []string
+	for _, towel := range towels {
+		checked := make(map[string]bool)
+		if checkTowel(towel, 0, patterns, checked) {
+			possible = append(possible, towel)
+		}
+	}
+
+	return possible
+}
+
+func checkTowel2(towel string, index int, patterns [][]string, badOnes map[string]bool) int {
+	var count int
+	if index >= len(towel) {
+		return 1
+	}
+
+	edge := len(towel) - 1
+	for _, pattern := range patterns[towel[index]-delta] {
+		if index+len(pattern)-1 > edge {
+			badOnes[patternAndIndex(pattern, index)] = true
+			continue
+		}
+
+		if badOnes[patternAndIndex(pattern, index)] {
+			continue
+		}
+
+		patternMatch := true
+		for i := range pattern {
+			if index+i >= len(towel) || pattern[i] != towel[index+i] {
+				patternMatch = false
+				break
+			}
+		}
+
+		if patternMatch {
+			count += checkTowel2(towel, index+len(pattern), patterns, badOnes)
+		} else {
+			badOnes[patternAndIndex(pattern, index)] = true
+		}
+	}
+
+	return count
+}
+
+func getMax(patterns [][]string) int {
+	var maxPatterns int
+	for _, letter := range patterns {
+		maxPatterns += len(letter)
+	}
+
+	return maxPatterns
+}
+
+func part2(patterns [][]string, towels []string) int {
+	var count int
+	for _, towel := range towels {
+		badOnes := make(map[string]bool)
+		count += checkTowel2(towel, 0, patterns, badOnes)
+	}
+
+	return count
+}
+
+func main() {
+	if len(os.Args) < 2 {
+		log.Fatal("You need to specify a file!")
+	}
+
+	filePath := os.Args[1]
+	file, err := os.Open(filePath)
+	if err != nil {
+		log.Fatalf("Failed to open %s!\n", filePath)
+	}
+
+	patterns, towels := readInput(file)
+	possible := getPossibleTowers(patterns, towels)
+	fmt.Println("Part1:", len(possible))
+	fmt.Println("Part2:", part2(patterns, possible))
+}

+ 82 - 0
19/description.txt

@@ -0,0 +1,82 @@
+--- Day 19: Linen Layout ---
+
+Today, The Historians take you up to the hot springs on Gear Island! Very suspiciously, absolutely nothing goes wrong as they begin their careful search of the vast field of helixes.
+
+Could this finally be your chance to visit the onsen next door? Only one way to find out.
+
+After a brief conversation with the reception staff at the onsen front desk, you discover that you don't have the right kind of money to pay the admission fee. However, before you can leave, the staff get your attention. Apparently, they've heard about how you helped at the hot springs, and they're willing to make a deal: if you can simply help them arrange their towels, they'll let you in for free!
+
+Every towel at this onsen is marked with a pattern of colored stripes. There are only a few patterns, but for any particular pattern, the staff can get you as many towels with that pattern as you need. Each stripe can be white (w), blue (u), black (b), red (r), or green (g). So, a towel with the pattern ggr would have a green stripe, a green stripe, and then a red stripe, in that order. (You can't reverse a pattern by flipping a towel upside-down, as that would cause the onsen logo to face the wrong way.)
+
+The Official Onsen Branding Expert has produced a list of designs - each a long sequence of stripe colors - that they would like to be able to display. You can use any towels you want, but all of the towels' stripes must exactly match the desired design. So, to display the design rgrgr, you could use two rg towels and then an r towel, an rgr towel and then a gr towel, or even a single massive rgrgr towel (assuming such towel patterns were actually available).
+
+To start, collect together all of the available towel patterns and the list of desired designs (your puzzle input). For example:
+
+r, wr, b, g, bwu, rb, gb, br
+
+brwrr
+bggr
+gbbr
+rrbgbr
+ubwu
+bwurrg
+brgr
+bbrgwb
+
+The first line indicates the available towel patterns; in this example, the onsen has unlimited towels with a single red stripe (r), unlimited towels with a white stripe and then a red stripe (wr), and so on.
+
+After the blank line, the remaining lines each describe a design the onsen would like to be able to display. In this example, the first design (brwrr) indicates that the onsen would like to be able to display a black stripe, a red stripe, a white stripe, and then two red stripes, in that order.
+
+Not all designs will be possible with the available towels. In the above example, the designs are possible or impossible as follows:
+
+    brwrr can be made with a br towel, then a wr towel, and then finally an r towel.
+    bggr can be made with a b towel, two g towels, and then an r towel.
+    gbbr can be made with a gb towel and then a br towel.
+    rrbgbr can be made with r, rb, g, and br.
+    ubwu is impossible.
+    bwurrg can be made with bwu, r, r, and g.
+    brgr can be made with br, g, and r.
+    bbrgwb is impossible.
+
+In this example, 6 of the eight designs are possible with the available towel patterns.
+
+To get into the onsen as soon as possible, consult your list of towel patterns and desired designs carefully. How many designs are possible?
+
+Your puzzle answer was 255.
+
+The first half of this puzzle is complete! It provides one gold star: *
+--- Part Two ---
+
+The staff don't really like some of the towel arrangements you came up with. To avoid an endless cycle of towel rearrangement, maybe you should just give them every possible option.
+
+Here are all of the different ways the above example's designs can be made:
+
+brwrr can be made in two different ways: b, r, wr, r or br, wr, r.
+
+bggr can only be made with b, g, g, and r.
+
+gbbr can be made 4 different ways:
+
+    g, b, b, r
+    g, b, br
+    gb, b, r
+    gb, br
+
+rrbgbr can be made 6 different ways:
+
+    r, r, b, g, b, r
+    r, r, b, g, br
+    r, r, b, gb, r
+    r, rb, g, b, r
+    r, rb, g, br
+    r, rb, gb, r
+
+bwurrg can only be made with bwu, r, r, and g.
+
+brgr can be made in two different ways: b, r, g, r or br, g, r.
+
+ubwu and bbrgwb are still impossible.
+
+Adding up all of the ways the towels in this example could be arranged into the desired designs yields 16 (2 + 1 + 4 + 6 + 1 + 2).
+
+They'll let you into the onsen as soon as you have the list. What do you get if you add up the number of different ways you could make each design?

File diff suppressed because it is too large
+ 0 - 0
19/input


Some files were not shown because too many files changed in this diff