106 lines
2.3 KiB
Odin
106 lines
2.3 KiB
Odin
package day1
|
|
|
|
import "core:strings"
|
|
import "core:testing"
|
|
import strconv "core:strconv"
|
|
import os "core:os"
|
|
|
|
sample :: `L68
|
|
L30
|
|
R48
|
|
L5
|
|
R60
|
|
L55
|
|
L1
|
|
L99
|
|
R14
|
|
L82`
|
|
|
|
solve_first :: proc(s: string) -> uint {
|
|
s := s
|
|
pointing_at_zero: uint
|
|
current_dial := 50
|
|
for line in strings.split_lines_iterator(&s) {
|
|
if line == "" do continue
|
|
value, _ := strconv.parse_int(line[1:])
|
|
if line[0] == 'L' {
|
|
current_dial -= value
|
|
} else {
|
|
current_dial += value
|
|
}
|
|
|
|
|
|
for current_dial < 0 {
|
|
current_dial += 100
|
|
}
|
|
for current_dial >= 100 {
|
|
current_dial -= 100
|
|
}
|
|
|
|
if current_dial == 0 {
|
|
pointing_at_zero += 1
|
|
}
|
|
}
|
|
return pointing_at_zero
|
|
}
|
|
|
|
solve_second :: proc(s: string) -> uint {
|
|
s := s
|
|
pointing_at_zero: uint
|
|
current_dial := 50
|
|
for line in strings.split_lines_iterator(&s) {
|
|
if line == "" do continue
|
|
value, _ := strconv.parse_int(line[1:])
|
|
for value >= 100 {
|
|
value -= 100
|
|
pointing_at_zero += 1
|
|
}
|
|
if line[0] == 'L' {
|
|
current_dial -= value
|
|
} else {
|
|
current_dial += value
|
|
}
|
|
|
|
for current_dial < 0 {
|
|
current_dial += 100
|
|
pointing_at_zero += 1
|
|
}
|
|
for current_dial > 100 {
|
|
current_dial -= 100
|
|
pointing_at_zero += 1
|
|
}
|
|
|
|
if current_dial == 0 {
|
|
pointing_at_zero += 1
|
|
}
|
|
}
|
|
return pointing_at_zero
|
|
}
|
|
|
|
@(test)
|
|
solve_first_sample :: proc(t: ^testing.T) {
|
|
pointing_at_zero := solve_first(sample)
|
|
testing.expect_value(t, pointing_at_zero, 3)
|
|
}
|
|
|
|
@(test)
|
|
solve_first_input :: proc(t: ^testing.T) {
|
|
contents, _ := os.read_entire_file("day1/input.txt")
|
|
defer delete(contents)
|
|
pointing_at_zero := solve_first(string(contents))
|
|
testing.expect_value(t, pointing_at_zero, 1158)
|
|
}
|
|
|
|
@(test)
|
|
solve_second_sample :: proc(t: ^testing.T) {
|
|
pointing_at_zero := solve_second(sample)
|
|
testing.expect_value(t, pointing_at_zero, 6)
|
|
}
|
|
|
|
@(test)
|
|
solve_second_input :: proc(t: ^testing.T) {
|
|
contents, _ := os.read_entire_file("day1/input.txt")
|
|
defer delete(contents)
|
|
pointing_at_zero := solve_second(string(contents))
|
|
testing.expect_value(t, pointing_at_zero, 6860)
|
|
} |