Skip to content

Commit 41c6693

Browse files
committed
Day 10 complete
1 parent 253620d commit 41c6693

18 files changed

+640
-0
lines changed

2019/10/.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

2019/10/.gitignore

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where third-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
aoc-*.tar
24+

2019/10/.projections.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"lib/*.ex": {
3+
"type": "src",
4+
"alternate": "test/{}_test.exs",
5+
"template": [
6+
"# lib/{}.ex",
7+
"defmodule XXX do",
8+
"end"
9+
]
10+
},
11+
"test/*_test.exs": {
12+
"type": "test",
13+
"alternate": "lib/{}.ex",
14+
"template": [
15+
"# test/{}_test.exs",
16+
"defmodule XXX do",
17+
"end"
18+
]
19+
},
20+
}

2019/10/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# AOC
2+
3+
**TODO: Add description**
4+
5+
## Installation
6+
7+
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
8+
by adding `aoc` to your list of dependencies in `mix.exs`:
9+
10+
```elixir
11+
def deps do
12+
[
13+
{:aoc, "~> 0.1.0"}
14+
]
15+
end
16+
```
17+
18+
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
19+
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
20+
be found at [https://hexdocs.pm/aoc](https://hexdocs.pm/aoc).
21+

2019/10/config/config.exs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
use Mix.Config
4+
5+
# This configuration is loaded before any dependency and is restricted
6+
# to this project. If another project depends on this project, this
7+
# file won't be loaded nor affect the parent project. For this reason,
8+
# if you want to provide default values for your application for
9+
# third-party users, it should be done in your "mix.exs" file.
10+
11+
# You can configure your application as:
12+
#
13+
# config :aoc, key: :value
14+
#
15+
# and access this configuration in your application as:
16+
#
17+
# Application.get_env(:aoc, :key)
18+
#
19+
# You can also configure a third-party app:
20+
#
21+
# config :logger, level: :info
22+
#
23+
24+
# It is also possible to import configuration files, relative to this
25+
# directory. For example, you can emulate configuration per environment
26+
# by uncommenting the line below and defining dev.exs, test.exs and such.
27+
# Configuration from the imported file will override the ones defined
28+
# here (which is why it is important to import them last).
29+
#
30+
# import_config "#{Mix.env()}.exs"

2019/10/input.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
.###.###.###.#####.#
2+
#####.##.###..###..#
3+
.#...####.###.######
4+
######.###.####.####
5+
#####..###..########
6+
#.##.###########.#.#
7+
##.###.######..#.#.#
8+
.#.##.###.#.####.###
9+
##..#.#.##.#########
10+
###.#######.###..##.
11+
###.###.##.##..####.
12+
.##.####.##########.
13+
#######.##.###.#####
14+
#####.##..####.#####
15+
##.#.#####.##.#.#..#
16+
###########.#######.
17+
#.##..#####.#####..#
18+
#####..#####.###.###
19+
####.#.############.
20+
####.#.#.##########.

2019/10/instructions.md

+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
--- Day 10: Monitoring Station ---
2+
----------------------------------
3+
4+
You fly into the asteroid belt and reach the Ceres monitoring station.
5+
The Elves here have an emergency: they're having trouble tracking all of
6+
the asteroids and can't be sure they're safe.
7+
8+
The Elves would like to build a new monitoring station in a nearby area
9+
of space; they hand you a map of all of the asteroids in that region
10+
(your puzzle input).
11+
12+
The map indicates whether each position is empty (`.`) or contains an
13+
asteroid (`#`). The asteroids are much smaller than they appear on the
14+
map, and every asteroid is exactly in the center of its marked position.
15+
The asteroids can be described with `X,Y` coordinates where `X` is the
16+
distance from the left edge and `Y` is the distance from the top edge
17+
(so the top-left corner is `0,0` and the position immediately to its
18+
right is `1,0`).
19+
20+
Your job is to figure out which asteroid would be the best place to
21+
build a *new monitoring station*. A monitoring station can *detect* any
22+
asteroid to which it has *direct line of sight* - that is, there cannot
23+
be another asteroid *exactly* between them. This line of sight can be at
24+
any angle, not just lines aligned to the grid or
25+
[diagonally]{title="The Elves on Ceres are clearly not concerned with honor."}.
26+
The *best* location is the asteroid that can *detect* the largest number
27+
of other asteroids.
28+
29+
For example, consider the following map:
30+
31+
.#..#
32+
.....
33+
#####
34+
....#
35+
...##
36+
37+
The best location for a new monitoring station on this map is the
38+
highlighted asteroid at `3,4` because it can detect `8` asteroids, more
39+
than any other location. (The only asteroid it cannot detect is the one
40+
at `1,0`; its view of this asteroid is blocked by the asteroid at
41+
`2,2`.) All other asteroids are worse locations; they can detect `7` or
42+
fewer other asteroids. Here is the number of other asteroids a
43+
monitoring station on each asteroid could detect:
44+
45+
.7..7
46+
.....
47+
67775
48+
....7
49+
...87
50+
51+
Here is an asteroid (`#`) and some examples of the ways its line of
52+
sight might be blocked. If there were another asteroid at the location
53+
of a capital letter, the locations marked with the corresponding
54+
lowercase letter would be blocked and could not be detected:
55+
56+
#.........
57+
...A......
58+
...B..a...
59+
.EDCG....a
60+
..F.c.b...
61+
.....c....
62+
..efd.c.gb
63+
.......c..
64+
....f...c.
65+
...e..d..c
66+
67+
Here are some larger examples:
68+
69+
- Best is `5,8` with `33` other asteroids detected:
70+
71+
......#.#.
72+
#..#.#....
73+
..#######.
74+
.#.#.###..
75+
.#..#.....
76+
..#....#.#
77+
#..#....#.
78+
.##.#..###
79+
##...#..#.
80+
.#....####
81+
82+
- Best is `1,2` with `35` other asteroids detected:
83+
84+
#.#...#.#.
85+
.###....#.
86+
.#....#...
87+
##.#.#.#.#
88+
....#.#.#.
89+
.##..###.#
90+
..#...##..
91+
..##....##
92+
......#...
93+
.####.###.
94+
95+
- Best is `6,3` with `41` other asteroids detected:
96+
97+
.#..#..###
98+
####.###.#
99+
....###.#.
100+
..###.##.#
101+
##.##.#.#.
102+
....###..#
103+
..#.#..#.#
104+
#..#.#.###
105+
.##...##.#
106+
.....#.#..
107+
108+
- Best is `11,13` with `210` other asteroids detected:
109+
110+
.#..##.###...#######
111+
##.############..##.
112+
.#.######.########.#
113+
.###.#######.####.#.
114+
#####.##.#.##.###.##
115+
..#####..#.#########
116+
####################
117+
#.####....###.#.#.##
118+
##.#################
119+
#####.##.###..####..
120+
..######..##.#######
121+
####.##.####...##..#
122+
.#####..#.######.###
123+
##...#.##########...
124+
#.##########.#######
125+
.####.#.###.###.#.##
126+
....##.##.###..#####
127+
.#.#.###########.###
128+
#.#.#.#####.####.###
129+
###.##.####.##.#..##
130+
131+
Find the best location for a new monitoring station. *How many other
132+
asteroids can be detected from that location?*
133+
134+
Your puzzle answer was `214`.
135+
136+
The first half of this puzzle is complete! It provides one gold star: \*
137+
138+
--- Part Two --- {#part2}
139+
----------------
140+
141+
Once you give them the coordinates, the Elves quickly deploy an Instant
142+
Monitoring Station to the location and discover [the
143+
worst]{title="The Elves on Ceres just have a unique system of values, that's all."}:
144+
there are simply too many asteroids.
145+
146+
The only solution is *complete vaporization by giant laser*.
147+
148+
Fortunately, in addition to an asteroid scanner, the new monitoring
149+
station also comes equipped with a giant rotating laser perfect for
150+
vaporizing asteroids. The laser starts by pointing *up* and always
151+
rotates *clockwise*, vaporizing any asteroid it hits.
152+
153+
If multiple asteroids are *exactly* in line with the station, the laser
154+
only has enough power to vaporize *one* of them before continuing its
155+
rotation. In other words, the same asteroids that can be *detected* can
156+
be vaporized, but if vaporizing one asteroid makes another one
157+
detectable, the newly-detected asteroid won't be vaporized until the
158+
laser has returned to the same position by rotating a full 360 degrees.
159+
160+
For example, consider the following map, where the asteroid with the new
161+
monitoring station (and laser) is marked `X`:
162+
163+
.#....#####...#..
164+
##...##.#####..##
165+
##...#...#.#####.
166+
..#.....X...###..
167+
..#.#.....#....##
168+
169+
The first nine asteroids to get vaporized, in order, would be:
170+
171+
.#....###24...#..
172+
##...##.13#67..9#
173+
##...#...5.8####.
174+
..#.....X...###..
175+
..#.#.....#....##
176+
177+
Note that some asteroids (the ones behind the asteroids marked `1`, `5`,
178+
and `7`) won't have a chance to be vaporized until the next full
179+
rotation. The laser continues rotating; the next nine to be vaporized
180+
are:
181+
182+
.#....###.....#..
183+
##...##...#.....#
184+
##...#......1234.
185+
..#.....X...5##..
186+
..#.9.....8....76
187+
188+
The next nine to be vaporized are then:
189+
190+
.8....###.....#..
191+
56...9#...#.....#
192+
34...7...........
193+
..2.....X....##..
194+
..1..............
195+
196+
Finally, the laser completes its first full rotation (`1` through `3`),
197+
a second rotation (`4` through `8`), and vaporizes the last asteroid
198+
(`9`) partway through its third rotation:
199+
200+
......234.....6..
201+
......1...5.....7
202+
.................
203+
........X....89..
204+
.................
205+
206+
In the large example above (the one with the best monitoring station
207+
location at `11,13`):
208+
209+
- The 1st asteroid to be vaporized is at `11,12`.
210+
- The 2nd asteroid to be vaporized is at `12,1`.
211+
- The 3rd asteroid to be vaporized is at `12,2`.
212+
- The 10th asteroid to be vaporized is at `12,8`.
213+
- The 20th asteroid to be vaporized is at `16,0`.
214+
- The 50th asteroid to be vaporized is at `16,9`.
215+
- The 100th asteroid to be vaporized is at `10,16`.
216+
- The 199th asteroid to be vaporized is at `9,6`.
217+
- *The 200th asteroid to be vaporized is at `8,2`.*
218+
- The 201st asteroid to be vaporized is at `10,9`.
219+
- The 299th and final asteroid to be vaporized is at `11,1`.
220+
221+
The Elves are placing bets on which will be the *200th* asteroid to be
222+
vaporized. Win the bet by determining which asteroid that will be; *what
223+
do you get if you multiply its X coordinate by `100` and then add its Y
224+
coordinate?* (For example, `8,2` becomes *`802`*.)
225+
226+
Although it hasn't changed, you can still [get your puzzle
227+
input](10/input).
228+
229+
Answer:
230+
231+
You can also [\[Share[on
232+
[Twitter](https://twitter.com/intent/tweet?text=I%27ve+completed+Part+One+of+%22Monitoring+Station%22+%2D+Day+10+%2D+Advent+of+Code+2019&url=https%3A%2F%2Fadventofcode%2Ecom%2F2019%2Fday%2F10&related=ericwastl&hashtags=AdventOfCode)
233+
[Mastodon](javascript:void(0);)]{.share-content}\]]{.share} this puzzle.

2019/10/lib/aoc.ex

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
defmodule AOC do
2+
def run do
3+
AOC.Runner.run
4+
end
5+
end

2019/10/lib/aoc/line_of_sight.ex

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
defmodule AOC.LineOfSight do
2+
def polar(a, a), do: nil
3+
def polar({ox, oy}, {px, py}) do
4+
x = px - ox
5+
y = py - oy
6+
7+
distance = :math.sqrt(:math.pow(x, 2) + :math.pow(y, 2))
8+
angle = :math.atan2(y, x)
9+
angle = angle * 180/:math.pi
10+
angle = if angle < 0, do: angle + 360 , else: angle
11+
angle = angle - 270
12+
angle = if angle < 0, do: angle + 360 , else: angle
13+
14+
{ distance, angle, {px, py} }
15+
end
16+
end
17+

0 commit comments

Comments
 (0)