Day 2
This commit is contained in:
parent
406d4f06eb
commit
eaedf6b76b
1 changed files with 73 additions and 0 deletions
73
d02/main.odin
Normal file
73
d02/main.odin
Normal file
|
|
@ -0,0 +1,73 @@
|
||||||
|
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)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue