Skip to content

Commit

Permalink
use a non brute force approach for day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Nov 2, 2024
1 parent 4b125f7 commit ec85d6f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 24 deletions.
26 changes: 13 additions & 13 deletions 2021/06_test.odin
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ d06p1 :: proc(t: ^testing.T) {
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)
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}
want := [9]int{0, 1, 1, 2, 1, 0, 0, 0, 0}
got := parse_lanterfish(input)

for fish, i in got {
Expand All @@ -44,8 +44,8 @@ test_parse_laternfish :: proc(t: ^testing.T) {

@(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}
fish := [9]int{1, 1, 2, 1, 0, 0, 0, 0, 0}
want := [9]int{1, 2, 1, 0, 0, 0, 1, 0, 1}

simulate_lanternfish_day(&fish)

Expand Down
30 changes: 20 additions & 10 deletions 2021/06p1.odin
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,40 @@ D06P1 :: proc() {

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)

total_lanternfish := 0
for i in 0 ..< len(fish) {
total_lanternfish += fish[i]
}
return total_lanternfish
}

parse_lanterfish :: proc(input: string) -> (fish: [dynamic]int) {
parse_lanterfish :: proc(input: string) -> (fish: [9]int) {
fish_strs := strings.split(input, ",", context.temp_allocator)
for fish_str in fish_strs {
append(&fish, strconv.atoi(fish_str))
current_fish := strconv.atoi(fish_str)
fish[current_fish] += 1
}
return
}

simulate_lanternfish_day :: proc(fish: ^[dynamic]int) {
for i in 0 ..< len(fish) {
switch fish[i] {
simulate_lanternfish_day :: proc(fish: ^[9]int) {
temp_fish := [9]int{}
for i := 8; i >= 0; i -= 1 {
switch i {
case 0:
fish[i] = 6
append(fish, 9) // append 9 instead of 8 because this fish will always be processed in this day
temp_fish[6] += fish[i]
temp_fish[8] = fish[i]
case:
fish[i] -= 1
temp_fish[i - 1] = fish[i]
}
}

for i := 0; i < 9; i += 1 {
fish[i] = temp_fish[i]
}
}
2 changes: 1 addition & 1 deletion 2021/main.odin
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ solutions: map[string]proc() = {
"05P1:Hydrothermal Venture" = D05P1,
"05P2:Hydrothermal Venture" = D05P2,
"06P1:Lanternfish" = D06P1,
//"06P2:Lanternfish" = D06P2,
"06P2:Lanternfish" = D06P2,
}

main :: proc() {
Expand Down

0 comments on commit ec85d6f

Please sign in to comment.