Fix Day 1

This commit is contained in:
2026-02-11 19:09:32 -05:00
parent 73aefb8c9c
commit daafaf45f1
6 changed files with 4786 additions and 12 deletions

2
Brewfile Normal file
View File

@@ -0,0 +1,2 @@
brew "odin"
# brew "ols"

View File

@@ -1,6 +0,0 @@
1
10
1
200

View File

@@ -4,6 +4,7 @@ import "core:fmt"
import "core:os"
import "core:math"
import "core:strconv"
import "core:slice/heap"
scan_to_next :: proc(data: []byte, delim: byte, start_from: int) -> int {
for idx := start_from; idx < len(data); idx += 1 {
@@ -114,7 +115,7 @@ main :: proc() {
fh, errno := os.open(os.args[1])
if errno != os.ERROR_NONE {
fmt.printf("Cannot open %s: %s\n", os.args[1], os.get_last_error_string())
os.exit(int(errno))
os.exit(-1)
}
defer os.close(fh)
content, ok := os.read_entire_file_from_handle(fh)
@@ -124,10 +125,17 @@ main :: proc() {
}
lines := split_lines(content, '\n')
total := 0
max := 0
mh := make([dynamic]int, context.temp_allocator)
defer delete(mh)
comparator :: proc(a, b: int) -> bool {
return a < b
}
heap.make(mh[:], comparator)
for line, idx in lines {
if len(line) == 0 {
max = math.max(max, total)
append(&mh, total)
heap.push(mh[:], comparator)
total = 0
} else {
x: int
@@ -140,6 +148,13 @@ main :: proc() {
}
}
}
max = math.max(total, max)
fmt.printf("Largest grouping: %d\n", max)
}
append(&mh, total)
heap.make(mh[:], comparator)
total = 0
fmt.printf("Largest single value: %d\n", mh[0])
for _ in 0..<3 {
total += mh[0]
heap.pop(mh[:], comparator)
}
fmt.printf("Sum of three largest: %d\n", total)
}

2249
odin/day01/input.txt Normal file

File diff suppressed because it is too large Load Diff

14
odin/day01/test.txt Normal file
View File

@@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

2500
odin/day02/input.txt Normal file

File diff suppressed because it is too large Load Diff