finish day 3
This commit is contained in:
86
day3/day3.odin
Normal file
86
day3/day3.odin
Normal file
@@ -0,0 +1,86 @@
|
||||
package day3
|
||||
|
||||
import "../utils"
|
||||
import math "core:math"
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
|
||||
sample :: `987654321111111
|
||||
811111111111119
|
||||
234234234234278
|
||||
818181911112111`
|
||||
|
||||
solve_first :: proc(s: string) -> u64 {
|
||||
input := s
|
||||
total: u64
|
||||
for line in strings.split_lines_iterator(&input) {
|
||||
largest := '0'
|
||||
largest_index := 0
|
||||
for c, i in line[:len(line) - 1] {
|
||||
if c > largest {
|
||||
largest = c
|
||||
largest_index = i
|
||||
}
|
||||
}
|
||||
total += u64(largest - '0') * 10
|
||||
largest = 0
|
||||
for c in line[largest_index + 1:] {
|
||||
if c > largest {
|
||||
largest = c
|
||||
}
|
||||
}
|
||||
total += u64(largest - '0')
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_first_sample :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, solve_first(sample), 357)
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_first_puzzle :: proc(t: ^testing.T) {
|
||||
content := utils.read_file("day3/input.txt")
|
||||
defer delete(content)
|
||||
testing.expect_value(t, solve_first(content), 17095)
|
||||
}
|
||||
|
||||
solve_second :: proc(s: string) -> u64 {
|
||||
input := s
|
||||
total: u64
|
||||
for line in strings.split_lines_iterator(&input) {
|
||||
intermediate_total: u64
|
||||
largest_index := 0
|
||||
first := true
|
||||
for i := 0; i < 12; i += 1 {
|
||||
defer first = false
|
||||
largest := '0'
|
||||
starting := first ? 0 : largest_index + 1
|
||||
for c, j in line[starting:len(line) - (11 - i)] {
|
||||
if c > largest {
|
||||
largest = c
|
||||
largest_index = starting + j
|
||||
}
|
||||
}
|
||||
exp := 11 - i
|
||||
intermediate_total += u64(largest - '0') * u64(math.pow_f64(10, f64(exp)))
|
||||
}
|
||||
total += intermediate_total
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_second_sample :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, solve_second(sample), 3121910778619)
|
||||
}
|
||||
|
||||
@(test)
|
||||
solve_second_puzzle :: proc(t: ^testing.T) {
|
||||
content := utils.read_file("day3/input.txt")
|
||||
defer delete(content)
|
||||
testing.expect_value(t, solve_second(content), 168794698570517)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user