Fix Day 1
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user