2022 day 9
This commit is contained in:
2000
2022/09/input
Normal file
2000
2022/09/input
Normal file
File diff suppressed because it is too large
Load Diff
40
2022/09/part1
Executable file
40
2022/09/part1
Executable file
@@ -0,0 +1,40 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
MOVES = {
|
||||||
|
'U': ( 0, -1),
|
||||||
|
'D': ( 0, +1),
|
||||||
|
'L': (-1, 0),
|
||||||
|
'R': (+1, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
TAIL_POSITIONS = set([])
|
||||||
|
HEAD = (0, 0)
|
||||||
|
TAIL = (0, 0)
|
||||||
|
|
||||||
|
def point_add(a, b):
|
||||||
|
return tuple(map(lambda i, j: i + j, a, b))
|
||||||
|
|
||||||
|
def move(delta):
|
||||||
|
global TAIL_POSITIONS, HEAD, TAIL
|
||||||
|
|
||||||
|
last_head = HEAD
|
||||||
|
HEAD = point_add(HEAD, delta)
|
||||||
|
|
||||||
|
if ed := math.dist(HEAD, TAIL) < 2.0:
|
||||||
|
_ # do nothing
|
||||||
|
elif ed == 2.0:
|
||||||
|
TAIL = point_add(TAIL, delta)
|
||||||
|
else:
|
||||||
|
TAIL = last_head
|
||||||
|
|
||||||
|
TAIL_POSITIONS.add(TAIL)
|
||||||
|
|
||||||
|
with open('input') as f:
|
||||||
|
for line in f:
|
||||||
|
direction, count = line.rstrip().split(" ")
|
||||||
|
for _ in range(int(count)):
|
||||||
|
move(MOVES[direction])
|
||||||
|
|
||||||
|
print(len(TAIL_POSITIONS))
|
60
2022/09/part2
Executable file
60
2022/09/part2
Executable file
@@ -0,0 +1,60 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import math
|
||||||
|
|
||||||
|
MOVES = {
|
||||||
|
'U': ( 0, -1),
|
||||||
|
'D': ( 0, +1),
|
||||||
|
'L': (-1, 0),
|
||||||
|
'R': (+1, 0),
|
||||||
|
}
|
||||||
|
|
||||||
|
TAIL_POSITIONS = set([])
|
||||||
|
HEAD = (0, 0)
|
||||||
|
TAILS = [(0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0), (0, 0)]
|
||||||
|
|
||||||
|
def point_add(a, b):
|
||||||
|
return tuple(map(lambda i, j: i + j, a, b))
|
||||||
|
|
||||||
|
def move(delta):
|
||||||
|
global TAIL_POSITIONS, HEAD, TAIL
|
||||||
|
|
||||||
|
HEAD = point_add(HEAD, delta)
|
||||||
|
|
||||||
|
leader = HEAD
|
||||||
|
for idx, tail in enumerate(TAILS):
|
||||||
|
ed = math.dist(leader, tail);
|
||||||
|
if ed < 2.0:
|
||||||
|
_ # do nothing
|
||||||
|
elif ed == 2.0: # follow the leader: linear
|
||||||
|
x, y = 0,0
|
||||||
|
if leader[0] < tail[0]:
|
||||||
|
x = -1
|
||||||
|
elif leader[0] > tail[0]:
|
||||||
|
x = 1
|
||||||
|
if leader[1] < tail[1]:
|
||||||
|
y = -1
|
||||||
|
elif leader[1] > tail[1]:
|
||||||
|
y = 1
|
||||||
|
TAILS[idx] = point_add(tail, (x,y))
|
||||||
|
else: # follow the leader: diagonal
|
||||||
|
x, y = 1, 1
|
||||||
|
if leader[0] < tail[0]:
|
||||||
|
x = -1
|
||||||
|
if leader[1] < tail[1]:
|
||||||
|
y = -1
|
||||||
|
TAILS[idx] = point_add(tail, (x,y))
|
||||||
|
|
||||||
|
leader = TAILS[idx]
|
||||||
|
|
||||||
|
TAIL_POSITIONS.add(TAILS[-1])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
with open('input') as f:
|
||||||
|
for line in f:
|
||||||
|
direction, count = line.rstrip().split(" ")
|
||||||
|
for _ in range(int(count)):
|
||||||
|
move(MOVES[direction])
|
||||||
|
|
||||||
|
print(len(TAIL_POSITIONS))
|
@@ -9,7 +9,7 @@ a programming language I'm interested in learning more about.
|
|||||||
2018, 2019, and 2020 were in their own separate repositories, but have been
|
2018, 2019, and 2020 were in their own separate repositories, but have been
|
||||||
`git subtree`'d into this one - no point creating a new repository every year.
|
`git subtree`'d into this one - no point creating a new repository every year.
|
||||||
|
|
||||||
* [2022](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2022) 🌟 `16` - Python
|
* [2022](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2022) 🌟 `18` - Python
|
||||||
* [2021](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2021) 🌟 ` 5` - Python
|
* [2021](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2021) 🌟 ` 5` - Python
|
||||||
* [2020](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2020) 🌟 `20` - Javascript
|
* [2020](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2020) 🌟 `20` - Javascript
|
||||||
* [2019](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2019) 🌟 `22` - Zig
|
* [2019](https://code.ur.gs/lupine/advent-of-code/src/branch/main/2019) 🌟 `22` - Zig
|
||||||
@@ -18,6 +18,6 @@ a programming language I'm interested in learning more about.
|
|||||||
* 2016 🌟 `16` - LISP?
|
* 2016 🌟 `16` - LISP?
|
||||||
* 2015 🌟 ` 9` - Ruby?
|
* 2015 🌟 ` 9` - Ruby?
|
||||||
|
|
||||||
Total: 🌟 `94`
|
Total: 🌟 `96`
|
||||||
|
|
||||||
Maybe I'll backfill some of the old years one day.
|
Maybe I'll backfill some of the old years one day.
|
||||||
|
Reference in New Issue
Block a user