-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
brute force approach won't work for part 2, needs updating to prevent 10+ gb of memory allocation
- Loading branch information
1 parent
1ff1b7a
commit e2c2db4
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters