-
Notifications
You must be signed in to change notification settings - Fork 0
/
day_2.py
74 lines (49 loc) · 1.7 KB
/
day_2.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from typing import List
import logging
logging.basicConfig(level=logging.INFO)
# PART 1
def parse_text_file(filepath: str) -> List[int]:
with open(filepath, 'r') as f:
for line in f:
return [int(string) for string in line.split(',')]
list_of_ints = parse_text_file('day-2.txt')
Program = List[int]
def run_intcode_program(
list_of_ints: Program,
noun: int = 12,
verb: int = 2,
) -> Program:
list_of_ints = list_of_ints[:] # Make a copy
list_of_ints[1] = noun
list_of_ints[2] = verb
for index in range(0, len(list_of_ints), 4):
opcode = list_of_ints[index]
if opcode == 99:
return list_of_ints
first_position = list_of_ints[index + 1]
second_position = list_of_ints[index + 2]
output_position = list_of_ints[index + 3]
if opcode == 1:
list_of_ints[output_position] = (
list_of_ints[first_position] +
list_of_ints[second_position]
)
if opcode == 2:
list_of_ints[output_position] = (
list_of_ints[first_position] *
list_of_ints[second_position]
)
logging.info(
f'First value: {run_intcode_program(list_of_ints)[0]}'
)
# PART 2
EXPECTED_OUTPUT = 19690720
def find_noun_verb(list_of_ints: Program) -> Program:
for noun in range(0, 100):
for verb in range(0, 100):
program = list_of_ints[:] # Make a copy
output = run_intcode_program(program, noun=noun, verb=verb)[0]
if output == EXPECTED_OUTPUT:
return (noun, verb)
noun, verb = find_noun_verb(list_of_ints)
logging.info(f'100 * noun + verb = {100 * noun + verb}')