Day 3
This commit is contained in:
parent
aa0f725e19
commit
d115cdeac1
1 changed files with 58 additions and 0 deletions
58
d03/main.odin
Normal file
58
d03/main.odin
Normal file
|
|
@ -0,0 +1,58 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "core:container/queue"
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:math"
|
||||||
|
import "core:os"
|
||||||
|
import "core:slice"
|
||||||
|
import "core:strconv"
|
||||||
|
import "core:strings"
|
||||||
|
import "core:time"
|
||||||
|
import "core:unicode/utf8"
|
||||||
|
|
||||||
|
import util "../util"
|
||||||
|
|
||||||
|
find_max_jolts :: proc(line: ^string, battery_count: int) -> i64 {
|
||||||
|
stack := make([dynamic]rune, 0, battery_count)
|
||||||
|
for ch, i in line {
|
||||||
|
for len(stack) > 0 && stack[len(stack) - 1] < ch && len(stack) + len(line) - i > battery_count {
|
||||||
|
pop(&stack)
|
||||||
|
}
|
||||||
|
if len(stack) < battery_count do append(&stack, ch)
|
||||||
|
}
|
||||||
|
jolts, _ := strconv.parse_i64(utf8.runes_to_string(stack[:]))
|
||||||
|
return jolts
|
||||||
|
}
|
||||||
|
|
||||||
|
part_1 :: proc(lines: ^[]string) {
|
||||||
|
result: i64 = 0
|
||||||
|
for &line in lines do result += find_max_jolts(&line, 2)
|
||||||
|
fmt.printfln(" [Result] %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
part_2 :: proc(lines: ^[]string) {
|
||||||
|
result: i64 = 0
|
||||||
|
for &line in lines do result += find_max_jolts(&line, 12)
|
||||||
|
fmt.printfln(" [Result] %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
main :: proc() {
|
||||||
|
context.allocator = context.temp_allocator
|
||||||
|
defer free_all(context.temp_allocator)
|
||||||
|
|
||||||
|
INPUT :: #load("input.txt", string)
|
||||||
|
lines := strings.split(INPUT, "\n")
|
||||||
|
lines = lines[:len(lines) - 1]
|
||||||
|
|
||||||
|
fmt.println("[Part 1]")
|
||||||
|
start := time.tick_now()
|
||||||
|
part_1(&lines)
|
||||||
|
duration := time.tick_diff(start, time.tick_now())
|
||||||
|
fmt.printfln(" [Time] %fms\n", time.duration_milliseconds(duration))
|
||||||
|
|
||||||
|
fmt.printfln("[Part 2]")
|
||||||
|
start = time.tick_now()
|
||||||
|
part_2(&lines)
|
||||||
|
duration = time.tick_diff(start, time.tick_now())
|
||||||
|
fmt.printfln(" [Time] %fms", time.duration_milliseconds(duration))
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue