-
Notifications
You must be signed in to change notification settings - Fork 0
/
06p1.odin
55 lines (46 loc) · 1.31 KB
/
06p1.odin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import "core:fmt"
import "core:strconv"
import "core:strings"
D06P1 :: proc() {
input_string := #load("./inputs/06.txt", string)
lines := strings.split(input_string, "\n", context.temp_allocator)
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)
for _ in 1 ..= days {
simulate_lanternfish_day(&fish)
}
total_lanternfish := 0
for i in 0 ..< len(fish) {
total_lanternfish += fish[i]
}
return total_lanternfish
}
parse_lanterfish :: proc(input: string) -> (fish: [9]int) {
fish_strs := strings.split(input, ",", context.temp_allocator)
for fish_str in fish_strs {
current_fish := strconv.atoi(fish_str)
fish[current_fish] += 1
}
return
}
simulate_lanternfish_day :: proc(fish: ^[9]int) {
temp_fish := [9]int{}
// reversing the loop ensures that the fish at index 8 are not overwritten
for i := 8; i >= 0; i -= 1 {
switch i {
case 0:
temp_fish[6] += fish[i] // fish at index 7 have already been moved, so we can add to them
temp_fish[8] = fish[i] // newborn fish at index 8
case:
temp_fish[i - 1] = fish[i]
}
}
for i := 0; i < 9; i += 1 {
fish[i] = temp_fish[i]
}
}