day 12
This commit is contained in:
764
day12/data/data.txt
Normal file
764
day12/data/data.txt
Normal file
File diff suppressed because it is too large
Load Diff
5
day12/data/sample.txt
Normal file
5
day12/data/sample.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
F10
|
||||
N3
|
||||
F7
|
||||
R90
|
||||
F11
|
||||
149
day12/index.html
Normal file
149
day12/index.html
Normal file
@@ -0,0 +1,149 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en-us">
|
||||
<head>
|
||||
<meta charset="utf-8"/>
|
||||
<title>Day 12 - Advent of Code 2020</title>
|
||||
<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
|
||||
<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'/>
|
||||
<link rel="stylesheet" type="text/css" href="/static/style.css?25"/>
|
||||
<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
|
||||
<link rel="shortcut icon" href="/favicon.png"/>
|
||||
</head><!--
|
||||
|
||||
|
||||
|
||||
|
||||
Oh, hello! Funny seeing you here.
|
||||
|
||||
I appreciate your enthusiasm, but you aren't going to find much down here.
|
||||
There certainly aren't clues to any of the puzzles. The best surprises don't
|
||||
even appear in the source until you unlock them for real.
|
||||
|
||||
Please be careful with automated requests; I'm not a massive company, and I can
|
||||
only take so much traffic. Please be considerate so that everyone gets to play.
|
||||
|
||||
If you're curious about how Advent of Code works, it's running on some custom
|
||||
Perl code. Other than a few integrations (auth, analytics, social media), I
|
||||
built the whole thing myself, including the design, animations, prose, and all
|
||||
of the puzzles.
|
||||
|
||||
The puzzles are most of the work; preparing a new calendar and a new set of
|
||||
puzzles each year takes all of my free time for 4-5 months. A lot of effort
|
||||
went into building this thing - I hope you're enjoying playing it as much as I
|
||||
enjoyed making it for you!
|
||||
|
||||
If you'd like to hang out, I'm @ericwastl on Twitter.
|
||||
|
||||
- Eric Wastl
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-->
|
||||
<body>
|
||||
<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2020/about">[About]</a></li><li><a href="/2020/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2020/settings">[Settings]</a></li><li><a href="/2020/auth/logout">[Log Out]</a></li></ul></nav><div class="user">m42e <a href="/2020/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">22*</span></div></div><div><h1 class="title-event"> <span class="title-event-wrap">λy.</span><a href="/2020">2020</a><span class="title-event-wrap"></span></h1><nav><ul><li><a href="/2020">[Calendar]</a></li><li><a href="/2020/support">[AoC++]</a></li><li><a href="/2020/sponsors">[Sponsors]</a></li><li><a href="/2020/leaderboard">[Leaderboard]</a></li><li><a href="/2020/stats">[Stats]</a></li></ul></nav></div></header>
|
||||
|
||||
<div id="sidebar">
|
||||
<div id="sponsor"><div class="quiet">Our <a href="/2020/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://github.com/" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">GitHub</a> - We're hiring engineers to make GitHub fast. Interested? Email fast@github.com with details of exceptional performance work you've done in the past.</div></div>
|
||||
</div><!--/sidebar-->
|
||||
|
||||
<main>
|
||||
<script>window.addEventListener('click', function(e,s,r){if(e.target.nodeName==='CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(e.target);s.addRange(r);}});</script>
|
||||
<article class="day-desc"><h2>--- Day 12: Rain Risk ---</h2><p>Your ferry made decent progress toward the island, but the storm came in <span title="At least it wasn't a Category Six!">faster than anyone expected</span>. The ferry needs to take <em>evasive actions</em>!</p>
|
||||
<p>Unfortunately, the ship's navigation computer seems to be malfunctioning; rather than giving a route directly to safety, it produced extremely circuitous instructions. When the captain uses the <a href="https://en.wikipedia.org/wiki/Public_address_system" target="_blank">PA system</a> to ask if anyone can help, you quickly volunteer.</p>
|
||||
<p>The navigation instructions (your puzzle input) consists of a sequence of single-character <em>actions</em> paired with integer input <em>values</em>. After staring at them for a few minutes, you work out what they probably mean:</p>
|
||||
<ul>
|
||||
<li>Action <em><code>N</code></em> means to move <em>north</em> by the given value.</li>
|
||||
<li>Action <em><code>S</code></em> means to move <em>south</em> by the given value.</li>
|
||||
<li>Action <em><code>E</code></em> means to move <em>east</em> by the given value.</li>
|
||||
<li>Action <em><code>W</code></em> means to move <em>west</em> by the given value.</li>
|
||||
<li>Action <em><code>L</code></em> means to turn <em>left</em> the given number of degrees.</li>
|
||||
<li>Action <em><code>R</code></em> means to turn <em>right</em> the given number of degrees.</li>
|
||||
<li>Action <em><code>F</code></em> means to move <em>forward</em> by the given value in the direction the ship is currently facing.</li>
|
||||
</ul>
|
||||
<p>The ship starts by facing <em>east</em>. Only the <code>L</code> and <code>R</code> actions change the direction the ship is facing. (That is, if the ship is facing east and the next instruction is <code>N10</code>, the ship would move north 10 units, but would still move east if the following action were <code>F</code>.)</p>
|
||||
<p>For example:</p>
|
||||
<pre><code>F10
|
||||
N3
|
||||
F7
|
||||
R90
|
||||
F11
|
||||
</code></pre>
|
||||
<p>These instructions would be handled as follows:</p>
|
||||
<ul>
|
||||
<li><code>F10</code> would move the ship 10 units east (because the ship starts by facing east) to <em>east 10, north 0</em>.</li>
|
||||
<li><code>N3</code> would move the ship 3 units north to <em>east 10, north 3</em>.</li>
|
||||
<li><code>F7</code> would move the ship another 7 units east (because the ship is still facing east) to <em>east 17, north 3</em>.</li>
|
||||
<li><code>R90</code> would cause the ship to turn right by 90 degrees and face <em>south</em>; it remains at <em>east 17, north 3</em>.</li>
|
||||
<li><code>F11</code> would move the ship 11 units south to <em>east 17, south 8</em>.</li>
|
||||
</ul>
|
||||
<p>At the end of these instructions, the ship's <a href="https://en.wikipedia.org/wiki/Manhattan_distance" target="_blank">Manhattan distance</a> (sum of the absolute values of its east/west position and its north/south position) from its starting position is <code>17 + 8</code> = <em><code>25</code></em>.</p>
|
||||
<p>Figure out where the navigation instructions lead. <em>What is the Manhattan distance between that location and the ship's starting position?</em></p>
|
||||
</article>
|
||||
<p>To begin, <a href="12/input" target="_blank">get your puzzle input</a>.</p>
|
||||
<form method="post" action="12/answer"><input type="hidden" name="level" value="1"/><p>Answer: <input type="text" name="answer" autocomplete="off"/> <input type="submit" value="[Submit]"/></p></form>
|
||||
<p>You can also <span class="share">[Share<span class="share-content">on
|
||||
<a href="https://twitter.com/intent/tweet?text=%22Rain+Risk%22+%2D+Day+12+%2D+Advent+of+Code+2020&url=https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F12&related=ericwastl&hashtags=AdventOfCode" target="_blank">Twitter</a>
|
||||
<a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' && mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=%22Rain+Risk%22+%2D+Day+12+%2D+Advent+of+Code+2020+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F12'}else{return false;}" target="_blank">Mastodon</a
|
||||
></span>]</span> this puzzle.</p>
|
||||
</main>
|
||||
|
||||
<!-- ga -->
|
||||
<script>
|
||||
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
||||
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
||||
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
||||
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
||||
ga('create', 'UA-69522494-1', 'auto');
|
||||
ga('set', 'anonymizeIp', true);
|
||||
ga('send', 'pageview');
|
||||
</script>
|
||||
<!-- /ga -->
|
||||
</body>
|
||||
</html>
|
||||
96
day12/module.py
Normal file
96
day12/module.py
Normal file
@@ -0,0 +1,96 @@
|
||||
from aoc.input import get_input
|
||||
import copy
|
||||
import itertools
|
||||
import time
|
||||
import collections
|
||||
import re
|
||||
from aoc.partselector import part_one, part_two
|
||||
import functools
|
||||
|
||||
def pw(line):
|
||||
l = line.strip()
|
||||
return l[0], int(l[1:])
|
||||
|
||||
def p1():
|
||||
inp = get_input(pw)
|
||||
go = {
|
||||
'N': (0, -1),
|
||||
'S': (0, 1),
|
||||
'W': (-1, 0),
|
||||
'E': (1, 0),
|
||||
}
|
||||
pos = (0, 0)
|
||||
doffset = 1
|
||||
for sample in inp:
|
||||
print(sample)
|
||||
d, v, = sample
|
||||
if sample[0] in 'NWSE':
|
||||
pos = (pos[0] + sample[1]*go[d][0],pos[1] + sample[1]*go[d][1])
|
||||
if d == 'L':
|
||||
doffset -= v//90
|
||||
if d == 'R':
|
||||
doffset += v//90
|
||||
if d == 'F':
|
||||
di = 'NESW'
|
||||
d = di[doffset%4]
|
||||
pos = (pos[0] + sample[1]*go[d][0],pos[1] + sample[1]*go[d][1])
|
||||
print(pos)
|
||||
|
||||
|
||||
|
||||
print(pos)
|
||||
print(abs(pos[0]) + abs(pos[1]))
|
||||
return inp
|
||||
|
||||
|
||||
def p2(inp):
|
||||
go = {
|
||||
'N': (0, -1),
|
||||
'S': (0, 1),
|
||||
'W': (-1, 0),
|
||||
'E': (1, 0),
|
||||
}
|
||||
pos = (0, 0)
|
||||
wp = (10, -1)
|
||||
doffset = 1
|
||||
for sample in inp:
|
||||
print(sample)
|
||||
d, v, = sample
|
||||
if sample[0] in 'NWSE':
|
||||
wp = (wp[0] + sample[1]*go[d][0],wp[1] + sample[1]*go[d][1])
|
||||
if d == 'L':
|
||||
doffset -= v//90
|
||||
dd = (v//90)%4
|
||||
if dd == 2:
|
||||
wp = (-wp[0], -wp[1])
|
||||
if dd == 1:
|
||||
wp = (wp[1], -wp[0])
|
||||
if dd == 3:
|
||||
wp = (-wp[1], wp[0])
|
||||
if d == 'R':
|
||||
dd = (v//90)%4
|
||||
if dd == 2:
|
||||
wp = (-wp[0], -wp[1])
|
||||
if dd == 3:
|
||||
wp = (wp[1], -wp[0])
|
||||
if dd == 1:
|
||||
wp = (-wp[1], wp[0])
|
||||
if d == 'F':
|
||||
pos = (pos[0] + sample[1]*wp[0],pos[1] + sample[1]*wp[1])
|
||||
print(pos, wp)
|
||||
print(abs(pos[0]) + abs(pos[1]))
|
||||
return 0
|
||||
|
||||
result1 = None
|
||||
if part_one():
|
||||
start = time.time()
|
||||
result1 = p1()
|
||||
print(round(1000*(time.time() - start), 2), 'ms')
|
||||
|
||||
|
||||
if part_two():
|
||||
start = time.time()
|
||||
p2(result1)
|
||||
print(round(1000*(time.time() - start), 2), 'ms')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user