package main
import "core:fmt"
import "core:strings"
import "core:os"
import "core:strconv"
import "core:unicode/utf8"
main :: proc() {
input, err := os.read_entire_file("input.txt");
inputStr := string(input);
lines := strings.split(inputStr, "\n");
validNum_1 := 0;
validNum_2 := 0;
for line in lines {
if part1(line) do validNum_1 += 1;
if part2(line) do validNum_2 += 1;
}
fmt.println(validNum_1);
fmt.println(validNum_2);
}
part1 :: proc(line: string) -> bool {
segments := strings.split(line, " ");
segment1_sections := strings.split(segments[0], "-");
minNum, _ := strconv.parse_int(segment1_sections[0]);
maxNum, _ := strconv.parse_int(segment1_sections[1]);
segment2_rune, _ := utf8.decode_rune_in_string(segments[1]);
segment3 := segments[2];
count := 0;
for r in segment3 {
if r == segment2_rune {
count += 1;
}
}
if count >= minNum && count <= maxNum do return true;
return false;
}
part2 :: proc(line: string) -> bool {
segments := strings.split(line, " ");
segment1_sections := strings.split(segments[0], "-");
pos1, _ := strconv.parse_int(segment1_sections[0]);
pos2, _ := strconv.parse_int(segment1_sections[1]);
segment2_rune, _ := utf8.decode_rune_in_string(segments[1]);
segment3 := segments[2];
rune_index := 0;
count := 0;
for r, byte_index in segment3 {
if rune_index == pos1 - 1 {
if r == segment2_rune do count += 1;
} else if rune_index == pos2 - 1 {
if r == segment2_rune do count += 1;
}
rune_index += 1;
}
return count == 1;
}