Skip to content

Commit 595293f

Browse files
committed
day 12
1 parent 773fc93 commit 595293f

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
|[09](https://adventofcode.com/2020/day/9)|Encoding Error|[py](/day09/main.py)|
1414
|[10](https://adventofcode.com/2020/day/10)|Adapter Array|[py](/day10/main.py), [alt](/day10/alt.py)|
1515
|[11](https://adventofcode.com/2020/day/11)|Seating System|[py](/day11/main.py)|
16-
|[12](https://adventofcode.com/2020/day/12)|-|-|
16+
|[12](https://adventofcode.com/2020/day/12)|Rain Risk|[py](/day12/main.py)|
1717
|[13](https://adventofcode.com/2020/day/13)|-|-|
1818
|[14](https://adventofcode.com/2020/day/14)|-|-|
1919
|[15](https://adventofcode.com/2020/day/15)|-|-|

day12/main.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
from parse import search
2+
3+
with open("input.txt") as f:
4+
code = [search("{}{:d}", x).fixed for x in f]
5+
6+
posx = 0
7+
posy = 0
8+
dirs = ["E", "S", "W", "N"]
9+
ptr = 0
10+
11+
for d, n in code:
12+
if d == "N": posy += n
13+
elif d == "S": posy -= n
14+
elif d == "E": posx += n
15+
elif d == "W": posx -= n
16+
elif d == "L": ptr = (ptr - n // 90) % len(dirs)
17+
elif d == "R": ptr = (ptr + n // 90) % len(dirs)
18+
elif d == "F":
19+
if dirs[ptr] == "N": posy += n
20+
elif dirs[ptr] == "S": posy -= n
21+
elif dirs[ptr] == "E": posx += n
22+
elif dirs[ptr] == "W": posx -= n
23+
24+
print(abs(posx)+abs(posy))
25+
26+
wpx = 10
27+
wpy = 1
28+
posx = 0
29+
posy = 0
30+
31+
for d, n in code:
32+
if d == "N": wpy += n
33+
elif d == "S": wpy -= n
34+
elif d == "E": wpx += n
35+
elif d == "W": wpx -= n
36+
elif d in ("R", "L") and n == 180:
37+
wpx, wpy = -wpx, -wpy
38+
elif d == "R":
39+
if n == 90: wpx, wpy = wpy, -wpx
40+
elif n == 270: wpx, wpy = -wpy, wpx
41+
elif d == "L":
42+
if n == 90: wpx, wpy = -wpy, wpx
43+
elif n == 270: wpx, wpy = wpy, -wpx
44+
elif d == "F":
45+
posx += n * wpx
46+
posy += n * wpy
47+
48+
print(abs(posx)+abs(posy))

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy