Skip to content

Commit ef1cb8a

Browse files
committed
Solved Day 4 of AOC2019
1 parent 0ab0c00 commit ef1cb8a

File tree

9 files changed

+167
-38
lines changed

9 files changed

+167
-38
lines changed

2019/Day4.R

+58-8
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,74 @@ rm(list = ls())
55
input <- readLines("2019/data/Day4.txt")
66
input <- as.integer(unlist(strsplit(input,"-")))
77

8-
password <- function(min = input[1], max = input[2]) {
8+
passwords <- function(min = input[1], max = input[2]) {
99

1010
require(stringi)
1111
require(dplyr)
1212
vector <- as.character(min:max)
13-
dbl_digits <- stringi::stri_count_regex(vector, pattern = "(\\d)\\1+")
14-
vector <- vector[which(dbl_digits == 1)]
1513

16-
vector <- strsplit(vector, "")
17-
vector <- lapply(vector, as.integer)
18-
19-
return(vector)
14+
vh <- tibble(d1 = as.integer(substring(vector, 1, 1)),
15+
d2 = as.integer(substring(vector, 2, 2)),
16+
d3 = as.integer(substring(vector, 3, 3)),
17+
d4 = as.integer(substring(vector, 4, 4)),
18+
d5 = as.integer(substring(vector, 5, 5)),
19+
d6 = as.integer(substring(vector, 6, 6))
20+
)
21+
22+
vh <- vh %>%
23+
filter(d6 >= d5 & d5 >= d4 & d4 >= d3 & d3 >= d2 & d2 >= d1) %>%
24+
filter(d1 == d2 | d2 == d3 | d3 == d4 | d4 == d5 | d5 == d6)
25+
26+
return(vh)
2027

2128

2229
}
2330

24-
password()
31+
nrow(passwords())
2532

33+
# Explanation
34+
# The first two conditions (range and 6 digits) are fulfilled by default, so we only check for the other two.
35+
# The function splits the number in string format and puts every digit in integer form into a separate column.
36+
# From there, we filter using two criteria: The number in the column to the right can be equal or larger,
37+
# and numbers in any two adjacent columns must be equal.
2638

2739

2840
# Part 2 ------------------------------------------------------------------
41+
# Process the input in form of XXXXXX-YYYYYY
42+
rm(list = ls())
43+
input <- readLines("2019/data/Day4.txt")
44+
input <- as.integer(unlist(strsplit(input,"-")))
45+
46+
passwords <- function(min = input[1], max = input[2]) {
47+
48+
require(stringi)
49+
require(dplyr)
50+
vector <- as.character(min:max)
51+
52+
vh <- tibble(d1 = as.integer(substring(vector, 1, 1)),
53+
d2 = as.integer(substring(vector, 2, 2)),
54+
d3 = as.integer(substring(vector, 3, 3)),
55+
d4 = as.integer(substring(vector, 4, 4)),
56+
d5 = as.integer(substring(vector, 5, 5)),
57+
d6 = as.integer(substring(vector, 6, 6))
58+
)
59+
60+
vh <- vh %>%
61+
filter(d6 >= d5 & d5 >= d4 & d4 >= d3 & d3 >= d2 & d2 >= d1) %>%
62+
filter((d1 == d2 & d2 != d3)|
63+
(d2 == d3 & (d2 != d1 & d3 != d4)) |
64+
(d3 == d4 & (d3 != d2 & d4 != d5)) |
65+
(d4 == d5 & (d4 != d3 & d5 != d6)) |
66+
(d5 == d6 & d5 != d4))
67+
68+
return(vh)
69+
70+
71+
}
72+
73+
nrow(passwords())
74+
75+
# Explanation
76+
# The same principle with slight modification, in the second filtering condition, we assure that
77+
# adjacent numbers are the same, but also exclude the cases when adjacent numbers to our compared pair
78+
# are the same as any one of the numbers in the compared pair.

2019/Day5.R

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Part 1 ------------------------------------------------------------------
2+
rm(list = ls())
3+
4+
5+
6+
7+
8+
# Part 2 ------------------------------------------------------------------

2019/Day7.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Part 1 ------------------------------------------------------------------
2-
2+
rm(list = ls())
33

44

55

2019/README.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Advent of Code 2019
22

3-
[x] Day 1 | [Solution](2019/Day1.R) | [Data](2019/data/Day1.txt) | [Assignment](2019/assignments/Day1.md)
4-
[x] Day 2 | [Solution](2019/Day2.R) | [Data](2019/data/Day2.txt) | [Assignment](2019/assignments/Day2.md)
5-
[x] Day 3 | [Solution](2019/Day3.R) | [Data](2019/data/Day3.txt) | [Assignment](2019/assignments/Day3.md) *thanks to [u/turtlegraphics](https://www.reddit.com/user/turtlegraphics/)*
6-
[ ] Day 4 | [Solution](2019/Day4.R) | [Data](2019/data/Day4.txt) | [Assignment](2019/assignments/Day4.md)
7-
[ ] Day 5 | [Solution](2019/Day5.R) | [Data](2019/data/Day5.txt) | [Assignment](2019/assignments/Day5.md)
8-
[x] Day 6 | [Solution](2019/Day6.R) | [Data](2019/data/Day6.txt) | [Assignment](2019/assignments/Day6.md)
9-
[ ] Day 7 | [Solution](2019/Day7.R) | [Data](2019/data/Day7.txt) | [Assignment](2019/assignments/Day7.md)
3+
[x] Day 1 | [Solution](Day1.R) | [Data](data/Day1.txt) | [Assignment](assignments/Day1.md)
4+
[x] Day 2 | [Solution](Day2.R) | [Data](data/Day2.txt) | [Assignment](assignments/Day2.md)
5+
[x] Day 3 | [Solution](Day3.R) | [Data](data/Day3.txt) | [Assignment](assignments/Day3.md) *thanks to [u/turtlegraphics](https://www.reddit.com/user/turtlegraphics/)*
6+
[ ] Day 4 | [Solution](Day4.R) | [Data](data/Day4.txt) | [Assignment](assignments/Day4.md)
7+
[ ] Day 5 | [Solution](Day5.R) | [Data](data/Day5.txt) | [Assignment](assignments/Day5.md)
8+
[x] Day 6 | [Solution](Day6.R) | [Data](data/Day6.txt) | [Assignment](assignments/Day6.md)
9+
[ ] Day 7 | [Solution](Day7.R) | [Data](data/Day7.txt) | [Assignment](assignments/Day7.md)
1010
[ ] Day 8
1111
[ ] Day 9
1212
[ ] Day 10

2019/assignments/Day4.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,15 @@ Other than the range rule, the following are true:
1616
* 223450 does not meet these criteria (decreasing pair of digits 50).
1717
* 123789 does not meet these criteria (no double).
1818

19-
*How many different passwords within the range given in your puzzle input meet these criteria?*
19+
*How many different passwords within the range given in your puzzle input meet these criteria?*
20+
21+
**--- Part Two ---**
22+
An Elf just remembered one more important detail: the two adjacent matching digits are not part of a larger group of matching digits.
23+
24+
Given this additional criterion, but still ignoring the range rule, the following are now true:
25+
26+
* 112233 meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
27+
* 123444 no longer meets the criteria (the repeated 44 is part of a larger group of 444).
28+
* 111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
29+
30+
How many different passwords within the range given in your puzzle input meet all of the criteria?

2019/assignments/Day5.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
**--- Day 5: Sunny with a Chance of Asteroids ---**
2+
3+
You're starting to sweat as the ship makes its way toward Mercury. The Elves suggest that you get the air conditioner working by upgrading your ship computer to support the Thermal Environment Supervision Terminal.
4+
5+
The Thermal Environment Supervision Terminal (TEST) starts by running a diagnostic program (your puzzle input). The TEST diagnostic program will run on your existing Intcode computer after a few modifications:
6+
7+
First, you'll need to add two new instructions:
8+
9+
* Opcode 3 takes a single integer as input and saves it to the position given by its only parameter. For example, the instruction 3,50 would take an input value and store it at address 50.
10+
* Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
11+
12+
Programs that use these instructions will come with documentation that explains what should be connected to the input and output. The program 3,0,4,0,99 outputs whatever it gets as input, then halts.
13+
14+
Second, you'll need to add support for parameter modes:
15+
16+
Each parameter of an instruction is handled based on its parameter mode. Right now, your ship computer already understands parameter mode 0, position mode, which causes the parameter to be interpreted as a position - if the parameter is 50, its value is the value stored at address 50 in emory. Until now, all parameters have been in position mode.
17+
18+
Now, your ship computer will also need to handle parameters in mode 1, immediate mode. In immediate mode, a parameter is interpreted as a value - if the parameter is 50, its value is simply 50.
19+
20+
Parameter modes are stored in the same value as the instruction's opcode. The opcode is a two-digit number based only on the ones and tens digit of the value, that is, the opcode is the rightmost two digits of the first value in an instruction. Parameter modes are single digits, one per parameter, read right-to-left from the opcode: the first parameter's mode is in the hundreds digit, the second parameter's mode is in the thousands digit, the third parameter's mode is in the ten-thousands digit, and so on. Any missing modes are 0.
21+
22+
For example, consider the program 1002,4,3,4,33.
23+
24+
The first instruction, 1002,4,3,4, is a multiply instruction - the rightmost two digits of the first value, 02, indicate opcode 2, multiplication. Then, going right to left, the parameter modes are 0 (hundreds digit), 1 (thousands digit), and 0 (ten-thousands digit, not present and therefore zero):
25+
26+
<pre>
27+
ABCDE
28+
1002
29+
30+
DE - two-digit opcode, 02 == opcode 2
31+
C - mode of 1st parameter, 0 == position mode
32+
B - mode of 2nd parameter, 1 == immediate mode
33+
A - mode of 3rd parameter, 0 == position mode,
34+
omitted due to being a leading zero
35+
</pre>
36+
37+
This instruction multiplies its first two parameters. The first parameter, 4 in position mode, works like it did before - its value is the value stored at address 4 (33). The second parameter, 3 in immediate mode, simply has value 3. The result of this operation, 33 * 3 = 99, is written according to the third parameter, 4 in position mode, which also works like it did before - 99 is written to address 4.
38+
39+
Parameters that an instruction writes to will never be in immediate mode.
40+
41+
Finally, some notes:
42+
43+
It is important to remember that the instruction pointer should increase by the number of values in the instruction after the instruction finishes. Because of the new instructions, this amount is no longer always 4.
44+
45+
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
46+
The TEST diagnostic program will start by requesting from the user the ID of the system to test by running an input instruction - provide it 1, the ID for the ship's air conditioner unit.
47+
48+
It will then perform a series of diagnostic tests confirming that various parts of the Intcode computer, like parameter modes, function correctly. For each test, it will run an output instruction indicating how far the result of the test was from the expected value, where 0 means the test was successful. Non-zero outputs mean that a function is not working correctly; check the instructions that were run before the output instruction to see which one failed.
49+
50+
Finally, the program will output a diagnostic code and immediately halt. This final output isn't an error; an output followed immediately by a halt means the program finished. If all outputs were zero except the diagnostic code, the diagnostic program ran successfully.
51+
52+
After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

2019/assignments/Day7.md

+27-21
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,47 @@
11
**--- Day 7: Amplification Circuit ---**
22

3-
Based on the navigational maps, you're going to need to send more power to your ship's thrusters to reach Santa in time. To do this, you'll need to configure a series of amplifiers already installed on the ship.
3+
Based on the navigational maps, you're going to need to send more power to your ship's thrusters to reach Santa in time. To do this, you'll need to configure a series of amplifiers already installed on the ship.
44

5-
There are five amplifiers connected in series; each one receives an input signal and produces an output signal. They are connected such that the first amplifier's output leads to the second amplifier's input, the second amplifier's output leads to the third amplifier's input, and so on. The first amplifier's input value is 0, and the last amplifier's output leads to your ship's thrusters.
5+
There are five amplifiers connected in series; each one receives an input signal and produces an output signal. They are connected such that the first amplifier's output leads to the second amplifier's input, the second amplifier's output leads to the third amplifier's input, and so on. The first amplifier's input value is 0, and the last amplifier's output leads to your ship's thrusters.
66

7+
<pre>
78
O-------O O-------O O-------O O-------O O-------O
89
0 ->| Amp A |->| Amp B |->| Amp C |->| Amp D |->| Amp E |-> (to thrusters)
910
O-------O O-------O O-------O O-------O O-------O
11+
</pre>
1012

11-
The Elves have sent you some Amplifier Controller Software (your puzzle input), a program that should run on your existing Intcode computer. Each amplifier will need to run a copy of the program.
13+
The Elves have sent you some Amplifier Controller Software (your puzzle input), a program that should run on your existing Intcode computer. Each amplifier will need to run a copy of the program.
1214

13-
When a copy of the program starts running on an amplifier, it will first use an input instruction to ask the amplifier for its current phase setting (an integer from 0 to 4). Each phase setting is used exactly once, but the Elves can't remember which amplifier needs which phase setting.
15+
When a copy of the program starts running on an amplifier, it will first use an input instruction to ask the amplifier for its current phase setting (an integer from 0 to 4). Each phase setting is used exactly once, but the Elves can't remember which amplifier needs which phase setting.
1416

15-
The program will then call another input instruction to get the amplifier's input signal, compute the correct output signal, and supply it back to the amplifier with an output instruction. (If the amplifier has not yet received an input signal, it waits until one arrives.)
17+
The program will then call another input instruction to get the amplifier's input signal, compute the correct output signal, and supply it back to the amplifier with an output instruction. (If the amplifier has not yet received an input signal, it waits until one arrives.)
1618

17-
Your job is to find the largest output signal that can be sent to the thrusters by trying every possible combination of phase settings on the amplifiers. Make sure that memory is not shared or reused between copies of the program.
19+
Your job is to find the largest output signal that can be sent to the thrusters by trying every possible combination of phase settings on the amplifiers. Make sure that memory is not shared or reused between copies of the program.
1820

19-
For example, suppose you want to try the phase setting sequence 3,1,2,4,0, which would mean setting amplifier A to phase setting 3, amplifier B to setting 1, C to 2, D to 4, and E to 0. Then, you could determine the output signal that gets sent from amplifier E to the thrusters with the following steps:
21+
For example, suppose you want to try the phase setting sequence 3,1,2,4,0, which would mean setting amplifier A to phase setting 3, amplifier B to setting 1, C to 2, D to 4, and E to 0. Then, you could determine the output signal that gets sent from amplifier E to the thrusters with the following steps
2022

21-
Start the copy of the amplifier controller software that will run on amplifier A. At its first input instruction, provide it the amplifier's phase setting, 3. At its second input instruction, provide it the input signal, 0. After some calculations, it will use an output instruction to indicate the amplifier's output signal.
22-
Start the software for amplifier B. Provide it the phase setting (1) and then whatever output signal was produced from amplifier A. It will then produce a new output signal destined for amplifier C.
23-
Start the software for amplifier C, provide the phase setting (2) and the value from amplifier B, then collect its output signal.
24-
Run amplifier D's software, provide the phase setting (4) and input value, and collect its output signal.
25-
Run amplifier E's software, provide the phase setting (0) and input value, and collect its output signal.
26-
The final output signal from amplifier E would be sent to the thrusters. However, this phase setting sequence may not have been the best one; another sequence might have sent a higher signal to the thrusters.
23+
* Start the copy of the amplifier controller software that will run on amplifier A. At its first input instruction, provide it the amplifier's phase setting, 3. At its second input instruction, provide it the input signal, 0. After some calculations, it will use an output instruction to indicate the amplifier's output signal.
24+
* Start the software for amplifier B. Provide it the phase setting (1) and then whatever output signal was produced from amplifier A. It will then produce a new output signal destined for amplifier C.
25+
* Start the software for amplifier C, provide the phase setting (2) and the value from amplifier B, then collect its output signal.
26+
* Run amplifier D's software, provide the phase setting (4) and input value, and collect its output signal.
27+
* Run amplifier E's software, provide the phase setting (0) and input value, and collect its output signal.
2728

28-
Here are some example programs:
29+
The final output signal from amplifier E would be sent to the thrusters. However, this phase setting sequence may not have been the best one; another sequence might have sent a higher signal to the thrusters.
2930

30-
Max thruster signal 43210 (from phase setting sequence 4,3,2,1,0):
31+
Here are some example programs:
3132

32-
3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0
33-
Max thruster signal 54321 (from phase setting sequence 0,1,2,3,4):
33+
Max thruster signal 43210 (from phase setting sequence 4,3,2,1,0):
34+
35+
3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0
36+
37+
Max thruster signal 54321 (from phase setting sequence 0,1,2,3,4):
3438

3539
3,23,3,24,1002,24,10,24,1002,23,-1,23,
36-
101,5,23,23,1,24,23,23,4,23,99,0,0
37-
Max thruster signal 65210 (from phase setting sequence 1,0,4,3,2):
40+
101,5,23,23,1,24,23,23,4,23,99,0,0
41+
42+
Max thruster signal 65210 (from phase setting sequence 1,0,4,3,2):
3843

3944
3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,
40-
1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0
41-
Try every combination of phase settings on the amplifiers. What is the highest signal that can be sent to the thrusters?
45+
1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0
46+
47+
Try every combination of phase settings on the amplifiers. What is the highest signal that can be sent to the thrusters?

0 commit comments

Comments
 (0)