Day 3 Parts 1 and 2

This commit is contained in:
Adam Millerchip 2020-12-03 14:37:29 +09:00
parent a724754428
commit 031f92c680
6 changed files with 415 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
template/

View File

@ -2,7 +2,7 @@
My (attempted) solutions to [Advent of Code 2020](https://adventofcode.com/2020) in Elixir.
<img width="973" alt="image" src="https://user-images.githubusercontent.com/498229/100832848-05b99600-34ac-11eb-931f-4b3a95d428f7.png">
<img width="973" alt="image" src="https://user-images.githubusercontent.com/498229/100967992-db331000-3573-11eb-8a48-aa73d01f1a53.png">
## Strategy

34
day3/README Normal file
View File

@ -0,0 +1,34 @@
Day 3 Notes
+--------+
| Part 1 |
+--------|
$ elixir day3part1.exs
Travelled (969, 323) and encountereed 270 trees
Thoughts:
Input repeats, so use Stream.cycle/2.
Need to keep track of the x-offset, pass it to Enum.at/2.
+--------+
| Part 2 |
+--------|
$ elixir day3part2.exs
2122848000
Thoughts:
Modify original answer to take x and y velocity.
Can just pass x velocity in as before.
Use Enum.take_every/2 to skip unneeded rows for y velocity.
+------------------+
| Overall Thoughts |
+------------------+
Pretty simple, because Stream.cycle/2 takes care of the tricky bit.
Thankfully I answered part 1 in a way that made part 2 just a small change to add Enum.take_every/2.

22
day3/day3part1.exs Normal file
View File

@ -0,0 +1,22 @@
defmodule Day3Part1 do
def run do
{x, y, trees} =
File.read!("input")
|> String.trim()
|> String.split("\n")
|> Enum.map(&String.to_charlist/1)
|> Enum.reduce({0, 0, 0}, fn row, {x, y, trees} ->
trees =
case Stream.cycle(row) |> Enum.at(x) do
?# -> trees + 1
?. -> trees
end
{x + 3, y + 1, trees}
end)
IO.puts("Travelled (#{x}, #{y}) and encountereed #{trees} trees")
end
end
Day3Part1.run()

34
day3/day3part2.exs Normal file
View File

@ -0,0 +1,34 @@
defmodule Day3Part2 do
def run do
answer =
slope(1, 1) *
slope(3, 1) *
slope(5, 1) *
slope(7, 1) *
slope(1, 2)
IO.puts(answer)
end
def slope(v_x, v_y) do
{_, trees} =
File.read!("input")
|> String.trim()
|> String.split("\n")
|> Enum.take_every(v_y)
|> Enum.map(&String.to_charlist/1)
|> Enum.reduce({0, 0}, fn row, {x, trees} ->
trees =
case Stream.cycle(row) |> Enum.at(x) do
?# -> trees + 1
?. -> trees
end
{x + v_x, trees}
end)
trees
end
end
Day3Part2.run()

323
day3/input Normal file
View File

@ -0,0 +1,323 @@
...............#.#.............
##..#....................#...##
......#..#.#.....#..#.#.##.....
.........#...#..............#.#
............#.......##.........
...#.....#.....#...#.....#..#..
..............#..##.#..#......#
.##.....#.....#......##.#......
.#..........###....#...##....#.
.....#....#.#.......#......##..
.#....#......#.......#........#
..#.#.......#..##.....##.......
...#.#....#.......#.......#...#
##.##...##..#......#.#.....#..#
.#.#.......#..#.#......#...#.#.
#.......##.......#...#.........
.....#......#.#.#.....#....##..
.#.#........#....#..#..#.......
...#....#..###.........#.....#.
........#........#........#....
..##..............#.....#.#..#.
.#...##.............#.#........
....#..#...........#.......#...
..#....#.....................#.
#.#..................##......##
.#.##....#......#........#.....
.........##.....#....#...##..#.
#..........#..#.#.............#
.........#...#.#.#.#..##..##...
#...#.....#..#..#....#...#.....
..##.....#..................#..
#..###.....#....#.......#..#...
...##.##..#............#......#
........###.........###......#.
#..##....#.........#.........#.
....#.....................#....
#..#..##..#..####.##..#.....##.
..#...#.#....#....##.....#.....
...#.#.........#.....#.#.......
....#................#..#...##.
....#..#..........#...#.#.##...
........#..##............#....#
...#......##..........#.##...#.
.......##......................
.......##..........#....#.#...#
......###.##..##..#....#...#..#
#.#...........##.....#........#
..#...........#..###....#.#.#..
........#...........#......##..
.........#...##.###...###..#...
.....#.....#..##.........##....
...##..............#.....#...##
.##....#.......###.....#.......
.#...........##.............##.
......#..#..##.##......#......#
........###........#......#.#..
#.#....#.....#........#......#.
.##..#.........##...##....#....
.....#.........#...##.....#....
.............#........###....#.
......#.......#.#........#.#...
..#....#.#...#....#...#.#...##.
#...#......##..##......#.##.###
...##.#....#...#....#.........#
...#..####.....##.#..#.#...##..
##.#..#....##......#......##...
###.........#.#..#.#.....#.....
...#........#..##...#.#.#..#.#.
...###..#.###.#...#............
....................###........
...........#...........#.......
#..............#.#.........###.
....................##.....#..#
#.#.....#.......#...#..........
.#...#......#....##...#...#....
.....#.##..................###.
.........#.#..#.#......#.......
.......#.....##..#.##.#........
..#..........#.###.....#....#..
......#.............#.#........
........##....#........#.......
...#.............#....#.#......
#........#..####.....#.....#.#.
.##......##...#........#..#.#..
....##....#...#...#..##...#.#..
#.##...###..#....##.#..........
....#.#...#.#...#..##.###...#..
#.....##..#..#....#.#.....##...
.#..#..........##.#.....##.....
.#..#........#.#.#.#...........
.#..#.....#...........#...#....
...#......##..........##..#....
...#..#....#.##...#..#.....###.
#.#....#.....##................
#..#......#.#.#.......#........
......#....#.#....#..##....#..#
.#.....#.#....###.##.........#.
.###..#.....#........#.#.......
.#...#......#..#.#......#.....#
#...............####...#.....#.
.......#..........##.#........#
#........##....##.....###..##..
#..#.....#..##.....#....#..#...
#.....#.......##......#.#.....#
#.##..#......##..#.............
##...#.....#........##.........
....#..##....#...#.......#.#...
....#...#...##..#....#..#...#..
..............#.#...#....###...
...#....#..##...##..#....##....
#.##.#..#..#......#.#.#.#...#..
.......#..#..##........#......#
##.#....#....##.#......##.#....
.#...#..............#........#.
.#.#....#.........#............
.#..#..###.............#....#..
#......#...#.#..##..#...#....#.
.......................#...#.#.
.............#..#...##.........
..#.#..#....#....#........#....
#......#.##..#...#.#...........
.....#....#...........##.#..#..
..#.#.....#..............#.#...
#.......#.....#................
#..............#...#....#...#..
...#...##..#..#............#...
......###.....................#
.........#.......##..#....#....
........#...#.##..#.##......#..
....###..#.#...#...#..#.#...###
##...#...##.#...#.#...#.#....#.
.........#...#.....###.........
...#........##..#.......##.....
.#.......##.........#.....##..#
.#..................#...#......
.##..#..#.#.....#.###..........
...#.....##..#.........#...#...
.#......#.#.......#.#..........
.........#.#...#..........#.#..
#..........#.##..#.##....#.....
.#.#....#.....#..##.....#...#..
..#........##...##..#..#....#..
#...........##....#..###....#..
...........##.........####...#.
..#........###...#.#.........#.
.#...............#.##.#.#...#..
.#.##..#.....#.#.....##..#.....
...#...#..#.##.##...#.......##.
..#...#...#......##.##.##...#..
##....#...#...#...............#
...##...........#......#..#.#..
#.........#......#.#.##.....#..
........#..#.........##........
..#.#....###.....##..#...#.....
.........#...#.......#.....##..
##.....................#...##..
.#.#..#......#.................
.....###..#......#..###..#.....
...#.....##.........#......#..#
......##.....#...#........#.#..
..#.#...#......#...#.##.##.....
...#..........#...#.......#..##
.###........#........##........
..#.#.#..........#.#...##......
.........#........#......###..#
....##..#.........#...........#
..####..#............##.......#
.....##.#..##.........#...#.#..
...#.........#.....#.....#.....
.......#...#..#...##.........#.
...#...#..#...#....#..#........
#............##.##...#.........
.#.#.....#.......####.....#....
..............#......#.#.......
..............#...........#...#
#...#........###....#.#....#.#.
##.#..#..#......#......#.#.#...
.#..#.....#..#.#..#.#.......##.
......##.#...#...#......#...#..
#...........##....#.#..........
....#.......###.#...#..........
.......................#.....#.
........#...#..#...#.#.#.#.#...
.#.#...........#......##...#...
.........................#.....
.................#.##.#...##...
...#...##.....#.....##....#.#..
...#...#...................#...
...#..#..#...#...#....#........
#....#...#.....#...............
.......#...........#...#.......
....#....#.....##.......#......
.......#..........##...........
.#.#........#..##....#......#..
.....#.......#.#.........#...#.
.#..####.#.#...............#..#
.....###..#..#..........#.#..##
..#.......#...#.....##..#..#.#.
#....#......#..................
........#.##.#....#...........#
....#.#....##..#.#.....##......
...#..#.......#....#.....#.#.#.
#...#......#.....#.#..........#
....#....#...............#.....
..###......................###.
.##....#..#.......###.....#..#.
..###............#........#.##.
.#........#......#.....#..#....
....#..##...#...#.###.......#.#
.......#.##...........#.#..#...
.....#...##....................
....#....#...##......#.........
..#............##....###.#...#.
.#........#...............#....
#..#.#.##.........#..##....##..
#.#....#..#.##....##...#.#.....
.....#.....##....#.#........#..
#..#...#...#....#....#.........
...#........#..#.#.....##......
..#...#...#................##..
#........#.#.##.......#.#...#..
#......#..####.##.....#.#..#.#.
............#..#.#....#......##
..#.....##....#...#.#..........
...#...#.........#...#.#.......
.###..#.......##.##.....#.#.#..
...#....#...............##.#...
....##..#..#..#.#......##.....#
#.#..............##...##...####
.....#.##...#.#...............#
.##.....#.........#.......#.#.#
#.#..#.....#.......#.......#..#
...#.#.....#.....#......#......
.......#....#..#.#..........#..
......#......#.##...#..........
.....#.......###...#...#.#.....
#..#.#.........#.....#.##....#.
..#.#.........#..#..#..#.....#.
.#..##..#..#....#......#.##..#.
...##......###.....#.##.##.....
.#.....#...#..#...#............
##..##..#.##....#..#...........
...#..##..#..#.............#.##
...............##............#.
..#.....##........##.#...#....#
.#.#...#.#.#..#.#.....#....#...
.#....#...............#..#.....
....#.##..#....#......#...###..
#................###...#.#.....
...#...#......##..#.#....#.....
.#....#....#.#...##............
....#...##..#..#........#.##...
..##.....#..#..##..............
..#..##..#.#..##....#....#....#
...##.............#............
#....#....#.#........#.....##.#
.....#..#.#.....####...###.....
................#......#.......
.....#.#.#.#.#....#..#........#
.##.#...#.#.......##....#....#.
.....#........#................
..#.....#..#...#..#...........#
.#.....#...##.....##..#.#....##
......#.......#..#......##.#...
#.#..........#.##.#........#...
...#..#.............#..........
#..#..#..........#..##.#.......
.#..#...............####..#....
.......#.....#......#.....#.#..
.#...............#...#.........
.#..#..........#..#.#..##..#..#
......##..#.....#..#......###..
..........#...#..#.......#.....
.#.#.#..#.....#.##.#...#..#....
........#.......#.....#.#......
......#.....##.....#....##.#...
...............#......#.......#
..#.#...#.....#.#...##......#..
#.#.........#.#...#........####
#..........##..#..#........##..
.............#..#.......##.#..#
..#........#.#....#........#.#.
.#......####..#..#.............
............###.......#.#..#...
#.##......##...#...#.........#.
....##.#.#.#......#....#..#...#
.#..#.#....#...#.........#.....
#...#.....##............#...#..
#.#...#..#.................#...
............#.#..#.....#.#.#..#
...................#....#.##...
.....#...#.#....#....#.#......#
.......##.#.#......##..........
.#..#...##.#...#..#......#.....
......#.#..#..###..##..##......
.#.#.#.#.....#...###.....#..#..
.#....#.....#.......#.......#..
..........##.........####......
.#.#.............#..#.#...#....
........#........##...#.#....#.
........#......................
..#.#....#...............#...##
.......#.#...#..#.....##......#
.#...#....#..........##........
.#.........#.#............##...
.....#......##...#.......#..#..
#.#..#.............#...#...#...
......#.......#............#...
...........##....#......##.....
.#.#..#.....................#..
##..##.....###..##.#...........
...##......##....#...##.....#..
#...#.##.............#.........
......#..#.........###.#......#
#.#.....#.....................#
....#####.....##........#.#..#.
...........##..##.###..........
..........##.....#........#...#
.......#..#......#.....##..##.#
.....##.#........#.........#...
......##......................#
.#.......#.#.#............#..#.
.....##.#.......#.#........#...