73 lines
1.8 KiB
Odin
73 lines
1.8 KiB
Odin
package main
|
|
|
|
import "core:math"
|
|
import "core:slice"
|
|
import "core:strconv"
|
|
import "core:strings"
|
|
import "core:bytes"
|
|
import "core:fmt"
|
|
import "core:os"
|
|
|
|
import "../util"
|
|
|
|
part_1 :: proc(lines: []string) {
|
|
result := 0
|
|
|
|
for line in lines {
|
|
line_parts := strings.split(line, " ")
|
|
|
|
password := line_parts[2]
|
|
policy_letter := strings.trim_suffix(line_parts[1], ":")
|
|
|
|
range_parts := strings.split(line_parts[0], "-")
|
|
policy_min, _ := strconv.parse_int(range_parts[0]);
|
|
policy_max, _ := strconv.parse_int(range_parts[1]);
|
|
|
|
password_policy_count := strings.count(password, policy_letter)
|
|
|
|
if password_policy_count <= policy_max && password_policy_count >= policy_min {
|
|
result += 1
|
|
}
|
|
}
|
|
|
|
fmt.printfln("Part 1: %d", result)
|
|
}
|
|
|
|
part_2 :: proc(lines: []string) {
|
|
result := 0
|
|
|
|
for line in lines {
|
|
line_parts := strings.split(line, " ")
|
|
|
|
password := line_parts[2]
|
|
policy_letter := strings.trim_suffix(line_parts[1], ":")
|
|
|
|
range_parts := strings.split(line_parts[0], "-")
|
|
policy_min, _ := strconv.parse_int(range_parts[0]);
|
|
policy_max, _ := strconv.parse_int(range_parts[1]);
|
|
|
|
password_policy_count := strings.count(password, policy_letter)
|
|
|
|
fst_hit := password[policy_min-1] == policy_letter[0]
|
|
snd_hit := password[policy_max-1] == policy_letter[0]
|
|
|
|
if fst_hit ~ snd_hit {
|
|
result += 1
|
|
}
|
|
}
|
|
|
|
fmt.printfln("Part 2: %d", result)
|
|
}
|
|
|
|
main :: proc() {
|
|
context.allocator = context.temp_allocator
|
|
|
|
INPUT :: #load("input.txt", string)
|
|
lines := strings.split(INPUT, "\n")
|
|
lines = lines[:len(lines)-1]
|
|
|
|
part_1(lines)
|
|
part_2(lines)
|
|
|
|
free_all(context.temp_allocator)
|
|
}
|