-
Notifications
You must be signed in to change notification settings - Fork 0
/
12_4.jl
88 lines (79 loc) · 2.37 KB
/
12_4.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env julia
"""
Puzzle at https://adventofcode.com/2019/day/4
"""
PUZZLE_INPUT="108457-562041"
function main1()
str_inputs = split(PUZZLE_INPUT, "-")
inputs = map(x -> parse(Int, x), str_inputs)
ranges = inputs[1]:inputs[end]
num_passwords = zero(Int)
for i in ranges
str_number = string(i)
length(str_number) == 6 || continue
# Test for at least 1 set of adjacent digits
adjacent_numbers = false
for j in 1:5
if str_number[j] == str_number[j+1]
adjacent_numbers = true
break
end
end
adjacent_numbers || continue
# Digits never decrease from left-to-right
decrease = false
for j in 1:5
if str_number[j] > str_number[j+1]
decrease = true
break
end
end
decrease && continue
num_passwords += 1
end
println("Part 1 Answer:")
@show num_passwords
end
function main2()
str_inputs = split(PUZZLE_INPUT, "-")
inputs = map(x -> parse(Int, x), str_inputs)
ranges = inputs[1]:inputs[end]
num_passwords = zero(Int)
for i in ranges
str_number = string(i)
length(str_number) == 6 || continue
# Test for at least 1 set of adjacent digits but as a run of 2
adjacent_numbers = false
for j in 1:5
total_adjacent_nums = zero(Int)
if str_number[j] == str_number[j+1]
# Look behind to check for a run of 3
if j > 1
str_number[j-1] == str_number[j] && continue
end
# Now look ahead
if j+1 < length(str_number)
str_number[j+1] == str_number[j+2] && continue
end
# If lookbehind and lookahead pass then this has to be a run of 2
adjacent_numbers = true
break
end
end
adjacent_numbers || continue
# Digits never decrease from left-to-right
decrease = false
for j in 1:5
if str_number[j] > str_number[j+1]
decrease = true
break
end
end
decrease && continue
num_passwords += 1
end
println("Part 2 Answer:")
@show num_passwords
end
main1()
main2()