-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.jl
61 lines (42 loc) · 1.49 KB
/
day02.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function parse_input_line(line)
pattern = r"^(\d+)\-(\d+)\s(\w):\s(\w+)"
m = match(pattern, line)
if m === nothing
error("Was not able to correctly parse the password")
end
# Get the first and last numbers
first_num = parse(Int, m.captures[1])
last_num = parse(Int, m.captures[2])
# Get the letter in question
letter = m.captures[3]
# Get the password
pswd = m.captures[4]
return (first_num, last_num, letter, pswd)
end
function pswd_is_valid_part_1(input_data)
min_num, max_num, letter, pswd = parse_input_line(input_data)
# Count the number of instances of said letter
num_instances = count(letter, pswd)
# Is it within the bounds?
(min_num <= num_instances) & (num_instances <= max_num)
end
function pswd_is_valid_part_2(input_data)
first_num, last_num, letter, pswd = parse_input_line(input_data)
# Convert the letter from a string to a character
character = only(letter)
# Check for matches
at_first_index = pswd[first_num] == character
at_last_index = pswd[last_num] == character
# Make sure only one matches
at_first_index + at_last_index == 1
end
function main()
filename = "inputs/day02.txt"
input_data = readlines(filename)
part1_solution = count(pswd_is_valid_part_1, input_data)
part2_solution = count(pswd_is_valid_part_2, input_data)
(part1_solution, part2_solution)
end
@time (part1_solution, part2_solution) = main()
@show part1_solution
@show part2_solution