2018 Day 06 Complete
This commit is contained in:
parent
20051e984a
commit
5f920c039d
@ -1,57 +1,84 @@
|
|||||||
"We've detected some temporal anomalies," one of Santa's Elves at the Temporal Anomaly Research and Detection Instrument Station tells you. She sounded pretty worried when she called you down here. "At 500-year intervals into the past, someone has been changing Santa's history!"
|
Advent of Code
|
||||||
|
|
||||||
"The good news is that the changes won't propagate to our time stream for another 25 days, and we have a device" - she attaches something to your wrist - "that will let you fix the changes with no such propagation delay. It's configured to send you 500 years further into the past every few days; that was the best we could do on such short notice."
|
--- Day 1: Chronal Calibration ---
|
||||||
|
|
||||||
"The bad news is that we are detecting roughly fifty anomalies throughout time; the device will indicate fixed anomalies with stars. The other bad news is that we only have one device and you're the best person for the job! Good lu--" She taps a button on the device and you suddenly feel like you're falling. To save Christmas, you need to get all fifty stars by December 25th.
|
"We've detected some temporal anomalies," one of Santa's Elves at the Temporal Anomaly Research and Detection Instrument Station tells you. She sounded pretty worried when she called you down here. "At 500-year intervals into the past, someone has been changing Santa's history!"
|
||||||
|
|
||||||
Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
|
"The good news is that the changes won't propagate to our time stream for another 25 days, and we have a device" - she attaches something to your wrist - "that will let you fix the changes with no such propagation delay. It's configured to send you 500 years further into the past every few days; that was the
|
||||||
|
best we could do on such short notice."
|
||||||
|
|
||||||
After feeling like you've been falling for a few minutes, you look at the device's tiny screen. "Error: Device must be calibrated before first use. Frequency drift detected. Cannot maintain destination lock." Below the message, the device shows a sequence of changes in frequency (your puzzle input). A value like +6 means the current frequency increases by 6; a value like -3 means the current frequency decreases by 3.
|
"The bad news is that we are detecting roughly fifty anomalies throughout time; the device will indicate fixed anomalies with stars. The other bad news is that we only have one device and you're the best person for the job! Good lu--" She taps a button on the device and you suddenly feel like you're falling. To
|
||||||
|
save Christmas, you need to get all fifty stars by December 25th.
|
||||||
|
|
||||||
For example, if the device displays frequency changes of +1, -2, +3, +1, then starting from a frequency of zero, the following changes would occur:
|
Collect stars by solving puzzles. Two puzzles will be made available on each day in the advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!
|
||||||
|
|
||||||
Current frequency 0, change of +1; resulting frequency 1.
|
After feeling like you've been falling for a few minutes, you look at the device's tiny screen. "Error: Device must be calibrated before first use. Frequency drift detected. Cannot maintain destination lock." Below the message, the device shows a sequence of changes in frequency (your puzzle input). A value
|
||||||
Current frequency 1, change of -2; resulting frequency -1.
|
like +6 means the current frequency increases by 6; a value like -3 means the current frequency decreases by 3.
|
||||||
Current frequency -1, change of +3; resulting frequency 2.
|
|
||||||
Current frequency 2, change of +1; resulting frequency 3.
|
|
||||||
|
|
||||||
In this example, the resulting frequency is 3.
|
For example, if the device displays frequency changes of +1, -2, +3, +1, then starting from a frequency of zero, the following changes would occur:
|
||||||
|
|
||||||
Here are other example situations:
|
• Current frequency 0, change of +1; resulting frequency 1.
|
||||||
|
• Current frequency 1, change of -2; resulting frequency -1.
|
||||||
|
• Current frequency -1, change of +3; resulting frequency 2.
|
||||||
|
• Current frequency 2, change of +1; resulting frequency 3.
|
||||||
|
|
||||||
+1, +1, +1 results in 3
|
In this example, the resulting frequency is 3.
|
||||||
+1, +1, -2 results in 0
|
|
||||||
-1, -2, -3 results in -6
|
|
||||||
|
|
||||||
Starting with a frequency of zero, what is the resulting frequency after all of the changes in frequency have been applied?
|
Here are other example situations:
|
||||||
|
|
||||||
Your puzzle answer was 553.
|
• +1, +1, +1 results in 3
|
||||||
|
• +1, +1, -2 results in 0
|
||||||
|
• -1, -2, -3 results in -6
|
||||||
|
|
||||||
|
Starting with a frequency of zero, what is the resulting frequency after all of the changes in frequency have been applied?
|
||||||
|
|
||||||
|
Your puzzle answer was 553.
|
||||||
|
|
||||||
--- Part Two ---
|
--- Part Two ---
|
||||||
|
|
||||||
You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches twice.
|
You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find the first frequency it reaches twice.
|
||||||
|
|
||||||
For example, using the same list of changes above, the device would loop as follows:
|
For example, using the same list of changes above, the device would loop as follows:
|
||||||
|
|
||||||
Current frequency 0, change of +1; resulting frequency 1.
|
• Current frequency 0, change of +1; resulting frequency 1.
|
||||||
Current frequency 1, change of -2; resulting frequency -1.
|
• Current frequency 1, change of -2; resulting frequency -1.
|
||||||
Current frequency -1, change of +3; resulting frequency 2.
|
• Current frequency -1, change of +3; resulting frequency 2.
|
||||||
Current frequency 2, change of +1; resulting frequency 3.
|
• Current frequency 2, change of +1; resulting frequency 3.
|
||||||
(At this point, the device continues from the start of the list.)
|
• (At this point, the device continues from the start of the list.)
|
||||||
Current frequency 3, change of +1; resulting frequency 4.
|
• Current frequency 3, change of +1; resulting frequency 4.
|
||||||
Current frequency 4, change of -2; resulting frequency 2, which has already been seen.
|
• Current frequency 4, change of -2; resulting frequency 2, which has already been seen.
|
||||||
|
|
||||||
In this example, the first frequency reached twice is 2. Note that your device might need to repeat its list of frequency changes many times before a duplicate frequency is found, and that duplicates might be found while in the middle of processing the list.
|
In this example, the first frequency reached twice is 2. Note that your device might need to repeat its list of frequency changes many times before a duplicate frequency is found, and that duplicates might be found while in the middle of processing the list.
|
||||||
|
|
||||||
Here are other examples:
|
Here are other examples:
|
||||||
|
|
||||||
+1, -1 first reaches 0 twice.
|
• +1, -1 first reaches 0 twice.
|
||||||
+3, +3, +4, -2, -4 first reaches 10 twice.
|
• +3, +3, +4, -2, -4 first reaches 10 twice.
|
||||||
-6, +3, +8, +5, -6 first reaches 5 twice.
|
• -6, +3, +8, +5, -6 first reaches 5 twice.
|
||||||
+7, +7, -2, -7, -4 first reaches 14 twice.
|
• +7, +7, -2, -7, -4 first reaches 14 twice.
|
||||||
|
|
||||||
What is the first frequency your device reaches twice?
|
What is the first frequency your device reaches twice?
|
||||||
|
|
||||||
Your puzzle answer was 78724.
|
Your puzzle answer was 78724.
|
||||||
|
|
||||||
Both parts of this puzzle are complete! They provide two gold stars: **
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
. https://adventofcode.com/
|
||||||
|
. https://adventofcode.com/2018/about
|
||||||
|
. https://adventofcode.com/2018/events
|
||||||
|
. https://adventofcode.com/2018/settings
|
||||||
|
. https://adventofcode.com/2018/auth/logout
|
||||||
|
. Advent of Code Supporter
|
||||||
|
https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018/leaderboard
|
||||||
|
. https://adventofcode.com/2018/stats
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/day/1/input
|
||||||
|
74
2018/day02/problem
Normal file
74
2018/day02/problem
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
Advent of Code
|
||||||
|
|
||||||
|
--- Day 2: Inventory Management System ---
|
||||||
|
|
||||||
|
You stop falling through time, catch your breath, and check the screen on the device. "Destination reached. Current Year: 1518. Current Location: North Pole Utility Closet 83N10." You made it! Now, to find those anomalies.
|
||||||
|
|
||||||
|
Outside the utility closet, you hear footsteps and a voice. "...I'm not sure either. But now that so many people have chimneys, maybe he could sneak in that way?" Another voice responds, "Actually, we've been working on a new kind of suit that would let him fit through tight spaces like that. But, I heard that
|
||||||
|
a few days ago, they lost the prototype fabric, the design plans, everything! Nobody on the team can even seem to remember important details of the project!"
|
||||||
|
|
||||||
|
"Wouldn't they have had enough fabric to fill several boxes in the warehouse? They'd be stored together, so the box IDs should be similar. Too bad it would take forever to search the warehouse for two similar box IDs..." They walk too far away to hear any more.
|
||||||
|
|
||||||
|
Late at night, you sneak to the warehouse - who knows what kinds of paradoxes you could cause if you were discovered - and use your fancy wrist device to quickly scan every box and produce a list of the likely candidates (your puzzle input).
|
||||||
|
|
||||||
|
To make sure you didn't miss any, you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to
|
||||||
|
what your device predicts.
|
||||||
|
|
||||||
|
For example, if you see the following box IDs:
|
||||||
|
|
||||||
|
• abcdef contains no letters that appear exactly two or three times.
|
||||||
|
• bababc contains two a and three b, so it counts for both.
|
||||||
|
• abbcde contains two b, but no letter appears exactly three times.
|
||||||
|
• abcccd contains three c, but no letter appears exactly two times.
|
||||||
|
• aabcdd contains two a and two d, but it only counts once.
|
||||||
|
• abcdee contains two e.
|
||||||
|
• ababab contains three a and three b, but it only counts once.
|
||||||
|
|
||||||
|
Of these box IDs, four of them contain a letter which appears exactly twice, and three of them contain a letter which appears exactly three times. Multiplying these together produces a checksum of 4 * 3 = 12.
|
||||||
|
|
||||||
|
What is the checksum for your list of box IDs?
|
||||||
|
|
||||||
|
Your puzzle answer was 6474.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
Confident that your list of box IDs is complete, you're ready to find the boxes full of prototype fabric.
|
||||||
|
|
||||||
|
The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:
|
||||||
|
|
||||||
|
abcde
|
||||||
|
fghij
|
||||||
|
klmno
|
||||||
|
pqrst
|
||||||
|
fguij
|
||||||
|
axcye
|
||||||
|
wvxyz
|
||||||
|
|
||||||
|
The IDs abcde and axcye are close, but they differ by two characters (the second and fourth). However, the IDs fghij and fguij differ by exactly one character, the third (h and u). Those must be the correct boxes.
|
||||||
|
|
||||||
|
What letters are common between the two correct box IDs? (In the example above, this is found by removing the differing character from either ID, producing fgij.)
|
||||||
|
|
||||||
|
Your puzzle answer was mxhwoglxgeauywfkztndcvjqr.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
. https://adventofcode.com/
|
||||||
|
. https://adventofcode.com/2018/about
|
||||||
|
. https://adventofcode.com/2018/events
|
||||||
|
. https://adventofcode.com/2018/settings
|
||||||
|
. https://adventofcode.com/2018/auth/logout
|
||||||
|
. Advent of Code Supporter
|
||||||
|
https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018/leaderboard
|
||||||
|
. https://adventofcode.com/2018/stats
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://en.wikipedia.org/wiki/Checksum
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/day/2/input
|
82
2018/day03/problem
Normal file
82
2018/day03/problem
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
Advent of Code
|
||||||
|
|
||||||
|
--- Day 3: No Matter How You Slice It ---
|
||||||
|
|
||||||
|
The Elves managed to locate the chimney-squeeze prototype fabric for Santa's suit (thanks to someone who helpfully wrote its box IDs on the wall of the warehouse in the middle of the night). Unfortunately, anomalies are still affecting them - nobody can even agree on how to cut the fabric.
|
||||||
|
|
||||||
|
The whole piece of fabric they're working on is a very large square - at least 1000 inches on each side.
|
||||||
|
|
||||||
|
Each Elf has made a claim about which area of fabric would be ideal for Santa's suit. All claims have an ID and consist of a single rectangle with edges parallel to the edges of the fabric. Each claim's rectangle is defined as follows:
|
||||||
|
|
||||||
|
• The number of inches between the left edge of the fabric and the left edge of the rectangle.
|
||||||
|
• The number of inches between the top edge of the fabric and the top edge of the rectangle.
|
||||||
|
• The width of the rectangle in inches.
|
||||||
|
• The height of the rectangle in inches.
|
||||||
|
|
||||||
|
A claim like #123 @ 3,2: 5x4 means that claim ID 123 specifies a rectangle 3 inches from the left edge, 2 inches from the top edge, 5 inches wide, and 4 inches tall. Visually, it claims the square inches of fabric represented by # (and ignores the square inches of fabric represented by .) in the diagram below:
|
||||||
|
|
||||||
|
...........
|
||||||
|
...........
|
||||||
|
...#####...
|
||||||
|
...#####...
|
||||||
|
...#####...
|
||||||
|
...#####...
|
||||||
|
...........
|
||||||
|
...........
|
||||||
|
...........
|
||||||
|
|
||||||
|
The problem is that many of the claims overlap, causing two or more claims to cover part of the same areas. For example, consider the following claims:
|
||||||
|
|
||||||
|
#1 @ 1,3: 4x4
|
||||||
|
#2 @ 3,1: 4x4
|
||||||
|
#3 @ 5,5: 2x2
|
||||||
|
|
||||||
|
Visually, these claim the following areas:
|
||||||
|
|
||||||
|
........
|
||||||
|
...2222.
|
||||||
|
...2222.
|
||||||
|
.11XX22.
|
||||||
|
.11XX22.
|
||||||
|
.111133.
|
||||||
|
.111133.
|
||||||
|
........
|
||||||
|
|
||||||
|
The four square inches marked with X are claimed by both 1 and 2. (Claim 3, while adjacent to the others, does not overlap either of them.)
|
||||||
|
|
||||||
|
If the Elves all proceed with their own plans, none of them will have enough fabric. How many square inches of fabric are within two or more claims?
|
||||||
|
|
||||||
|
Your puzzle answer was 119572.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
Amidst the chaos, you notice that exactly one claim doesn't overlap by even a single square inch of fabric with any other claim. If you can somehow draw attention to it, maybe the Elves will be able to make Santa's suit after all!
|
||||||
|
|
||||||
|
For example, in the claims above, only claim 3 is intact after all claims are made.
|
||||||
|
|
||||||
|
What is the ID of the only claim that doesn't overlap?
|
||||||
|
|
||||||
|
Your puzzle answer was 775.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
. https://adventofcode.com/
|
||||||
|
. https://adventofcode.com/2018/about
|
||||||
|
. https://adventofcode.com/2018/events
|
||||||
|
. https://teespring.com/adventofcode
|
||||||
|
. https://adventofcode.com/2018/settings
|
||||||
|
. https://adventofcode.com/2018/auth/logout
|
||||||
|
. Advent of Code Supporter
|
||||||
|
https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018/leaderboard
|
||||||
|
. https://adventofcode.com/2018/stats
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/day/3/input
|
88
2018/day04/problem
Normal file
88
2018/day04/problem
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
Advent of Code
|
||||||
|
|
||||||
|
--- Day 4: Repose Record ---
|
||||||
|
|
||||||
|
You've sneaked into another supply closet - this time, it's across from the prototype suit manufacturing lab. You need to sneak inside and fix the issues with the suit, but there's a guard stationed outside the lab, so this is as close as you can safely get.
|
||||||
|
|
||||||
|
As you search the closet for anything that might help, you discover that you're not the first person to want to sneak in. Covering the walls, someone has spent an hour starting every midnight for the past few months secretly observing this guard post! They've been writing down the ID of the one guard on duty
|
||||||
|
that night - the Elves seem to have decided that one guard was enough for the overnight shift - as well as when they fall asleep or wake up while at their post (your puzzle input).
|
||||||
|
|
||||||
|
For example, consider the following records, which have already been organized into chronological order:
|
||||||
|
|
||||||
|
[1518-11-01 00:00] Guard #10 begins shift
|
||||||
|
[1518-11-01 00:05] falls asleep
|
||||||
|
[1518-11-01 00:25] wakes up
|
||||||
|
[1518-11-01 00:30] falls asleep
|
||||||
|
[1518-11-01 00:55] wakes up
|
||||||
|
[1518-11-01 23:58] Guard #99 begins shift
|
||||||
|
[1518-11-02 00:40] falls asleep
|
||||||
|
[1518-11-02 00:50] wakes up
|
||||||
|
[1518-11-03 00:05] Guard #10 begins shift
|
||||||
|
[1518-11-03 00:24] falls asleep
|
||||||
|
[1518-11-03 00:29] wakes up
|
||||||
|
[1518-11-04 00:02] Guard #99 begins shift
|
||||||
|
[1518-11-04 00:36] falls asleep
|
||||||
|
[1518-11-04 00:46] wakes up
|
||||||
|
[1518-11-05 00:03] Guard #99 begins shift
|
||||||
|
[1518-11-05 00:45] falls asleep
|
||||||
|
[1518-11-05 00:55] wakes up
|
||||||
|
|
||||||
|
Timestamps are written using year-month-day hour:minute format. The guard falling asleep or waking up is always the one whose shift most recently started. Because all asleep/awake times are during the midnight hour (00:00 - 00:59), only the minute portion (00 - 59) is relevant for those events.
|
||||||
|
|
||||||
|
Visually, these records show that the guards are asleep at these times:
|
||||||
|
|
||||||
|
Date ID Minute
|
||||||
|
000000000011111111112222222222333333333344444444445555555555
|
||||||
|
012345678901234567890123456789012345678901234567890123456789
|
||||||
|
11-01 #10 .....####################.....#########################.....
|
||||||
|
11-02 #99 ........................................##########..........
|
||||||
|
11-03 #10 ........................#####...............................
|
||||||
|
11-04 #99 ....................................##########..............
|
||||||
|
11-05 #99 .............................................##########.....
|
||||||
|
|
||||||
|
The columns are Date, which shows the month-day portion of the relevant day; ID, which shows the guard on duty that day; and Minute, which shows the minutes during which the guard was asleep within the midnight hour. (The Minute column's header shows the minute's ten's digit in the first row and the one's digit
|
||||||
|
in the second row.) Awake is shown as ., and asleep is shown as #.
|
||||||
|
|
||||||
|
Note that guards count as asleep on the minute they fall asleep, and they count as awake on the minute they wake up. For example, because Guard #10 wakes up at 00:25 on 1518-11-01, minute 25 is marked as awake.
|
||||||
|
|
||||||
|
If you can figure out the guard most likely to be asleep at a specific time, you might be able to trick that guard into working tonight so you can have the best chance of sneaking in. You have two strategies for choosing the best guard/minute combination.
|
||||||
|
|
||||||
|
Strategy 1: Find the guard that has the most minutes asleep. What minute does that guard spend asleep the most?
|
||||||
|
|
||||||
|
In the example above, Guard #10 spent the most minutes asleep, a total of 50 minutes (20+25+5), while Guard #99 only slept for a total of 30 minutes (10+10+10). Guard #10 was asleep most during minute 24 (on two days, whereas any other minute the guard was asleep was only seen on one day).
|
||||||
|
|
||||||
|
While this example listed the entries in chronological order, your entries are in the order you found them. You'll need to organize them before they can be analyzed.
|
||||||
|
|
||||||
|
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 10 * 24 = 240.)
|
||||||
|
|
||||||
|
Your puzzle answer was 102688.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
Strategy 2: Of all guards, which guard is most frequently asleep on the same minute?
|
||||||
|
|
||||||
|
In the example above, Guard #99 spent minute 45 asleep more than any other guard or minute - three times in total. (In all other cases, any guard spent any minute asleep at most twice.)
|
||||||
|
|
||||||
|
What is the ID of the guard you chose multiplied by the minute you chose? (In the above example, the answer would be 99 * 45 = 4455.)
|
||||||
|
|
||||||
|
Your puzzle answer was 56901.
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
. https://adventofcode.com/
|
||||||
|
. https://adventofcode.com/2018/about
|
||||||
|
. https://adventofcode.com/2018/events
|
||||||
|
. https://adventofcode.com/2018/settings
|
||||||
|
. https://adventofcode.com/2018/auth/logout
|
||||||
|
. Advent of Code Supporter
|
||||||
|
https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018/leaderboard
|
||||||
|
. https://adventofcode.com/2018/stats
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/day/4/input
|
72
2018/day05/problem
Normal file
72
2018/day05/problem
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
Advent of Code
|
||||||
|
|
||||||
|
--- Day 5: Alchemical Reduction ---
|
||||||
|
|
||||||
|
You've managed to sneak in to the prototype suit manufacturing lab. The Elves are making decent progress, but are still struggling with the suit's size reduction capabilities.
|
||||||
|
|
||||||
|
While the very latest in 1518 alchemical technology might have solved their problem eventually, you can do better. You scan the chemical composition of the suit's material and discover that it is formed by extremely long polymers (one of which is available as your puzzle input).
|
||||||
|
|
||||||
|
The polymer is formed by smaller units which, when triggered, react with each other such that two adjacent units of the same type and opposite polarity are destroyed. Units' types are represented by letters; units' polarity is represented by capitalization. For instance, r and R are units with the same type but
|
||||||
|
opposite polarity, whereas r and s are entirely different types and do not react.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
|
• In aA, a and A react, leaving nothing behind.
|
||||||
|
• In abBA, bB destroys itself, leaving aA. As above, this then destroys itself, leaving nothing.
|
||||||
|
• In abAB, no two adjacent units are of the same type, and so nothing happens.
|
||||||
|
• In aabAAB, even though aa and AA are of the same type, their polarities match, and so nothing happens.
|
||||||
|
|
||||||
|
Now, consider a larger example, dabAcCaCBAcCcaDA:
|
||||||
|
|
||||||
|
dabAcCaCBAcCcaDA The first 'cC' is removed.
|
||||||
|
dabAaCBAcCcaDA This creates 'Aa', which is removed.
|
||||||
|
dabCBAcCcaDA Either 'cC' or 'Cc' are removed (the result is the same).
|
||||||
|
dabCBAcaDA No further actions can be taken.
|
||||||
|
|
||||||
|
After all possible reactions, the resulting polymer contains 10 units.
|
||||||
|
|
||||||
|
How many units remain after fully reacting the polymer you scanned? (Note: in this puzzle and others, the input is large; if you copy/paste your input, make sure you get the whole thing.)
|
||||||
|
|
||||||
|
Your puzzle answer was 10878.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
Time to improve the polymer.
|
||||||
|
|
||||||
|
One of the unit types is causing problems; it's preventing the polymer from collapsing as much as it should. Your goal is to figure out which unit type is causing the most problems, remove all instances of it (regardless of polarity), fully react the remaining polymer, and measure its length.
|
||||||
|
|
||||||
|
For example, again using the polymer dabAcCaCBAcCcaDA from above:
|
||||||
|
|
||||||
|
• Removing all A/a units produces dbcCCBcCcD. Fully reacting this polymer produces dbCBcD, which has length 6.
|
||||||
|
• Removing all B/b units produces daAcCaCAcCcaDA. Fully reacting this polymer produces daCAcaDA, which has length 8.
|
||||||
|
• Removing all C/c units produces dabAaBAaDA. Fully reacting this polymer produces daDA, which has length 4.
|
||||||
|
• Removing all D/d units produces abAcCaCBAcCcaA. Fully reacting this polymer produces abCBAc, which has length 6.
|
||||||
|
|
||||||
|
In this example, removing all C/c units was best, producing the answer 4.
|
||||||
|
|
||||||
|
What is the length of the shortest polymer you can produce by removing all units of exactly one type and fully reacting the result?
|
||||||
|
|
||||||
|
Your puzzle answer was 6874.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
. https://adventofcode.com/
|
||||||
|
. https://adventofcode.com/2018/about
|
||||||
|
. https://adventofcode.com/2018/events
|
||||||
|
. https://adventofcode.com/2018/settings
|
||||||
|
. https://adventofcode.com/2018/auth/logout
|
||||||
|
. Advent of Code Supporter
|
||||||
|
https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018/leaderboard
|
||||||
|
. https://adventofcode.com/2018/stats
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://en.wikipedia.org/wiki/Polymer
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/day/5/input
|
185
2018/day06/day06.go
Normal file
185
2018/day06/day06.go
Normal file
@ -0,0 +1,185 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"math"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const MaxInt = int(^uint(0) >> 1)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
inp := StdinToStringSlice()
|
||||||
|
start(inp)
|
||||||
|
part1()
|
||||||
|
part2()
|
||||||
|
}
|
||||||
|
|
||||||
|
var points []RegionPos
|
||||||
|
var boundaries []RegionPos
|
||||||
|
var allPoints []RegionPos
|
||||||
|
var maxX, maxY, minX, minY int
|
||||||
|
|
||||||
|
func start(inp []string) {
|
||||||
|
maxX, maxY = 0, 0
|
||||||
|
minX, minY = MaxInt, MaxInt
|
||||||
|
|
||||||
|
for _, v := range inp {
|
||||||
|
pts := strings.Split(v, ", ")
|
||||||
|
x, y := Atoi(pts[0]), Atoi(pts[1])
|
||||||
|
p := RegionPos{x: x, y: y}
|
||||||
|
points = append(points, p)
|
||||||
|
if x < minX {
|
||||||
|
minX = x
|
||||||
|
}
|
||||||
|
if x > maxX {
|
||||||
|
maxX = x
|
||||||
|
}
|
||||||
|
if y < minY {
|
||||||
|
minY = y
|
||||||
|
}
|
||||||
|
if y > maxY {
|
||||||
|
maxY = y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Find the points that are the closest to the boundaries
|
||||||
|
for x := minX; x <= maxX; x++ {
|
||||||
|
boundaries = append(boundaries, *NewRegionPos(x, minY))
|
||||||
|
boundaries = append(boundaries, *NewRegionPos(x, maxY))
|
||||||
|
}
|
||||||
|
for y := minY; y <= maxY; y++ {
|
||||||
|
boundaries = append(boundaries, *NewRegionPos(minX, y))
|
||||||
|
boundaries = append(boundaries, *NewRegionPos(maxX, y))
|
||||||
|
}
|
||||||
|
for i := range boundaries {
|
||||||
|
closest := boundaries[i].findClosest(points)
|
||||||
|
if len(closest) == 1 {
|
||||||
|
for j := range points {
|
||||||
|
if points[j].x == closest[0].x && points[j].y == closest[0].y {
|
||||||
|
points[j].ignored = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
boundaries[i].ignored = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func part1() {
|
||||||
|
for x := minX; x <= maxX; x++ {
|
||||||
|
for y := minY; y <= maxY; y++ {
|
||||||
|
p := NewRegionPos(x, y)
|
||||||
|
closest := p.findClosest(points)
|
||||||
|
if len(closest) == 1 {
|
||||||
|
c := closest[0]
|
||||||
|
for i := range points {
|
||||||
|
if points[i].ignored {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if points[i].x == c.x && points[i].y == c.y {
|
||||||
|
points[i].regionSize++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var largestSize int
|
||||||
|
for _, v := range points {
|
||||||
|
if !v.ignored {
|
||||||
|
if v.regionSize > largestSize {
|
||||||
|
largestSize = v.regionSize
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("= Part 1 =")
|
||||||
|
fmt.Println(largestSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
func part2() {
|
||||||
|
minDistance := 10000
|
||||||
|
|
||||||
|
var regionCount int
|
||||||
|
for y := minY - minDistance; y <= maxY+minDistance; y++ {
|
||||||
|
for x := minX - minDistance; x <= maxX+minDistance; x++ {
|
||||||
|
p := NewRegionPos(x, y)
|
||||||
|
if p.sumOfDistances(minDistance, points) < minDistance {
|
||||||
|
regionCount++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("= Part 2 =")
|
||||||
|
fmt.Println(regionCount)
|
||||||
|
}
|
||||||
|
|
||||||
|
func manhattanDistance(x1, y1, x2, y2 int) int {
|
||||||
|
return int(math.Abs(float64(x1)-float64(x2)) + math.Abs(float64(y1)-float64(y2)))
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegionPos struct {
|
||||||
|
x, y int
|
||||||
|
regionSize int
|
||||||
|
closestPos *RegionPos
|
||||||
|
ignored bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewRegionPos(x, y int) *RegionPos {
|
||||||
|
b := &RegionPos{}
|
||||||
|
b.x = x
|
||||||
|
b.y = y
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *RegionPos) distanceToPos(o *RegionPos) int {
|
||||||
|
return manhattanDistance(p.x, p.y, o.x, o.y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *RegionPos) sumOfDistances(d int, list []RegionPos) int {
|
||||||
|
var ret int
|
||||||
|
for _, p := range list {
|
||||||
|
ret += b.distanceToPos(&p)
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *RegionPos) findClosest(list []RegionPos) []RegionPos {
|
||||||
|
var ret []RegionPos
|
||||||
|
minD := MaxInt
|
||||||
|
for _, p := range list {
|
||||||
|
test := b.distanceToPos(&p)
|
||||||
|
if test < minD {
|
||||||
|
minD = test
|
||||||
|
ret = ret[:0]
|
||||||
|
ret = append(ret, p)
|
||||||
|
} else if test == minD {
|
||||||
|
ret = append(ret, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *RegionPos) string() string {
|
||||||
|
return fmt.Sprintf("%d;%d", b.x, b.y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func StdinToStringSlice() []string {
|
||||||
|
var input []string
|
||||||
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
|
for scanner.Scan() {
|
||||||
|
input = append(input, scanner.Text())
|
||||||
|
}
|
||||||
|
return input
|
||||||
|
}
|
||||||
|
|
||||||
|
func Atoi(i string) int {
|
||||||
|
var ret int
|
||||||
|
var err error
|
||||||
|
if ret, err = strconv.Atoi(i); err != nil {
|
||||||
|
log.Fatal("Invalid Atoi")
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
}
|
1
2018/day06/go.mod
Normal file
1
2018/day06/go.mod
Normal file
@ -0,0 +1 @@
|
|||||||
|
module day06
|
50
2018/day06/input
Normal file
50
2018/day06/input
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
224, 153
|
||||||
|
176, 350
|
||||||
|
353, 241
|
||||||
|
207, 59
|
||||||
|
145, 203
|
||||||
|
123, 210
|
||||||
|
113, 203
|
||||||
|
191, 241
|
||||||
|
172, 196
|
||||||
|
209, 249
|
||||||
|
260, 229
|
||||||
|
98, 231
|
||||||
|
305, 215
|
||||||
|
258, 141
|
||||||
|
337, 282
|
||||||
|
156, 140
|
||||||
|
325, 197
|
||||||
|
179, 279
|
||||||
|
283, 233
|
||||||
|
317, 150
|
||||||
|
305, 245
|
||||||
|
67, 109
|
||||||
|
251, 140
|
||||||
|
245, 59
|
||||||
|
173, 105
|
||||||
|
59, 173
|
||||||
|
257, 70
|
||||||
|
269, 110
|
||||||
|
102, 162
|
||||||
|
179, 180
|
||||||
|
324, 112
|
||||||
|
357, 311
|
||||||
|
317, 245
|
||||||
|
239, 112
|
||||||
|
321, 220
|
||||||
|
133, 97
|
||||||
|
334, 99
|
||||||
|
117, 102
|
||||||
|
133, 112
|
||||||
|
222, 316
|
||||||
|
68, 296
|
||||||
|
150, 287
|
||||||
|
263, 263
|
||||||
|
66, 347
|
||||||
|
128, 118
|
||||||
|
63, 202
|
||||||
|
68, 236
|
||||||
|
264, 122
|
||||||
|
77, 243
|
||||||
|
92, 110
|
122
2018/day06/problem
Normal file
122
2018/day06/problem
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
Advent of Code
|
||||||
|
|
||||||
|
--- Day 6: Chronal Coordinates ---
|
||||||
|
|
||||||
|
The device on your wrist beeps several times, and once again you feel like you're falling.
|
||||||
|
|
||||||
|
"Situation critical," the device announces. "Destination indeterminate. Chronal interference detected. Please specify new target coordinates."
|
||||||
|
|
||||||
|
The device then produces a list of coordinates (your puzzle input). Are they places it thinks are safe or dangerous? It recommends you check manual page 729. The Elves did not give you a manual.
|
||||||
|
|
||||||
|
If they're dangerous, maybe you can minimize the danger by finding the coordinate that gives the largest distance from the other points.
|
||||||
|
|
||||||
|
Using only the Manhattan distance, determine the area around each coordinate by counting the number of integer X,Y locations that are closest to that coordinate (and aren't tied in distance to any other coordinate).
|
||||||
|
|
||||||
|
Your goal is to find the size of the largest area that isn't infinite. For example, consider the following list of coordinates:
|
||||||
|
|
||||||
|
1, 1
|
||||||
|
1, 6
|
||||||
|
8, 3
|
||||||
|
3, 4
|
||||||
|
5, 5
|
||||||
|
8, 9
|
||||||
|
|
||||||
|
If we name these coordinates A through F, we can draw them on a grid, putting 0,0 at the top left:
|
||||||
|
|
||||||
|
..........
|
||||||
|
.A........
|
||||||
|
..........
|
||||||
|
........C.
|
||||||
|
...D......
|
||||||
|
.....E....
|
||||||
|
.B........
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
........F.
|
||||||
|
|
||||||
|
This view is partial - the actual grid extends infinitely in all directions. Using the Manhattan distance, each location's closest coordinate can be determined, shown here in lowercase:
|
||||||
|
|
||||||
|
aaaaa.cccc
|
||||||
|
aAaaa.cccc
|
||||||
|
aaaddecccc
|
||||||
|
aadddeccCc
|
||||||
|
..dDdeeccc
|
||||||
|
bb.deEeecc
|
||||||
|
bBb.eeee..
|
||||||
|
bbb.eeefff
|
||||||
|
bbb.eeffff
|
||||||
|
bbb.ffffFf
|
||||||
|
|
||||||
|
Locations shown as . are equally far from two or more coordinates, and so they don't count as being closest to any.
|
||||||
|
|
||||||
|
In this example, the areas of coordinates A, B, C, and F are infinite - while not shown here, their areas extend forever outside the visible grid. However, the areas of coordinates D and E are finite: D is closest to 9 locations, and E is closest to 17 (both including the coordinate's location itself).
|
||||||
|
Therefore, in this example, the size of the largest area is 17.
|
||||||
|
|
||||||
|
What is the size of the largest area that isn't infinite?
|
||||||
|
|
||||||
|
Your puzzle answer was 3989.
|
||||||
|
|
||||||
|
--- Part Two ---
|
||||||
|
|
||||||
|
On the other hand, if the coordinates are safe, maybe the best you can do is try to find a region near as many coordinates as possible.
|
||||||
|
|
||||||
|
For example, suppose you want the sum of the Manhattan distance to all of the coordinates to be less than 32. For each location, add up the distances to all of the given coordinates; if the total of those distances is less than 32, that location is within the desired region. Using the same coordinates as above,
|
||||||
|
the resulting region looks like this:
|
||||||
|
|
||||||
|
..........
|
||||||
|
.A........
|
||||||
|
..........
|
||||||
|
...###..C.
|
||||||
|
..#D###...
|
||||||
|
..###E#...
|
||||||
|
.B.###....
|
||||||
|
..........
|
||||||
|
..........
|
||||||
|
........F.
|
||||||
|
|
||||||
|
In particular, consider the highlighted location 4,3 located at the top middle of the region. Its calculation is as follows, where abs() is the absolute value function:
|
||||||
|
|
||||||
|
• Distance to coordinate A: abs(4-1) + abs(3-1) = 5
|
||||||
|
• Distance to coordinate B: abs(4-1) + abs(3-6) = 6
|
||||||
|
• Distance to coordinate C: abs(4-8) + abs(3-3) = 4
|
||||||
|
• Distance to coordinate D: abs(4-3) + abs(3-4) = 2
|
||||||
|
• Distance to coordinate E: abs(4-5) + abs(3-5) = 3
|
||||||
|
• Distance to coordinate F: abs(4-8) + abs(3-9) = 10
|
||||||
|
• Total distance: 5 + 6 + 4 + 2 + 3 + 10 = 30
|
||||||
|
|
||||||
|
Because the total distance to all coordinates (30) is less than 32, the location is within the region.
|
||||||
|
|
||||||
|
This region, which also includes coordinates D and E, has a total size of 16.
|
||||||
|
|
||||||
|
Your actual region will need to be much larger than this example, though, instead including all locations with a total distance of less than 10000.
|
||||||
|
|
||||||
|
What is the size of the region containing all locations which have a total distance to all given coordinates of less than 10000?
|
||||||
|
|
||||||
|
Your puzzle answer was 49715.
|
||||||
|
|
||||||
|
Both parts of this puzzle are complete! They provide two gold stars: **
|
||||||
|
|
||||||
|
References
|
||||||
|
|
||||||
|
Visible links
|
||||||
|
. https://adventofcode.com/
|
||||||
|
. https://adventofcode.com/2018/about
|
||||||
|
. https://adventofcode.com/2018/events
|
||||||
|
. https://teespring.com/adventofcode
|
||||||
|
. https://adventofcode.com/2018/settings
|
||||||
|
. https://adventofcode.com/2018/auth/logout
|
||||||
|
. Advent of Code Supporter
|
||||||
|
https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/support
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://adventofcode.com/2018/leaderboard
|
||||||
|
. https://adventofcode.com/2018/stats
|
||||||
|
. https://adventofcode.com/2018/sponsors
|
||||||
|
. https://en.wikipedia.org/wiki/Taxicab_geometry
|
||||||
|
. https://en.wikipedia.org/wiki/Integer
|
||||||
|
. https://en.wikipedia.org/wiki/Taxicab_geometry
|
||||||
|
. https://en.wikipedia.org/wiki/Absolute_value
|
||||||
|
. https://adventofcode.com/2018
|
||||||
|
. https://adventofcode.com/2018/day/6/input
|
6
2018/day06/testinput
Normal file
6
2018/day06/testinput
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
1, 1
|
||||||
|
1, 6
|
||||||
|
8, 3
|
||||||
|
3, 4
|
||||||
|
5, 5
|
||||||
|
8, 9
|
Loading…
Reference in New Issue
Block a user