Skip to content

Commit

Permalink
start building out odin helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed Oct 28, 2024
1 parent 56947e2 commit cd99c45
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 27 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ go.work

# Binaries & executables
**/aoc202*
2021/2021
*.exe
54 changes: 27 additions & 27 deletions 2021/01p1.odin
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
package main

import "core:fmt"
import "core:strconv"
import "utils"

main :: proc() {
lines := utils.read_lines("./inputs/01p1.txt")
depth_increase_count := get_depth_increase_count(lines)

fmt.printfln("Depth increasd %v times.", depth_increase_count)
}

get_depth_increase_count :: proc(input: [dynamic]string) -> int {
depth_increase_count := 0
prev_depth := strconv.atoi(input[0])

for line in input {
depth := strconv.atoi(line)

if prev_depth < depth {
depth_increase_count += 1
}
prev_depth = depth
}
return depth_increase_count
}
package main

import "core:fmt"
import "core:strconv"
import "utils"

D01P1 :: proc() {
lines := utils.read_lines("./inputs/01p1.txt")
depth_increase_count := get_depth_increase_count(lines)

fmt.printfln("Depth increasd %v times.", depth_increase_count)
}

get_depth_increase_count :: proc(input: [dynamic]string) -> int {
depth_increase_count := 0
prev_depth := strconv.atoi(input[0])

for line in input {
depth := strconv.atoi(line)

if prev_depth < depth {
depth_increase_count += 1
}
prev_depth = depth
}
return depth_increase_count
}
18 changes: 18 additions & 0 deletions 2021/main.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import "core:fmt"
import "core:os"
import "core:strconv"
import "core:strings"
import "utils"


main :: proc() {
solutions: map[string]proc() = {
"01P1:Sonar Sweep" = D01P1,
}

for _, solution in solutions {
utils.benchmark(solution)
}
}
9 changes: 9 additions & 0 deletions 2021/utils/utils.odin
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import "core:fmt"
import "core:os"
import "core:strings"
import "core:time"

read_lines :: proc(filepath: string) -> [dynamic]string {
data, ok := os.read_entire_file(filepath, context.allocator)
Expand All @@ -19,3 +20,11 @@ read_lines :: proc(filepath: string) -> [dynamic]string {

return lines
}

benchmark :: proc(solution: proc()) -> time.Duration {
start := time.now()
solution()
took := time.since(start)
fmt.printfln("Execution Time: %v", took)
return took
}

0 comments on commit cd99c45

Please sign in to comment.