summaryrefslogtreecommitdiffstats
path: root/2.py
blob: 618cd86d23ae45b150508323bb71896105a16bec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Command = tuple[str, int]

def part1(commands: list[Command]) -> int:
    pos: int = 0
    depth: int = 0
    for command in commands:
        match command:
            case ('up',      X): depth -= X
            case ('down',    X): depth += X
            case ('forward', X): pos += X
    return pos * depth

def part2(commands: list[Command]) -> int:
    pos: int = 0
    aim: int = 0
    depth: int = 0
    for command in commands:
        match command:
            case ('up',      X): aim -= X
            case ('down',    X): aim += X
            case ('forward', X):
                pos += X
                depth += aim * X
    return pos * depth

commands: list[Command] = [(c, int(q)) for c, q in (l.split() for l in open('2.in'))]
print(part1(commands))
print(part2(commands))