finish day 3
This commit is contained in:
96
day2/day2.odin
Normal file
96
day2/day2.odin
Normal file
@@ -0,0 +1,96 @@
|
||||
package day2
|
||||
|
||||
import "core:mem"
|
||||
import os "core:os"
|
||||
import "core:strconv"
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
import "../utils"
|
||||
|
||||
sample :: `11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124`
|
||||
|
||||
solve_first :: proc(s: string) -> (result: u64) {
|
||||
s := s
|
||||
buf: [100]u8
|
||||
for line in strings.split_iterator(&s, ",") {
|
||||
hyphen_index := strings.index_rune(line, '-')
|
||||
start_str := line[:hyphen_index]
|
||||
start, _ := strconv.parse_u64(start_str)
|
||||
end_str := line[hyphen_index + 1:]
|
||||
end, _ := strconv.parse_u64(end_str)
|
||||
for n in start ..= end {
|
||||
str := strconv.write_uint(buf[:], n, 10)
|
||||
defer mem.zero_slice(buf[:])
|
||||
|
||||
if len(str) % 2 != 0 do continue
|
||||
half_len := len(str) / 2
|
||||
first_half := str[:half_len]
|
||||
second_half := str[half_len:]
|
||||
if first_half == second_half {
|
||||
result += n
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_first_sample :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, solve_first(sample), 1227775554)
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_first_input :: proc(t: ^testing.T) {
|
||||
content := utils.read_file("day2/input.txt")
|
||||
defer delete(content)
|
||||
testing.expect_value(t, solve_first(content), 21898734247)
|
||||
}
|
||||
|
||||
solve_second :: proc(s: string) -> (result: u64) {
|
||||
s := s
|
||||
buf: [100]u8
|
||||
for line in strings.split_iterator(&s, ",") {
|
||||
hyphen_index := strings.index_rune(line, '-')
|
||||
start_str := line[:hyphen_index]
|
||||
start, _ := strconv.parse_u64(start_str)
|
||||
end_str := line[hyphen_index + 1:]
|
||||
end, _ := strconv.parse_u64(end_str)
|
||||
for n in start ..= end {
|
||||
str := strconv.write_uint(buf[:], n, 10)
|
||||
defer mem.zero_slice(buf[:])
|
||||
|
||||
for i := 1; i <= len(str) / 2; i += 1 {
|
||||
if len(str) % i != 0 do continue
|
||||
first := str[:i]
|
||||
found := true
|
||||
for j := i * 2; j <= len(str); j += i {
|
||||
second := str[j - i:j]
|
||||
if first != second {
|
||||
found = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if found {
|
||||
result += n
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@(test)
|
||||
solve_second_sample :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, solve_second(sample), 4174379265)
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_second_input :: proc(t: ^testing.T) {
|
||||
content := utils.read_file("day2/input.txt")
|
||||
defer delete(content)
|
||||
testing.expect_value(t, solve_second(content), 28915664389)
|
||||
}
|
||||
Reference in New Issue
Block a user