From e2c2db471247fa8b5f8b38ac6ac29c18d6ba27fd Mon Sep 17 00:00:00 2001 From: Scott McKendry <39483124+scottmckendry@users.noreply.github.com> Date: Sun, 3 Nov 2024 07:59:05 +1300 Subject: [PATCH] add day6 solutions and tests brute force approach won't work for part 2, needs updating to prevent 10+ gb of memory allocation --- 2021/06_test.odin | 57 ++++++++++++++++++++++++++++++++++++++++++++++ 2021/06p1.odin | 46 +++++++++++++++++++++++++++++++++++++ 2021/06p2.odin | 14 ++++++++++++ 2021/inputs/06.txt | 1 + 2021/main.odin | 2 ++ 5 files changed, 120 insertions(+) create mode 100644 2021/06_test.odin create mode 100644 2021/06p1.odin create mode 100644 2021/06p2.odin create mode 100644 2021/inputs/06.txt diff --git a/2021/06_test.odin b/2021/06_test.odin new file mode 100644 index 0000000..dcf8426 --- /dev/null +++ b/2021/06_test.odin @@ -0,0 +1,57 @@ +package main + +import "core:fmt" +import "core:testing" + +@(test) +d06p1 :: proc(t: ^testing.T) { + input := "3,4,3,1,2" + + want := 26 + got := simulate_lanterfish_growth(input, 18) + testing.expect(t, got == want, fmt.tprintf("Got: %v | Want: %v", got, want)) + + want = 5934 + got = simulate_lanterfish_growth(input, 80) + testing.expect(t, got == want, fmt.tprintf("Got: %v | Want: %v", got, want)) + + free_all() +} + +//@(test) +//d06p2 :: proc(t: ^testing.T) { +// input := "3,4,3,1,2" +// +// want := 26984457539 +// got := simulate_lanterfish_growth(input, 256) +// testing.expect(t, got == want, fmt.tprintf("Got: %v | Want: %v", got, want)) +// +// free_all() +//} + +@(test) +test_parse_laternfish :: proc(t: ^testing.T) { + input := "3,4,3,1,2" + want := [dynamic]int{3, 4, 3, 1, 2} + got := parse_lanterfish(input) + + for fish, i in got { + testing.expect(t, fish == want[i], fmt.tprintf("Got: %v | Want: %v", fish, want[i])) + } + + free_all() +} + +@(test) +test_simulate_laternfish_day :: proc(t: ^testing.T) { + fish := [dynamic]int{2, 3, 2, 0, 1} + want := [dynamic]int{1, 2, 1, 6, 0, 8} + + simulate_lanternfish_day(&fish) + + for i in 0 ..< len(fish) { + testing.expect(t, fish[i] == want[i], fmt.tprintf("Got: %v | Want: %v", fish[i], want[i])) + } + + free_all() +} diff --git a/2021/06p1.odin b/2021/06p1.odin new file mode 100644 index 0000000..3644bdd --- /dev/null +++ b/2021/06p1.odin @@ -0,0 +1,46 @@ +package main + +import "core:fmt" +import "core:strconv" +import "core:strings" +import "utils" + +D06P1 :: proc() { + lines, backing := utils.read_lines("./inputs/06.txt") + defer delete(lines) + defer delete(backing) + + days := 80 + total_lanterfish := simulate_lanterfish_growth(lines[0], days) + fmt.printfln("Number of lanterfish after %v days: %v", days, total_lanterfish) +} + +simulate_lanterfish_growth :: proc(input: string, days: int) -> int { + fish := parse_lanterfish(input) + defer delete(fish) + + for _ in 1 ..= days { + simulate_lanternfish_day(&fish) + } + return len(fish) +} + +parse_lanterfish :: proc(input: string) -> (fish: [dynamic]int) { + fish_strs := strings.split(input, ",", context.temp_allocator) + for fish_str in fish_strs { + append(&fish, strconv.atoi(fish_str)) + } + return +} + +simulate_lanternfish_day :: proc(fish: ^[dynamic]int) { + for i in 0 ..< len(fish) { + switch fish[i] { + case 0: + fish[i] = 6 + append(fish, 9) // append 9 instead of 8 because this fish will always be processed in this day + case: + fish[i] -= 1 + } + } +} diff --git a/2021/06p2.odin b/2021/06p2.odin new file mode 100644 index 0000000..e646d42 --- /dev/null +++ b/2021/06p2.odin @@ -0,0 +1,14 @@ +package main + +import "core:fmt" +import "utils" + +D06P2 :: proc() { + lines, backing := utils.read_lines("./inputs/06.txt") + defer delete(lines) + defer delete(backing) + + days := 256 + total_lanterfish := simulate_lanterfish_growth(lines[0], days) + fmt.printfln("Number of lanterfish after %v days: %v", days, total_lanterfish) +} diff --git a/2021/inputs/06.txt b/2021/inputs/06.txt new file mode 100644 index 0000000..9836a45 --- /dev/null +++ b/2021/inputs/06.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 diff --git a/2021/main.odin b/2021/main.odin index a81f513..61f81ab 100644 --- a/2021/main.odin +++ b/2021/main.odin @@ -21,6 +21,8 @@ solutions: map[string]proc() = { "04P2:Giant Squid" = D04P2, "05P1:Hydrothermal Venture" = D05P1, "05P2:Hydrothermal Venture" = D05P2, + "06P1:Lanternfish" = D06P1, + //"06P2:Lanternfish" = D06P2, } main :: proc() {