Day 6 solution
This commit is contained in:
10
README.md
10
README.md
@@ -65,3 +65,13 @@ There are 5585 overlapping vents
|
|||||||
There are 17193 overlapping vents
|
There are 17193 overlapping vents
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Day six
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ ./aoc2021 --six
|
||||||
|
The solution for "six" is:
|
||||||
|
After 80 days there are 377263 fish
|
||||||
|
After 256 days there are 1695929023803 fish
|
||||||
|
```
|
||||||
|
|
||||||
|
3
main.go
3
main.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"unsupervised.ca/aoc2021/five"
|
"unsupervised.ca/aoc2021/five"
|
||||||
"unsupervised.ca/aoc2021/four"
|
"unsupervised.ca/aoc2021/four"
|
||||||
"unsupervised.ca/aoc2021/one"
|
"unsupervised.ca/aoc2021/one"
|
||||||
|
"unsupervised.ca/aoc2021/six"
|
||||||
"unsupervised.ca/aoc2021/three"
|
"unsupervised.ca/aoc2021/three"
|
||||||
"unsupervised.ca/aoc2021/two"
|
"unsupervised.ca/aoc2021/two"
|
||||||
)
|
)
|
||||||
@@ -37,6 +38,8 @@ func main() {
|
|||||||
day = four.Init("four/input.txt")
|
day = four.Init("four/input.txt")
|
||||||
case "five":
|
case "five":
|
||||||
day = five.Init("five/input.txt")
|
day = five.Init("five/input.txt")
|
||||||
|
case "six":
|
||||||
|
day = six.Init("six/input.txt")
|
||||||
default:
|
default:
|
||||||
fmt.Printf("%q does not have a solution.\n", flagParts[1])
|
fmt.Printf("%q does not have a solution.\n", flagParts[1])
|
||||||
help()
|
help()
|
||||||
|
1
six/input.txt
Normal file
1
six/input.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
5,1,1,5,4,2,1,2,1,2,2,1,1,1,4,2,2,4,1,1,1,1,1,4,1,1,1,1,1,5,3,1,4,1,1,1,1,1,4,1,5,1,1,1,4,1,2,2,3,1,5,1,1,5,1,1,5,4,1,1,1,4,3,1,1,1,3,1,5,5,1,1,1,1,5,3,2,1,2,3,1,5,1,1,4,1,1,2,1,5,1,1,1,1,5,4,5,1,3,1,3,3,5,5,1,3,1,5,3,1,1,4,2,3,3,1,2,4,1,1,1,1,1,1,1,2,1,1,4,1,3,2,5,2,1,1,1,4,2,1,1,1,4,2,4,1,1,1,1,4,1,3,5,5,1,2,1,3,1,1,4,1,1,1,1,2,1,1,4,2,3,1,1,1,1,1,1,1,4,5,1,1,3,1,1,2,1,1,1,5,1,1,1,1,1,3,2,1,2,4,5,1,5,4,1,1,3,1,1,5,5,1,3,1,1,1,1,4,4,2,1,2,1,1,5,1,1,4,5,1,1,1,1,1,1,1,1,1,1,3,1,1,1,1,1,4,2,1,1,1,2,5,1,4,1,1,1,4,1,1,5,4,4,3,1,1,4,5,1,1,3,5,3,1,2,5,3,4,1,3,5,4,1,3,1,5,1,4,1,1,4,2,1,1,1,3,2,1,1,4
|
65
six/lanternfish.go
Normal file
65
six/lanternfish.go
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package six
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type lanternfish struct {
|
||||||
|
fish map[int]int
|
||||||
|
population int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lanternfish) load(filename string) error {
|
||||||
|
file, err := os.Open(filename)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
l.fish = map[int]int{}
|
||||||
|
for scanner.Scan() {
|
||||||
|
fishes := strings.Split(scanner.Text(), ",")
|
||||||
|
for _, fishy := range fishes {
|
||||||
|
f, err := strconv.Atoi(fishy)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
l.fish[f]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, count := range l.fish {
|
||||||
|
l.population += count
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *lanternfish) simulate(days int) {
|
||||||
|
for i := 0; i < days; i++ {
|
||||||
|
// How many fish spawn today
|
||||||
|
newFish := l.fish[0]
|
||||||
|
|
||||||
|
// Advance spawn counts
|
||||||
|
for j := 0; j < 8; j++ {
|
||||||
|
l.fish[j] = l.fish[j+1]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset spawning fish to 6 days
|
||||||
|
l.fish[6] += newFish
|
||||||
|
|
||||||
|
// Add new fish to day 8
|
||||||
|
l.fish[8] = newFish
|
||||||
|
|
||||||
|
population := 0
|
||||||
|
for _, count := range l.fish {
|
||||||
|
population += count
|
||||||
|
}
|
||||||
|
|
||||||
|
l.population = population
|
||||||
|
}
|
||||||
|
}
|
44
six/lanternfish_test.go
Normal file
44
six/lanternfish_test.go
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package six
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_read(t *testing.T) {
|
||||||
|
l := lanternfish{}
|
||||||
|
if err := l.load("test_input.txt"); err != nil {
|
||||||
|
t.Log(err)
|
||||||
|
t.FailNow()
|
||||||
|
}
|
||||||
|
|
||||||
|
if l.population != 5 {
|
||||||
|
t.Logf("Expected 5 fish, found %d", l.population)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_simulate(t *testing.T) {
|
||||||
|
l := lanternfish{}
|
||||||
|
l.load("test_input.txt")
|
||||||
|
|
||||||
|
l.simulate(18)
|
||||||
|
if l.population != 26 {
|
||||||
|
t.Logf("Expected 26 fish, found %d", l.population)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
|
||||||
|
l.simulate(62)
|
||||||
|
if l.population != 5934 {
|
||||||
|
t.Logf("Expected 5934 fish, found %d", l.population)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Test_simulateForever(t *testing.T) {
|
||||||
|
l := lanternfish{}
|
||||||
|
l.load("test_input.txt")
|
||||||
|
|
||||||
|
l.simulate(256)
|
||||||
|
if l.population != 26984457539 {
|
||||||
|
t.Logf("Expected 26984457539 fish, found %d", l.population)
|
||||||
|
t.Fail()
|
||||||
|
}
|
||||||
|
}
|
26
six/main.go
Normal file
26
six/main.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package six
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
type Six struct {
|
||||||
|
lanternfish lanternfish
|
||||||
|
}
|
||||||
|
|
||||||
|
func Init(filepath string) *Six {
|
||||||
|
six := &Six{
|
||||||
|
lanternfish: lanternfish{},
|
||||||
|
}
|
||||||
|
|
||||||
|
six.lanternfish.load(filepath)
|
||||||
|
return six
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Six) Answer() string {
|
||||||
|
d.lanternfish.simulate(80)
|
||||||
|
return fmt.Sprintf("After 80 days there are %d fish", d.lanternfish.population)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Six) FollowUp() string {
|
||||||
|
d.lanternfish.simulate(176)
|
||||||
|
return fmt.Sprintf("After 256 days there are %d fish", d.lanternfish.population)
|
||||||
|
}
|
1
six/test_input.txt
Normal file
1
six/test_input.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
3,4,3,1,2
|
Reference in New Issue
Block a user