Skip to content

Commit a0babc7

Browse files
committed
Day 4 part 2 complete
1 parent a1b2474 commit a0babc7

File tree

6 files changed

+44
-24
lines changed

6 files changed

+44
-24
lines changed

2019/4/instructions.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,33 @@ Other than the range rule, the following are true:
2424
*How many different passwords* within the range given in your puzzle
2525
input meet these criteria?
2626

27-
Your puzzle input is `123257-647015`{.puzzle-input}.
27+
Your puzzle answer was `2220`.
28+
29+
The first half of this puzzle is complete! It provides one gold star: \*
30+
31+
--- Part Two --- {#part2}
32+
----------------
33+
34+
An Elf just remembered one more important detail: the two adjacent
35+
matching digits *are not part of a larger group of matching digits*.
36+
37+
Given this additional criterion, but still ignoring the range rule, the
38+
following are now true:
39+
40+
- `112233` meets these criteria because the digits never decrease and
41+
all repeated digits are exactly two digits long.
42+
- `123444` no longer meets the criteria (the repeated `44` is part of
43+
a larger group of `444`).
44+
- `111122` meets the criteria (even though `1` is repeated more than
45+
twice, it still contains a double `22`).
46+
47+
*How many different passwords* within the range given in your puzzle
48+
input meet all of the criteria?
49+
50+
Your puzzle input is still `123257-647015`{.puzzle-input}.
2851

2952
Answer:
3053

3154
You can also [\[Share[on
32-
[Twitter](https://twitter.com/intent/tweet?text=%22Secure+Container%22+%2D+Day+4+%2D+Advent+of+Code+2019&url=https%3A%2F%2Fadventofcode%2Ecom%2F2019%2Fday%2F4&related=ericwastl&hashtags=AdventOfCode)
55+
[Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Secure+Container%22+%2D+Day+4+%2D+Advent+of+Code+2019&url=https%3A%2F%2Fadventofcode%2Ecom%2F2019%2Fday%2F4&related=ericwastl&hashtags=AdventOfCode)
3356
[Mastodon](javascript:void(0);)]{.share-content}\]]{.share} this puzzle.

2019/4/lib/aoc/password_validator.ex

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
# lib/aoc/password_validator.ex
22
defmodule AOC.PasswordValidator do
3-
def check(range, number) do
3+
def check(number) do
44
Regex.match?(~r/(\d)\1/, to_string(number)) &&
55
only_increasing(String.graphemes(to_string(number)))
66
end
77

8+
def check2(number) do
9+
only_increasing(String.graphemes(to_string(number))) && check_duplicates(number)
10+
end
11+
812
defp only_increasing(chars) do
913
chars == Enum.sort(chars)
1014
end
15+
16+
defp check_duplicates(number) do
17+
lengths = Regex.scan(~r/(\d)\1+/, to_string(number)) |> Enum.map(&(Enum.at(&1 || [0], 0))) |> Enum.map(&(String.length/1))
18+
19+
lengths != [] && Enum.any?(lengths, &(&1 == 2))
20+
end
1121
end

2019/4/lib/aoc/runner.ex

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
defmodule AOC.Runner do
22
alias AOC.PasswordValidator
3+
34
def part_1 do
45
range
5-
|> Enum.filter(&(PasswordValidator.check(range, &1)))
6+
|> Enum.filter(&(PasswordValidator.check/1))
67
|> length
78
end
89

910
def part_2 do
10-
"part 2"
11+
range
12+
|> Enum.filter(&(PasswordValidator.check2/1))
13+
|> length
1114
end
1215

1316
defp range do

2019/4/test/aoc/password_validator_test.exs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ defmodule AOCPasswordValidatorTest do
33
alias AOC.PasswordValidator
44

55
test "sample tests work" do
6-
assert(PasswordValidator.check((0..999999), 111111) == true)
7-
assert(PasswordValidator.check((0..999999), 123789) == false)
8-
assert(PasswordValidator.check((0..999999), 223450) == false)
6+
assert(PasswordValidator.check(111111) == true)
7+
assert(PasswordValidator.check(123789) == false)
8+
assert(PasswordValidator.check(223450) == false)
99
end
1010
end

2019/4/test/aoc_test.exs

-8
This file was deleted.

2019/common/test/aoc_test.exs

-8
This file was deleted.

0 commit comments

Comments
 (0)