chore: fix tests for day 4 & 5
This commit is contained in:
		
							parent
							
								
									fa2e7fedd9
								
							
						
					
					
						commit
						eca3745772
					
				@ -17,16 +17,18 @@ func TestExample(t *testing.T) {
 | 
				
			|||||||
	is := is.New(t)
 | 
						is := is.New(t)
 | 
				
			||||||
	scan := bufio.NewScanner(bytes.NewReader(example))
 | 
						scan := bufio.NewScanner(bytes.NewReader(example))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	points, cards := run(scan)
 | 
						r, err := run(scan)
 | 
				
			||||||
	is.Equal(points, 13)
 | 
						is.NoErr(err)
 | 
				
			||||||
	is.Equal(cards, 30)
 | 
						is.Equal(r.points, 13)
 | 
				
			||||||
 | 
						is.Equal(r.cards, 30)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestSolution(t *testing.T) {
 | 
					func TestSolution(t *testing.T) {
 | 
				
			||||||
	is := is.New(t)
 | 
						is := is.New(t)
 | 
				
			||||||
	scan := bufio.NewScanner(bytes.NewReader(input))
 | 
						scan := bufio.NewScanner(bytes.NewReader(input))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	points, cards := run(scan)
 | 
						r, err := run(scan)
 | 
				
			||||||
	is.Equal(points, 23235)
 | 
						is.NoErr(err)
 | 
				
			||||||
	is.Equal(cards, 5920640)
 | 
						is.Equal(r.points, 23235)
 | 
				
			||||||
 | 
						is.Equal(r.cards, 5920640)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -20,19 +20,20 @@ func TestExample(t *testing.T) {
 | 
				
			|||||||
	is := is.New(t)
 | 
						is := is.New(t)
 | 
				
			||||||
	scan := bufio.NewScanner(bytes.NewReader(example))
 | 
						scan := bufio.NewScanner(bytes.NewReader(example))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	minLocation, minRangeLocation := run(scan)
 | 
						r, err := run(scan)
 | 
				
			||||||
	is.Equal(minLocation, 35)
 | 
						is.NoErr(err)
 | 
				
			||||||
	is.Equal(minRangeLocation, 46)
 | 
						is.Equal(r.minLocation, 35)
 | 
				
			||||||
 | 
						is.Equal(r.minRange, 46)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func SkipTestSolution(t *testing.T) {
 | 
					func SkipTestSolution(t *testing.T) {
 | 
				
			||||||
	is := is.New(t)
 | 
						is := is.New(t)
 | 
				
			||||||
	scan := bufio.NewScanner(bytes.NewReader(input))
 | 
						scan := bufio.NewScanner(bytes.NewReader(input))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	minLocation, minRangeLocation := run(scan)
 | 
						r, err := run(scan)
 | 
				
			||||||
	is.Equal(minLocation, 199602917)
 | 
						is.NoErr(err)
 | 
				
			||||||
	is.Equal(minRangeLocation, 0)
 | 
						is.Equal(r.minLocation, 199602917)
 | 
				
			||||||
 | 
						is.Equal(r.minRange, 0)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestLookup(t *testing.T) {
 | 
					func TestLookup(t *testing.T) {
 | 
				
			||||||
@ -44,9 +45,9 @@ func TestLookup(t *testing.T) {
 | 
				
			|||||||
	is.Equal(find.Find(79), 81)
 | 
						is.Equal(find.Find(79), 81)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	find = &Lookup{ranges: Ranges{
 | 
						find = &Lookup{ranges: Ranges{
 | 
				
			||||||
		{77,45,23},
 | 
							{77, 45, 23},
 | 
				
			||||||
		{45,81,19},
 | 
							{45, 81, 19},
 | 
				
			||||||
		{64,68,13},
 | 
							{64, 68, 13},
 | 
				
			||||||
	}}
 | 
						}}
 | 
				
			||||||
	is.Equal(find.Find(77), 45)
 | 
						is.Equal(find.Find(77), 45)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -62,37 +63,37 @@ func TestFinder(t *testing.T) {
 | 
				
			|||||||
		}},
 | 
							}},
 | 
				
			||||||
		// soil-to-fertilizer
 | 
							// soil-to-fertilizer
 | 
				
			||||||
		&Lookup{ranges: Ranges{
 | 
							&Lookup{ranges: Ranges{
 | 
				
			||||||
			{15, 0,37},
 | 
								{15, 0, 37},
 | 
				
			||||||
			{52,37,2},
 | 
								{52, 37, 2},
 | 
				
			||||||
			{0,39,15},
 | 
								{0, 39, 15},
 | 
				
			||||||
		}},
 | 
							}},
 | 
				
			||||||
		// fertilizer-to-water
 | 
							// fertilizer-to-water
 | 
				
			||||||
		&Lookup{ranges: Ranges{
 | 
							&Lookup{ranges: Ranges{
 | 
				
			||||||
			{53,49,8},
 | 
								{53, 49, 8},
 | 
				
			||||||
			{11,0,42},
 | 
								{11, 0, 42},
 | 
				
			||||||
			{0,42,7},
 | 
								{0, 42, 7},
 | 
				
			||||||
			{7,57,4},
 | 
								{7, 57, 4},
 | 
				
			||||||
		}},
 | 
							}},
 | 
				
			||||||
		// water-to-light
 | 
							// water-to-light
 | 
				
			||||||
		&Lookup{ranges: Ranges{
 | 
							&Lookup{ranges: Ranges{
 | 
				
			||||||
			{18,88,7},
 | 
								{18, 88, 7},
 | 
				
			||||||
			{25,18,70},
 | 
								{25, 18, 70},
 | 
				
			||||||
		}},
 | 
							}},
 | 
				
			||||||
		// light-to-temperature
 | 
							// light-to-temperature
 | 
				
			||||||
		&Lookup{ranges: Ranges{
 | 
							&Lookup{ranges: Ranges{
 | 
				
			||||||
			{77,45,23},
 | 
								{77, 45, 23},
 | 
				
			||||||
			{45,81,19},
 | 
								{45, 81, 19},
 | 
				
			||||||
			{64,68,13},
 | 
								{64, 68, 13},
 | 
				
			||||||
		}},
 | 
							}},
 | 
				
			||||||
		// temperature-to-humidity
 | 
							// temperature-to-humidity
 | 
				
			||||||
		&Lookup{ranges: Ranges{
 | 
							&Lookup{ranges: Ranges{
 | 
				
			||||||
			{69,0,1},
 | 
								{69, 0, 1},
 | 
				
			||||||
			{0,1,69},
 | 
								{0, 1, 69},
 | 
				
			||||||
		}},
 | 
							}},
 | 
				
			||||||
		// humidity-to-location
 | 
							// humidity-to-location
 | 
				
			||||||
		&Lookup{ranges: Ranges{
 | 
							&Lookup{ranges: Ranges{
 | 
				
			||||||
			{56,60,37},
 | 
								{56, 60, 37},
 | 
				
			||||||
			{93,56,4},
 | 
								{93, 56, 4},
 | 
				
			||||||
		}},
 | 
							}},
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	is.Equal(find.Find(82), 46)
 | 
						is.Equal(find.Find(82), 46)
 | 
				
			||||||
 | 
				
			|||||||
@ -23,6 +23,7 @@ func (r result) String() string { return fmt.Sprintf("%#v", r) }
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
func run(scan *bufio.Scanner) (*result, error) {
 | 
					func run(scan *bufio.Scanner) (*result, error) {
 | 
				
			||||||
	var matches []int
 | 
						var matches []int
 | 
				
			||||||
 | 
						var matches2 []int
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for scan.Scan() {
 | 
						for scan.Scan() {
 | 
				
			||||||
		text := scan.Text()
 | 
							text := scan.Text()
 | 
				
			||||||
@ -31,16 +32,24 @@ func run(scan *bufio.Scanner) (*result, error) {
 | 
				
			|||||||
			continue
 | 
								continue
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// part 1 - brute force
 | 
				
			||||||
		grouping := aoc.SliceMap(aoc.Atoi, strings.Split(text, ",")...)
 | 
							grouping := aoc.SliceMap(aoc.Atoi, strings.Split(text, ",")...)
 | 
				
			||||||
		pattern := []rune(status)
 | 
							pattern := []rune(status)
 | 
				
			||||||
		missing := countQuestion(pattern)
 | 
							// sp := spring{pattern: pattern, grouping: grouping, missingNo: countQuestion(pattern)}
 | 
				
			||||||
 | 
							// matches = append(matches, sp.findMatches())
 | 
				
			||||||
 | 
							matches = append(matches, countPossible(pattern, grouping))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		s := spring{pattern: pattern, grouping: grouping, missingNo: missing}
 | 
							// part 2 - NFA
 | 
				
			||||||
 | 
							b, a := status, text
 | 
				
			||||||
		matches = append(matches, s.findMatches())
 | 
							bn, an := "", ""
 | 
				
			||||||
 | 
							for i := 0; i < 5; i++ {
 | 
				
			||||||
 | 
								bn, an = bn+b+"?", an+a+","
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							b, a = strings.TrimSuffix(bn, "?"), strings.TrimSuffix(an, ",")
 | 
				
			||||||
 | 
							matches2 = append(matches2, countPossible([]rune(b), aoc.SliceMap(aoc.Atoi, strings.Split(a, ",")...)))
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &result{valuePT1: aoc.Sum(matches...)}, nil
 | 
						return &result{valuePT1: aoc.Sum(matches...), valuePT2: aoc.Sum(matches2...)}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type spring struct {
 | 
					type spring struct {
 | 
				
			||||||
@ -89,6 +98,63 @@ func (s *spring) genPatterns() []string {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	return lis
 | 
						return lis
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					func countPossible(s []rune, c []int) int {
 | 
				
			||||||
 | 
						pos := 0
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						cstates := map[state]int{{}: 1} // current state
 | 
				
			||||||
 | 
						nstates := map[state]int{}      // next state
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for len(cstates) > 0 {
 | 
				
			||||||
 | 
							for st, num := range cstates {
 | 
				
			||||||
 | 
								si, ci, cc, expdot := st.springIndex, st.groupIndex, st.continuous, st.expectDot
 | 
				
			||||||
 | 
								
 | 
				
			||||||
 | 
								// have we reached the end?
 | 
				
			||||||
 | 
								if si == len(s) {
 | 
				
			||||||
 | 
									if ci == len(c) {
 | 
				
			||||||
 | 
										pos += num
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								switch {
 | 
				
			||||||
 | 
								case (s[si] == '#' || s[si] == '?') && ci < len(c) && !expdot:
 | 
				
			||||||
 | 
									// we are still looking for broken springs
 | 
				
			||||||
 | 
									if s[si] == '?' && cc == 0 {
 | 
				
			||||||
 | 
										// we are not in a run of broken springs, so ? can be working
 | 
				
			||||||
 | 
										nstates[state{si + 1, ci, cc, expdot}] += num
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									cc++
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									if cc == c[ci] {
 | 
				
			||||||
 | 
										// we've found the full next contiguous section of broken springs
 | 
				
			||||||
 | 
										ci++
 | 
				
			||||||
 | 
										cc = 0
 | 
				
			||||||
 | 
										expdot = true // we only want a working spring next
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									nstates[state{si + 1, ci, cc, expdot}] += num
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								case (s[si] == '.' || s[si] == '?') && cc == 0:
 | 
				
			||||||
 | 
									// we are not in a contiguous run of broken springs
 | 
				
			||||||
 | 
									expdot = false
 | 
				
			||||||
 | 
									nstates[state{si + 1, ci, cc, expdot}] += num
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// swap and clear previous states
 | 
				
			||||||
 | 
							cstates, nstates = nstates, cstates
 | 
				
			||||||
 | 
							clear(nstates)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return pos
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type state struct {
 | 
				
			||||||
 | 
						springIndex int
 | 
				
			||||||
 | 
						groupIndex  int
 | 
				
			||||||
 | 
						continuous  int
 | 
				
			||||||
 | 
						expectDot   bool
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func countQuestion(pattern []rune) int {
 | 
					func countQuestion(pattern []rune) int {
 | 
				
			||||||
	count := 0
 | 
						count := 0
 | 
				
			||||||
@ -119,4 +185,3 @@ func countGroupings(pattern []rune) []int {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return groupings
 | 
						return groupings
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -27,7 +27,7 @@ func TestExample(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	t.Log(result)
 | 
						t.Log(result)
 | 
				
			||||||
	is.Equal(result.valuePT1, 21)
 | 
						is.Equal(result.valuePT1, 21)
 | 
				
			||||||
	is.Equal(result.valuePT2, 0)
 | 
						is.Equal(result.valuePT2, 525152)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestSolution(t *testing.T) {
 | 
					func TestSolution(t *testing.T) {
 | 
				
			||||||
@ -39,7 +39,7 @@ func TestSolution(t *testing.T) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	t.Log(result)
 | 
						t.Log(result)
 | 
				
			||||||
	is.Equal(result.valuePT1, 8193)
 | 
						is.Equal(result.valuePT1, 8193)
 | 
				
			||||||
	is.Equal(result.valuePT2, 0)
 | 
						is.Equal(result.valuePT2, 45322533163795)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestPower2(t *testing.T) {
 | 
					func TestPower2(t *testing.T) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user