Skip to content

Commit

Permalink
add day6 solutions and tests
Browse files Browse the repository at this point in the history
brute force approach won't work for part 2, needs updating to prevent
10+ gb of memory allocation
  • Loading branch information
scottmckendry committed Nov 2, 2024
1 parent 1ff1b7a commit e2c2db4
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
57 changes: 57 additions & 0 deletions 2021/06_test.odin
Original file line number Diff line number Diff line change
@@ -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()
}
46 changes: 46 additions & 0 deletions 2021/06p1.odin
Original file line number Diff line number Diff line change
@@ -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
}
}
}
14 changes: 14 additions & 0 deletions 2021/06p2.odin
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions 2021/inputs/06.txt
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions 2021/main.odin
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit e2c2db4

Please sign in to comment.