From 84a1d25dbef22ab287e960e09959a6211f35ed54 Mon Sep 17 00:00:00 2001 From: James Griffin Date: Mon, 6 Dec 2021 14:08:30 +0000 Subject: [PATCH] Day 6 solution --- README.md | 10 +++++++ main.go | 3 ++ six/input.txt | 1 + six/lanternfish.go | 65 +++++++++++++++++++++++++++++++++++++++++ six/lanternfish_test.go | 44 ++++++++++++++++++++++++++++ six/main.go | 26 +++++++++++++++++ six/test_input.txt | 1 + 7 files changed, 150 insertions(+) create mode 100644 six/input.txt create mode 100644 six/lanternfish.go create mode 100644 six/lanternfish_test.go create mode 100644 six/main.go create mode 100644 six/test_input.txt diff --git a/README.md b/README.md index 4fa2e80..bdf1681 100644 --- a/README.md +++ b/README.md @@ -65,3 +65,13 @@ There are 5585 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 +``` + diff --git a/main.go b/main.go index 73b3ec2..5b243c1 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "unsupervised.ca/aoc2021/five" "unsupervised.ca/aoc2021/four" "unsupervised.ca/aoc2021/one" + "unsupervised.ca/aoc2021/six" "unsupervised.ca/aoc2021/three" "unsupervised.ca/aoc2021/two" ) @@ -37,6 +38,8 @@ func main() { day = four.Init("four/input.txt") case "five": day = five.Init("five/input.txt") + case "six": + day = six.Init("six/input.txt") default: fmt.Printf("%q does not have a solution.\n", flagParts[1]) help() diff --git a/six/input.txt b/six/input.txt new file mode 100644 index 0000000..c35cc57 --- /dev/null +++ b/six/input.txt @@ -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 \ No newline at end of file diff --git a/six/lanternfish.go b/six/lanternfish.go new file mode 100644 index 0000000..b407e44 --- /dev/null +++ b/six/lanternfish.go @@ -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 + } +} diff --git a/six/lanternfish_test.go b/six/lanternfish_test.go new file mode 100644 index 0000000..84946a7 --- /dev/null +++ b/six/lanternfish_test.go @@ -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() + } +} diff --git a/six/main.go b/six/main.go new file mode 100644 index 0000000..ccde17d --- /dev/null +++ b/six/main.go @@ -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) +} diff --git a/six/test_input.txt b/six/test_input.txt new file mode 100644 index 0000000..a7af2b1 --- /dev/null +++ b/six/test_input.txt @@ -0,0 +1 @@ +3,4,3,1,2 \ No newline at end of file