Day 1
This commit is contained in:
commit
366f814afd
3 changed files with 83 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
# Hide inputs
|
||||||
|
*.txt
|
||||||
79
d01/main.odin
Normal file
79
d01/main.odin
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
import "core:math"
|
||||||
|
import "core:os"
|
||||||
|
import "core:strconv"
|
||||||
|
import "core:strings"
|
||||||
|
import "core:time"
|
||||||
|
|
||||||
|
import util "../util"
|
||||||
|
|
||||||
|
DIAL_START :: 50
|
||||||
|
DIAL_MAX :: 100
|
||||||
|
|
||||||
|
part_1 :: proc(lines: ^[]string) {
|
||||||
|
result := 0
|
||||||
|
dial := DIAL_START
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
amount, ok := strconv.parse_int(line[1:]); assert(ok)
|
||||||
|
|
||||||
|
dir := line[0]
|
||||||
|
if dir == 'L' do amount = -amount
|
||||||
|
|
||||||
|
dial += amount
|
||||||
|
dial %%= DIAL_MAX
|
||||||
|
if dial == 0 do result += 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.printfln(" [Result] %v", result)
|
||||||
|
}
|
||||||
|
|
||||||
|
part_2 :: proc(lines: ^[]string) {
|
||||||
|
result := 0
|
||||||
|
dial := DIAL_START
|
||||||
|
|
||||||
|
for line in lines {
|
||||||
|
amount, ok := strconv.parse_int(line[1:]); assert(ok)
|
||||||
|
result += amount / DIAL_MAX
|
||||||
|
|
||||||
|
dir := line[0]
|
||||||
|
if dir == 'L' do amount = -amount
|
||||||
|
|
||||||
|
old_dial := dial
|
||||||
|
dial += amount
|
||||||
|
dial %%= DIAL_MAX
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case dial == 0:
|
||||||
|
result += 1
|
||||||
|
case old_dial != 0:
|
||||||
|
wrapped := (dir == 'L' && old_dial < dial) || (dir == 'R' && old_dial > dial)
|
||||||
|
if wrapped do result += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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] %vns\n", time.duration_nanoseconds(duration))
|
||||||
|
|
||||||
|
fmt.printfln("[Part 2]")
|
||||||
|
start = time.tick_now()
|
||||||
|
part_2(&lines)
|
||||||
|
duration = time.tick_diff(start, time.tick_now())
|
||||||
|
fmt.printfln(" [Time] %vns", time.duration_nanoseconds(duration))
|
||||||
|
}
|
||||||
2
util/util.odin
Normal file
2
util/util.odin
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
package util
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue