Day 2 Part 2

This commit is contained in:
Adam Millerchip 2020-12-02 14:43:05 +09:00
parent 87b30e106f
commit a6af9ede81
4 changed files with 1056 additions and 1 deletions

View File

@ -2,7 +2,7 @@
My (attempted) solutions to [Advent of Code 2020](https://adventofcode.com/2020) in Elixir.
<img width="976" alt="image" src="https://user-images.githubusercontent.com/498229/100701084-45b44680-33e2-11eb-9fb9-11c6161a49ef.png">
<img width="973" alt="image" src="https://user-images.githubusercontent.com/498229/100832848-05b99600-34ac-11eb-931f-4b3a95d428f7.png">
## Strategy

28
day2/README Normal file
View File

@ -0,0 +1,28 @@
Day 2 Notes
------
Part1:
------
$ elixir day2part1.exs
414
Thoughts:
Use a regex to parse the input
Huh, can't iterate a string with Enum. Split into a list with Enum.graphemes/1
------
Part1:
------
$ elixir day2part2.exs
413
Thoughts:
Convert indices to be zero-based.
No xor operator without importing Bitwise? Just write it manually.
Initially misread the problem - thought first position must match, and second position must not match,
which caused some confusion.
Probably rebound to the same variable names a bit too much - should work on clearer variable names.

27
day2/day2part2.exs Normal file
View File

@ -0,0 +1,27 @@
defmodule Day2Part2 do
def run do
regex = ~r/\A(\d+)-(\d+) (\w): (\w+)\z/
File.read!("input")
|> String.trim()
|> String.split("\n")
|> Enum.map(&Regex.run(regex, &1, capture: :all_but_first))
|> Enum.map(fn [first, second, char, password] ->
{
String.to_integer(first) - 1,
String.to_integer(second) - 1,
char,
String.graphemes(password)
}
end)
|> Enum.filter(fn {first, second, char, password} ->
first = Enum.at(password, first) == char
second = Enum.at(password, second) == char
(first && !second) || (!first && second)
end)
|> Enum.count()
|> IO.puts()
end
end
Day2Part2.run()

1000
day2/input Normal file

File diff suppressed because it is too large Load Diff