bit behind, so much work

This commit is contained in:
Benjamin Kyd
2020-12-06 00:31:14 +00:00
parent 7e353fc760
commit 77ecdee0f8
66 changed files with 12636 additions and 11133 deletions

10
.gitignore vendored
View File

@@ -1,5 +1,5 @@
.vscode/
.vs/
.exe/
.o/
*.o
.vscode/
.vs/
.exe/
.o/
*.o

114
.vscode/settings.json vendored
View File

@@ -1,57 +1,59 @@
{
"files.associations": {
"iosfwd": "cpp",
"vector": "cpp",
"cctype": "cpp",
"cmath": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xmemory0": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"algorithm": "cpp",
"concepts": "cpp",
"ctime": "cpp",
"iomanip": "cpp",
"iterator": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"xhash": "cpp",
"xlocmon": "cpp",
"xloctime": "cpp",
"xtree": "cpp"
}
{
"files.associations": {
"iosfwd": "cpp",
"vector": "cpp",
"cctype": "cpp",
"cmath": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xmemory0": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp",
"algorithm": "cpp",
"concepts": "cpp",
"ctime": "cpp",
"iomanip": "cpp",
"iterator": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"xhash": "cpp",
"xlocmon": "cpp",
"xloctime": "cpp",
"xtree": "cpp",
"atomic": "cpp",
"compare": "cpp"
}
}

View File

@@ -1,42 +1,42 @@
{
"files.associations": {
"cmath": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xmemory0": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp"
}
{
"files.associations": {
"cmath": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"cwchar": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"memory": "cpp",
"new": "cpp",
"ostream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp",
"vector": "cpp",
"xfacet": "cpp",
"xiosbase": "cpp",
"xlocale": "cpp",
"xlocinfo": "cpp",
"xlocnum": "cpp",
"xmemory": "cpp",
"xmemory0": "cpp",
"xstddef": "cpp",
"xstring": "cpp",
"xtr1common": "cpp",
"xutility": "cpp"
}
}

View File

@@ -1,25 +1,25 @@
--- Day 1: Chronal Calibration ---
"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!"
"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."
"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.
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!
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.
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:
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.
In this example, the resulting frequency is 3.
Here are other example situations:
+1, +1, +1 results in 3
+1, +1, -2 results in 0
-1, -2, -3 results in -6
--- Day 1: Chronal Calibration ---
"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!"
"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."
"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.
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!
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.
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:
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.
In this example, the resulting frequency is 3.
Here are other example situations:
+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?

View File

@@ -1,3 +1,3 @@
const fs = require('fs');
let input = fs.readFileSync('input.txt').toString().split('\n').join('');
console.log(eval(input));
const fs = require('fs');
let input = fs.readFileSync('input.txt').toString().split('\n').join('');
console.log(eval(input));

File diff suppressed because it is too large Load Diff

View File

@@ -1,21 +1,21 @@
--- 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.
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 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.
(At this point, the device continues from the start of the list.)
Current frequency 3, change of +1; resulting frequency 4.
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.
Here are other examples:
+1, -1 first reaches 0 twice.
+3, +3, +4, -2, -4 first reaches 10 twice.
-6, +3, +8, +5, -6 first reaches 5 twice.
+7, +7, -2, -7, -4 first reaches 14 twice.
--- 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.
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 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.
(At this point, the device continues from the start of the list.)
Current frequency 3, change of +1; resulting frequency 4.
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.
Here are other examples:
+1, -1 first reaches 0 twice.
+3, +3, +4, -2, -4 first reaches 10 twice.
-6, +3, +8, +5, -6 first reaches 5 twice.
+7, +7, -2, -7, -4 first reaches 14 twice.
What is the first frequency your device reaches twice?

View File

@@ -1,24 +1,24 @@
const fs = require('fs');
let input = fs.readFileSync('input.txt')
.toString()
.split('\n')
.map((x) => parseInt(x));
let seenFrequencies = new Set([0]);
let total = 0;
let i = 0;
while (true) {
if (i === input.length) {
i = 0;
continue;
}
total += input[i];
if (seenFrequencies.has(total)) {
break;
}
seenFrequencies.add(total);
i++;
}
console.log(total)
const fs = require('fs');
let input = fs.readFileSync('input.txt')
.toString()
.split('\n')
.map((x) => parseInt(x));
let seenFrequencies = new Set([0]);
let total = 0;
let i = 0;
while (true) {
if (i === input.length) {
i = 0;
continue;
}
total += input[i];
if (seenFrequencies.has(total)) {
break;
}
seenFrequencies.add(total);
i++;
}
console.log(total)

File diff suppressed because it is too large Load Diff

View File

@@ -1,23 +1,23 @@
--- 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.
--- 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?

View File

@@ -1,250 +1,250 @@
ybruvapdgixszyckwtfqjonsie
mbruvapxghslyyckwtfqjonsie
mbruvapdghslzyckwtkujonsie
rwruvapdghxlzyckwtfqjcnsie
obruvapdgtxlzyckwtfqionsie
lbruvapdghxqzyckwtfqjfnsie
mbrunapdghxlzyccatfqjonsie
mbruvapdghxlzyokltfqjdnsie
ybruvapdghxlzmckwtfqjmnsie
mbruwaadghxdzyckwtfqjonsie
muruvapdghxlzyckvtfqjonsim
mbruvapdghxlkyckwtxqjonjie
mbruvaqdghxlzyckwtfqjrnnie
mwruvapdghdlzyckttfqjonsie
mbruvapdgtelzyckwxfqjonsie
mbruvapdohxlzvckwtfqjonhie
mbrugapdgbxlzyckwtfqjynsie
mbruvapdghxlzyckwtlqjonjiu
mbruvapwghxlzyckwafqjonbie
wbruvapdghxlhyckwtfqjonsii
mbruvapdghxlzyckwtcqnonsiq
mbyuvapighxlzybkwtfqjonsie
mbrrvapdghxvzyckwtfqjonsio
mhruvapdghrlzyckwtfzjonsie
mtruvapvghxlzyckwtfnjonsie
mmrlhapdghxlzyckwtfqjonsie
mbruvapdgpxlzyjkwtfqjovsie
mbrucapdghxlzymkwtzqjonsie
mbeuvafdghxlzyckwtfqjonwie
mbruvapcghxlayckwtfqjonsii
mbruvabdghxlzyckwtfqyansie
mbruvjpdghxlzyckwtfqgfnsie
lbruvapdghxlzyckwtfqjonriv
mbrupapdghxlzycjwtfqronsie
mbpuvapdthxlzymkwtfqjonsie
mbiuvapdgixlzyckwxfqjonsie
mbruvapdghxyzyckwtfcjonsbe
mbrurapkghxlzyckwtfqjonzie
mbrufapdrhxlzyciwtfqjonsie
mbruvapdghxlzbckwtfqjoisae
ubruhapdghxlzuckwtfqjonsie
mbruvapdjhulzyckwtfqjonshe
mbruwapdgyxlzyckntfqjonsie
mwruvapdghplzyckwtfqjonsme
mbruvapjghtlzyckwtfqgonsie
pbruvapdghhlzyckwtfrjonsie
mbruvgpdihxqzyckwtfqjonsie
mbruvahdohxlzyckwtfijonsie
ibuuvapdghxlzyckwtfqjofsie
mbruvandghxlzyckwtfqjrnxie
mbrjvlpdghxlzyckwgfqjonsie
mbruvapogfxlzyckotfqjonsie
mbruvrpdghxlzyckutfejonsie
mbruvbpdghxlzyhkwtfqjonsip
mbruvapdghxlzyckmnfqjensie
mbruvapdghvlzyckwtfqjowsix
mbruvakdgholzwckwtfqjonsie
mbruvapdghxlzackwtfqconsae
mbruvapdghxlzyqvwtfqjlnsie
mprrvapdgfxlzyckwtfqjonsie
mbrunacdghxlhyckwtfqjonsie
obruvapdgsxlzyckwtfqjonvie
murcvapdghslzyckwtfqjonsie
mbruvapdghxlzyzkwmftjonsie
mbrwvapdgtvlzyckwtfqjonsie
mbxuvapdghxlzqcnwtfqjonsie
mbruvaddghxboyckwtfqjonsie
mhruvwndghxlzyckwtfqjonsie
mbrdvapdghxlzyckwmpqjonsie
mbruvapdgyxlzyckizfqjonsie
mbruvapdghxlzlckwtfqeowsie
mbruvbpdgrxlzyckwtfqjonsxe
mbruqapoghxlzyckwtvqjonsie
mbouhapdghmlzyckwtfqjonsie
mbruvapjghxidyckwtfqjonsie
mbsuvapkghxlkyckwtfqjonsie
mbruvlpdghxlzycrwtfqjonsis
mcrueapdghxlzyckwtfqjynsie
muruvapngbxlzyckwtfqjonsie
mbruvapdghxlzycawtfyjojsie
mbruvbpdghxczyjkwtfqjonsie
ybduvapdghxnzyckwtfqjonsie
mbruvbpdghxlzyckwtfbjousie
mbouvapdghxlzycbwtfqponsie
mbruvaedghplzycgwtfqjonsie
mbrhvapdghxlzyckytfqjgnsie
mbruvapdqbxleyckwtfqjonsie
mbruvapddhhldyckwtfqjonsie
mbruvapdghxlwrckwtfqjondie
mbruvapdmhxlzyckwtfqkonsve
xbbuvapdghxlzyckwtfkjonsie
mbruvapdghxlzyckwcfqjunkie
mbruvapdghxlzyckwtfqxonfib
mbrtvapkghxlzyckwtfqeonsie
mbruvazdghxldymkwtfqjonsie
kbruvapddhxlzfckwtfqjonsie
mbouvapdghxlpyckwtfqjoosie
mbauvapdghxlzyckwtfqjszsie
mbruvapdghtlzyckntfqtonsie
mbruvipdggxlzbckwtfqjonsie
mbruqapdghrlzyckwtfqjznsie
myruvacdghxlzyckwifqjonsie
mbruvapdghxlzuckwtfkjocsie
mwjuvapdghxlzyckwtfqjonsxe
mbruvapxghxlzickwtfqjobsie
mbrupapdghxtlyckwtfqjonsie
meruvapdjjxlzyckwtfqjonsie
mbruvkodghxlzyckwofqjonsie
mbruvapdgexlzyckwtgkjonsie
mbruvapwghxlzyckwtcqjonsiw
mbruvapdghxlzykkwtfqtoxsie
mbruvapdahxlzycgwtfwjonsie
mbruvapdgwxlhyckhtfqjonsie
mbruvapbghxlzycbhmfqjonsie
mbruvapdghxvzyzkwtfqjonsin
mbrcvapdqhxlzyckwyfqjonsie
zbruvaxdghxlzyckwgfqjonsie
mtruvapdghxlilckwtfqjonsie
bbruvapdghxlzyckwtfmjonsxe
mbruvajdghxlzyckwtfqfwnsie
mbruvapdgkxlzyckwtfqionpie
rbruvapdghxlryckwdfqjonsie
mbruvandghxlzyckwmfvjonsie
mbruvahdghxlzeckwtfqjonsme
mbruvnpcghxlzyckwtfqjobsie
mbruvapdghqlzyckwtfbjonsiy
mbruvavdghxlzyckwufqjodsie
mbruvapdghxlzyckwtfzmovsie
mbruvlpdghxuzyckwtfqjoesie
mbruvopdghxlzycwwtfqjansie
obruvapdghglzybkwtfqjonsie
mbpuvlpdghxlcyckwtfqjonsie
mbruvaidghxlzyckwtfmjonoie
mbruvapdihxzzyckwtfqjonsiy
mbquvapdghxlzyckwtfqconsme
mbruvapdghslzyckqtfqjojsie
mbrzdapdghxmzyckwtfqjonsie
mwruvapdghxozyckwtfqjonsxe
muruvapdgfxlzyckwtfqjojsie
wtruvapdghxlzyckvtfqjonsie
mbruvapdghxlzyckysfqjxnsie
mbruvrpdghxczyckktfqjonsie
mbquvapdghxlryckwtfqjonsne
mbruvapdghflzycvwtfqjpnsie
mbruvapughclzyckwtfqjonsin
mbrhvapdghxlpyckwtfqjonsre
mbruvapdgtxlzyckwtfqjoosit
mbrupapnghxhzyckwtfqjonsie
mmvuvapdvhxlzyckwtfqjonsie
mbruvaptghxlzyckwtfqjotsse
mgruvapvghxlzyckwtfqjonsix
mbrupapdghxszyckwtfqjunsie
mbruvkpdghelzyckwtfqjpnsie
mbruvrrdghjlzyckwtfqjonsie
mbruvapdghnlzyckwtfkjonsze
mbruvwpdghxlzyckwtfqhoysie
mbrsvapdfhxlzyckwtfqjobsie
mbruvapdgexezymkwtfqjonsie
ybruvapdghxlzyckwtfqxonsiw
mrruvapdghxdzyckwtfqjossie
mbruvapdghtlzyckwtfqconsiu
mbrpvapdghxlzlckwpfqjonsie
mbruvjpdghslzyckwtfqjjnsie
mhruvapoghxlzyckwtfvjonsie
mbrubqpdghvlzyckwtfqjonsie
mbruvapdghxlzackwtfqconsiw
mbruvapdgnxlzkckwtfqjdnsie
mbrudapgghelzyckwtfqjonsie
mbruvapdghxlzlakwbfqjonsie
mbpuvapdghxlzyckwtuqjonjie
abruvapdghxlzykkwtfqjonzie
mbrupupdghxlsyckwtfqjonsie
mbrsvupdghxlzyckwtfqjonkie
mxruvgpdghxllyckwtfqjonsie
mbrnvapdghxlzycbwtfqfonsie
mbrbxapdghxlzyckttfqjonsie
mbnuvapdghxlzyxkwtmqjonsie
mbrfvapdghjlzickwtfqjonsie
mbhuvupdghxlzyxkwtfqjonsie
mbrcvapdghxluyckwtfqjznsie
mbruvapdghxlzyckwofqjoxsiz
mbrevapdghxloyckwtfqjonnie
mbruvipdghnlzyckwtfqjopsie
mbxxvaptghxlzyckwtfqjonsie
mbruvcpdghxlztckwtjqjonsie
mqruvlpdghxlzyckotfqjonsie
mbruvapdgqxlzyckwtfqjpvsie
mbruvapdgvxlzyjkwtfqjbnsie
mbruvapdghxlgyckwtfqcocsie
mbruvapdghxkwyckwtfqjoqsie
mbrgvavdghxlzyckwxfqjonsie
qbruqapdgvxlzyckwtfqjonsie
mbauvapdghxlzgckwtfqjunsie
mbruvapdgdxluyckwtfqjoosie
mbruvapdghxlzykkwtfqwobsie
mbruvapdghxlzhcnwtfqjonqie
mbruvapdghxlzycbhmfqjonsie
mbruvapdghxluyczwtfqjontie
mbruvapnghxlzyckwnfqjonbie
moruvapdghxlzcckwtfqponsie
mbruvapfgxxlzyckwtfqjunsie
mbruvapdghxlryckvtfejonsie
mbrzvapdghxlzvcbwtfqjonsie
mbruvapdgqxlzyckwcfqjonsce
abruvupdrhxlzyckwtfqjonsie
mbrubaptghxlzyckwtfqjondie
mgruvapdgpxlzyckwtfijonsie
mbruvapdghxczlckwtfujonsie
mbruvapdgmmlzyckwtfqjonsir
mbruvapdhhxltyckwtfdjonsie
mbruvapdghxlzyckwtfdjjnste
mbrdvzpdghxlcyckwtfqjonsie
mbruvapdghxlzyckwtnqbonsim
mbrovapdghxlzyckwtfpjousie
mymuvapdghxlzyjkwtfqjonsie
mbpuvapdghxlzyckwtfljcnsie
mbrxvapdghxlzyclwtfqjonpie
mbrueapdghxlzyckwtfqjopsia
mbruvapdghxlzycdwtfqjbfsie
tbruvavdghxlzyckwtmqjonsie
mbduvapdghxlzyckwrfqjrnsie
mkrsvapughxlzyckwtfqjonsie
mbruvapdghylzyckwtfqtolsie
mgruvapdglxldyckwtfqjonsie
mbrunapdghclzyckwtfqjonsiy
mbruvapdgrxlxyckwtfgjonsie
mbruvapdghxpzbckftfqjonsie
mbruvcpdghxyzyckotfqjonsie
mbruvapdghxlsyckwtfqcqnsie
mbruvapdghxlzzckwtfqjonskf
mbruvppdghxlzfckwtfqjgnsie
mbhuvapdghxlzytkwtfqjonoie
mbruvapdghxlzvrkwtfqjjnsie
mbmuvapdghxuzyckwtfqjonsze
mbruvapdghnlzycnwtfqjonsil
mbruvapdgholzyckitfqjonsia
mbruxapdghxlmyckwtfqbonsie
mbauvapdgholzyckwtfqjolsie
mbruvapdghxlzyckwtfqjotslq
dbrutapdghxlzyckwtfqjonsiv
mbruvapdzhxlyyckwtfbjonsie
mmruaapsghxlzyckwtfqjonsie
mbruvaldgqxqzyckwtfqjonsie
mbruvaodghxdzyjkwtfqjonsie
mbrcmatdghxlzyckwtfqjonsie
mbrqvapdgtxlzycewtfqjonsie
mjruvapdghzlzyckwtfqjonrie
mbruvapdghxopcckwtfqjonsie
mbruvapdghxszycwwtfqjoqsie
ybruvapdgixszyckwtfqjonsie
mbruvapxghslyyckwtfqjonsie
mbruvapdghslzyckwtkujonsie
rwruvapdghxlzyckwtfqjcnsie
obruvapdgtxlzyckwtfqionsie
lbruvapdghxqzyckwtfqjfnsie
mbrunapdghxlzyccatfqjonsie
mbruvapdghxlzyokltfqjdnsie
ybruvapdghxlzmckwtfqjmnsie
mbruwaadghxdzyckwtfqjonsie
muruvapdghxlzyckvtfqjonsim
mbruvapdghxlkyckwtxqjonjie
mbruvaqdghxlzyckwtfqjrnnie
mwruvapdghdlzyckttfqjonsie
mbruvapdgtelzyckwxfqjonsie
mbruvapdohxlzvckwtfqjonhie
mbrugapdgbxlzyckwtfqjynsie
mbruvapdghxlzyckwtlqjonjiu
mbruvapwghxlzyckwafqjonbie
wbruvapdghxlhyckwtfqjonsii
mbruvapdghxlzyckwtcqnonsiq
mbyuvapighxlzybkwtfqjonsie
mbrrvapdghxvzyckwtfqjonsio
mhruvapdghrlzyckwtfzjonsie
mtruvapvghxlzyckwtfnjonsie
mmrlhapdghxlzyckwtfqjonsie
mbruvapdgpxlzyjkwtfqjovsie
mbrucapdghxlzymkwtzqjonsie
mbeuvafdghxlzyckwtfqjonwie
mbruvapcghxlayckwtfqjonsii
mbruvabdghxlzyckwtfqyansie
mbruvjpdghxlzyckwtfqgfnsie
lbruvapdghxlzyckwtfqjonriv
mbrupapdghxlzycjwtfqronsie
mbpuvapdthxlzymkwtfqjonsie
mbiuvapdgixlzyckwxfqjonsie
mbruvapdghxyzyckwtfcjonsbe
mbrurapkghxlzyckwtfqjonzie
mbrufapdrhxlzyciwtfqjonsie
mbruvapdghxlzbckwtfqjoisae
ubruhapdghxlzuckwtfqjonsie
mbruvapdjhulzyckwtfqjonshe
mbruwapdgyxlzyckntfqjonsie
mwruvapdghplzyckwtfqjonsme
mbruvapjghtlzyckwtfqgonsie
pbruvapdghhlzyckwtfrjonsie
mbruvgpdihxqzyckwtfqjonsie
mbruvahdohxlzyckwtfijonsie
ibuuvapdghxlzyckwtfqjofsie
mbruvandghxlzyckwtfqjrnxie
mbrjvlpdghxlzyckwgfqjonsie
mbruvapogfxlzyckotfqjonsie
mbruvrpdghxlzyckutfejonsie
mbruvbpdghxlzyhkwtfqjonsip
mbruvapdghxlzyckmnfqjensie
mbruvapdghvlzyckwtfqjowsix
mbruvakdgholzwckwtfqjonsie
mbruvapdghxlzackwtfqconsae
mbruvapdghxlzyqvwtfqjlnsie
mprrvapdgfxlzyckwtfqjonsie
mbrunacdghxlhyckwtfqjonsie
obruvapdgsxlzyckwtfqjonvie
murcvapdghslzyckwtfqjonsie
mbruvapdghxlzyzkwmftjonsie
mbrwvapdgtvlzyckwtfqjonsie
mbxuvapdghxlzqcnwtfqjonsie
mbruvaddghxboyckwtfqjonsie
mhruvwndghxlzyckwtfqjonsie
mbrdvapdghxlzyckwmpqjonsie
mbruvapdgyxlzyckizfqjonsie
mbruvapdghxlzlckwtfqeowsie
mbruvbpdgrxlzyckwtfqjonsxe
mbruqapoghxlzyckwtvqjonsie
mbouhapdghmlzyckwtfqjonsie
mbruvapjghxidyckwtfqjonsie
mbsuvapkghxlkyckwtfqjonsie
mbruvlpdghxlzycrwtfqjonsis
mcrueapdghxlzyckwtfqjynsie
muruvapngbxlzyckwtfqjonsie
mbruvapdghxlzycawtfyjojsie
mbruvbpdghxczyjkwtfqjonsie
ybduvapdghxnzyckwtfqjonsie
mbruvbpdghxlzyckwtfbjousie
mbouvapdghxlzycbwtfqponsie
mbruvaedghplzycgwtfqjonsie
mbrhvapdghxlzyckytfqjgnsie
mbruvapdqbxleyckwtfqjonsie
mbruvapddhhldyckwtfqjonsie
mbruvapdghxlwrckwtfqjondie
mbruvapdmhxlzyckwtfqkonsve
xbbuvapdghxlzyckwtfkjonsie
mbruvapdghxlzyckwcfqjunkie
mbruvapdghxlzyckwtfqxonfib
mbrtvapkghxlzyckwtfqeonsie
mbruvazdghxldymkwtfqjonsie
kbruvapddhxlzfckwtfqjonsie
mbouvapdghxlpyckwtfqjoosie
mbauvapdghxlzyckwtfqjszsie
mbruvapdghtlzyckntfqtonsie
mbruvipdggxlzbckwtfqjonsie
mbruqapdghrlzyckwtfqjznsie
myruvacdghxlzyckwifqjonsie
mbruvapdghxlzuckwtfkjocsie
mwjuvapdghxlzyckwtfqjonsxe
mbruvapxghxlzickwtfqjobsie
mbrupapdghxtlyckwtfqjonsie
meruvapdjjxlzyckwtfqjonsie
mbruvkodghxlzyckwofqjonsie
mbruvapdgexlzyckwtgkjonsie
mbruvapwghxlzyckwtcqjonsiw
mbruvapdghxlzykkwtfqtoxsie
mbruvapdahxlzycgwtfwjonsie
mbruvapdgwxlhyckhtfqjonsie
mbruvapbghxlzycbhmfqjonsie
mbruvapdghxvzyzkwtfqjonsin
mbrcvapdqhxlzyckwyfqjonsie
zbruvaxdghxlzyckwgfqjonsie
mtruvapdghxlilckwtfqjonsie
bbruvapdghxlzyckwtfmjonsxe
mbruvajdghxlzyckwtfqfwnsie
mbruvapdgkxlzyckwtfqionpie
rbruvapdghxlryckwdfqjonsie
mbruvandghxlzyckwmfvjonsie
mbruvahdghxlzeckwtfqjonsme
mbruvnpcghxlzyckwtfqjobsie
mbruvapdghqlzyckwtfbjonsiy
mbruvavdghxlzyckwufqjodsie
mbruvapdghxlzyckwtfzmovsie
mbruvlpdghxuzyckwtfqjoesie
mbruvopdghxlzycwwtfqjansie
obruvapdghglzybkwtfqjonsie
mbpuvlpdghxlcyckwtfqjonsie
mbruvaidghxlzyckwtfmjonoie
mbruvapdihxzzyckwtfqjonsiy
mbquvapdghxlzyckwtfqconsme
mbruvapdghslzyckqtfqjojsie
mbrzdapdghxmzyckwtfqjonsie
mwruvapdghxozyckwtfqjonsxe
muruvapdgfxlzyckwtfqjojsie
wtruvapdghxlzyckvtfqjonsie
mbruvapdghxlzyckysfqjxnsie
mbruvrpdghxczyckktfqjonsie
mbquvapdghxlryckwtfqjonsne
mbruvapdghflzycvwtfqjpnsie
mbruvapughclzyckwtfqjonsin
mbrhvapdghxlpyckwtfqjonsre
mbruvapdgtxlzyckwtfqjoosit
mbrupapnghxhzyckwtfqjonsie
mmvuvapdvhxlzyckwtfqjonsie
mbruvaptghxlzyckwtfqjotsse
mgruvapvghxlzyckwtfqjonsix
mbrupapdghxszyckwtfqjunsie
mbruvkpdghelzyckwtfqjpnsie
mbruvrrdghjlzyckwtfqjonsie
mbruvapdghnlzyckwtfkjonsze
mbruvwpdghxlzyckwtfqhoysie
mbrsvapdfhxlzyckwtfqjobsie
mbruvapdgexezymkwtfqjonsie
ybruvapdghxlzyckwtfqxonsiw
mrruvapdghxdzyckwtfqjossie
mbruvapdghtlzyckwtfqconsiu
mbrpvapdghxlzlckwpfqjonsie
mbruvjpdghslzyckwtfqjjnsie
mhruvapoghxlzyckwtfvjonsie
mbrubqpdghvlzyckwtfqjonsie
mbruvapdghxlzackwtfqconsiw
mbruvapdgnxlzkckwtfqjdnsie
mbrudapgghelzyckwtfqjonsie
mbruvapdghxlzlakwbfqjonsie
mbpuvapdghxlzyckwtuqjonjie
abruvapdghxlzykkwtfqjonzie
mbrupupdghxlsyckwtfqjonsie
mbrsvupdghxlzyckwtfqjonkie
mxruvgpdghxllyckwtfqjonsie
mbrnvapdghxlzycbwtfqfonsie
mbrbxapdghxlzyckttfqjonsie
mbnuvapdghxlzyxkwtmqjonsie
mbrfvapdghjlzickwtfqjonsie
mbhuvupdghxlzyxkwtfqjonsie
mbrcvapdghxluyckwtfqjznsie
mbruvapdghxlzyckwofqjoxsiz
mbrevapdghxloyckwtfqjonnie
mbruvipdghnlzyckwtfqjopsie
mbxxvaptghxlzyckwtfqjonsie
mbruvcpdghxlztckwtjqjonsie
mqruvlpdghxlzyckotfqjonsie
mbruvapdgqxlzyckwtfqjpvsie
mbruvapdgvxlzyjkwtfqjbnsie
mbruvapdghxlgyckwtfqcocsie
mbruvapdghxkwyckwtfqjoqsie
mbrgvavdghxlzyckwxfqjonsie
qbruqapdgvxlzyckwtfqjonsie
mbauvapdghxlzgckwtfqjunsie
mbruvapdgdxluyckwtfqjoosie
mbruvapdghxlzykkwtfqwobsie
mbruvapdghxlzhcnwtfqjonqie
mbruvapdghxlzycbhmfqjonsie
mbruvapdghxluyczwtfqjontie
mbruvapnghxlzyckwnfqjonbie
moruvapdghxlzcckwtfqponsie
mbruvapfgxxlzyckwtfqjunsie
mbruvapdghxlryckvtfejonsie
mbrzvapdghxlzvcbwtfqjonsie
mbruvapdgqxlzyckwcfqjonsce
abruvupdrhxlzyckwtfqjonsie
mbrubaptghxlzyckwtfqjondie
mgruvapdgpxlzyckwtfijonsie
mbruvapdghxczlckwtfujonsie
mbruvapdgmmlzyckwtfqjonsir
mbruvapdhhxltyckwtfdjonsie
mbruvapdghxlzyckwtfdjjnste
mbrdvzpdghxlcyckwtfqjonsie
mbruvapdghxlzyckwtnqbonsim
mbrovapdghxlzyckwtfpjousie
mymuvapdghxlzyjkwtfqjonsie
mbpuvapdghxlzyckwtfljcnsie
mbrxvapdghxlzyclwtfqjonpie
mbrueapdghxlzyckwtfqjopsia
mbruvapdghxlzycdwtfqjbfsie
tbruvavdghxlzyckwtmqjonsie
mbduvapdghxlzyckwrfqjrnsie
mkrsvapughxlzyckwtfqjonsie
mbruvapdghylzyckwtfqtolsie
mgruvapdglxldyckwtfqjonsie
mbrunapdghclzyckwtfqjonsiy
mbruvapdgrxlxyckwtfgjonsie
mbruvapdghxpzbckftfqjonsie
mbruvcpdghxyzyckotfqjonsie
mbruvapdghxlsyckwtfqcqnsie
mbruvapdghxlzzckwtfqjonskf
mbruvppdghxlzfckwtfqjgnsie
mbhuvapdghxlzytkwtfqjonoie
mbruvapdghxlzvrkwtfqjjnsie
mbmuvapdghxuzyckwtfqjonsze
mbruvapdghnlzycnwtfqjonsil
mbruvapdgholzyckitfqjonsia
mbruxapdghxlmyckwtfqbonsie
mbauvapdgholzyckwtfqjolsie
mbruvapdghxlzyckwtfqjotslq
dbrutapdghxlzyckwtfqjonsiv
mbruvapdzhxlyyckwtfbjonsie
mmruaapsghxlzyckwtfqjonsie
mbruvaldgqxqzyckwtfqjonsie
mbruvaodghxdzyjkwtfqjonsie
mbrcmatdghxlzyckwtfqjonsie
mbrqvapdgtxlzycewtfqjonsie
mjruvapdghzlzyckwtfqjonrie
mbruvapdghxopcckwtfqjonsie
mbruvapdghxszycwwtfqjoqsie
mbruvapdgoxezyckwtjqjonsie

View File

@@ -1,36 +1,36 @@
#include <string>
#include <iostream>
#include <fstream>
bool isRepeat(std::string line, int repeats) {
for (char c = 'a'; c <= 'z'; ++c) {
int count = 0;
for (size_t i = 0; i < line.size(); ++i) {
if (line[i] == c)
++count;
}
if (count == repeats)
return true;
}
return false;
}
int checkSum(std::string fileName) {
std::fstream input(fileName);
std::string line;
int doubles = 0;
int triples = 0;
while (!input.eof()) {
std::getline(input, line);
if (isRepeat(line, 2))
++doubles;
if (isRepeat(line, 3))
++triples;
}
return doubles * triples;
}
int main(int argc, char** argv) {
std::cout << "Found: " << checkSum("input.txt") << std::endl;
}
#include <string>
#include <iostream>
#include <fstream>
bool isRepeat(std::string line, int repeats) {
for (char c = 'a'; c <= 'z'; ++c) {
int count = 0;
for (size_t i = 0; i < line.size(); ++i) {
if (line[i] == c)
++count;
}
if (count == repeats)
return true;
}
return false;
}
int checkSum(std::string fileName) {
std::fstream input(fileName);
std::string line;
int doubles = 0;
int triples = 0;
while (!input.eof()) {
std::getline(input, line);
if (isRepeat(line, 2))
++doubles;
if (isRepeat(line, 3))
++triples;
}
return doubles * triples;
}
int main(int argc, char** argv) {
std::cout << "Found: " << checkSum("input.txt") << std::endl;
}

View File

@@ -1,15 +1,15 @@
--- 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.
--- 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.)

View File

@@ -1,250 +1,250 @@
ybruvapdgixszyckwtfqjonsie
mbruvapxghslyyckwtfqjonsie
mbruvapdghslzyckwtkujonsie
rwruvapdghxlzyckwtfqjcnsie
obruvapdgtxlzyckwtfqionsie
lbruvapdghxqzyckwtfqjfnsie
mbrunapdghxlzyccatfqjonsie
mbruvapdghxlzyokltfqjdnsie
ybruvapdghxlzmckwtfqjmnsie
mbruwaadghxdzyckwtfqjonsie
muruvapdghxlzyckvtfqjonsim
mbruvapdghxlkyckwtxqjonjie
mbruvaqdghxlzyckwtfqjrnnie
mwruvapdghdlzyckttfqjonsie
mbruvapdgtelzyckwxfqjonsie
mbruvapdohxlzvckwtfqjonhie
mbrugapdgbxlzyckwtfqjynsie
mbruvapdghxlzyckwtlqjonjiu
mbruvapwghxlzyckwafqjonbie
wbruvapdghxlhyckwtfqjonsii
mbruvapdghxlzyckwtcqnonsiq
mbyuvapighxlzybkwtfqjonsie
mbrrvapdghxvzyckwtfqjonsio
mhruvapdghrlzyckwtfzjonsie
mtruvapvghxlzyckwtfnjonsie
mmrlhapdghxlzyckwtfqjonsie
mbruvapdgpxlzyjkwtfqjovsie
mbrucapdghxlzymkwtzqjonsie
mbeuvafdghxlzyckwtfqjonwie
mbruvapcghxlayckwtfqjonsii
mbruvabdghxlzyckwtfqyansie
mbruvjpdghxlzyckwtfqgfnsie
lbruvapdghxlzyckwtfqjonriv
mbrupapdghxlzycjwtfqronsie
mbpuvapdthxlzymkwtfqjonsie
mbiuvapdgixlzyckwxfqjonsie
mbruvapdghxyzyckwtfcjonsbe
mbrurapkghxlzyckwtfqjonzie
mbrufapdrhxlzyciwtfqjonsie
mbruvapdghxlzbckwtfqjoisae
ubruhapdghxlzuckwtfqjonsie
mbruvapdjhulzyckwtfqjonshe
mbruwapdgyxlzyckntfqjonsie
mwruvapdghplzyckwtfqjonsme
mbruvapjghtlzyckwtfqgonsie
pbruvapdghhlzyckwtfrjonsie
mbruvgpdihxqzyckwtfqjonsie
mbruvahdohxlzyckwtfijonsie
ibuuvapdghxlzyckwtfqjofsie
mbruvandghxlzyckwtfqjrnxie
mbrjvlpdghxlzyckwgfqjonsie
mbruvapogfxlzyckotfqjonsie
mbruvrpdghxlzyckutfejonsie
mbruvbpdghxlzyhkwtfqjonsip
mbruvapdghxlzyckmnfqjensie
mbruvapdghvlzyckwtfqjowsix
mbruvakdgholzwckwtfqjonsie
mbruvapdghxlzackwtfqconsae
mbruvapdghxlzyqvwtfqjlnsie
mprrvapdgfxlzyckwtfqjonsie
mbrunacdghxlhyckwtfqjonsie
obruvapdgsxlzyckwtfqjonvie
murcvapdghslzyckwtfqjonsie
mbruvapdghxlzyzkwmftjonsie
mbrwvapdgtvlzyckwtfqjonsie
mbxuvapdghxlzqcnwtfqjonsie
mbruvaddghxboyckwtfqjonsie
mhruvwndghxlzyckwtfqjonsie
mbrdvapdghxlzyckwmpqjonsie
mbruvapdgyxlzyckizfqjonsie
mbruvapdghxlzlckwtfqeowsie
mbruvbpdgrxlzyckwtfqjonsxe
mbruqapoghxlzyckwtvqjonsie
mbouhapdghmlzyckwtfqjonsie
mbruvapjghxidyckwtfqjonsie
mbsuvapkghxlkyckwtfqjonsie
mbruvlpdghxlzycrwtfqjonsis
mcrueapdghxlzyckwtfqjynsie
muruvapngbxlzyckwtfqjonsie
mbruvapdghxlzycawtfyjojsie
mbruvbpdghxczyjkwtfqjonsie
ybduvapdghxnzyckwtfqjonsie
mbruvbpdghxlzyckwtfbjousie
mbouvapdghxlzycbwtfqponsie
mbruvaedghplzycgwtfqjonsie
mbrhvapdghxlzyckytfqjgnsie
mbruvapdqbxleyckwtfqjonsie
mbruvapddhhldyckwtfqjonsie
mbruvapdghxlwrckwtfqjondie
mbruvapdmhxlzyckwtfqkonsve
xbbuvapdghxlzyckwtfkjonsie
mbruvapdghxlzyckwcfqjunkie
mbruvapdghxlzyckwtfqxonfib
mbrtvapkghxlzyckwtfqeonsie
mbruvazdghxldymkwtfqjonsie
kbruvapddhxlzfckwtfqjonsie
mbouvapdghxlpyckwtfqjoosie
mbauvapdghxlzyckwtfqjszsie
mbruvapdghtlzyckntfqtonsie
mbruvipdggxlzbckwtfqjonsie
mbruqapdghrlzyckwtfqjznsie
myruvacdghxlzyckwifqjonsie
mbruvapdghxlzuckwtfkjocsie
mwjuvapdghxlzyckwtfqjonsxe
mbruvapxghxlzickwtfqjobsie
mbrupapdghxtlyckwtfqjonsie
meruvapdjjxlzyckwtfqjonsie
mbruvkodghxlzyckwofqjonsie
mbruvapdgexlzyckwtgkjonsie
mbruvapwghxlzyckwtcqjonsiw
mbruvapdghxlzykkwtfqtoxsie
mbruvapdahxlzycgwtfwjonsie
mbruvapdgwxlhyckhtfqjonsie
mbruvapbghxlzycbhmfqjonsie
mbruvapdghxvzyzkwtfqjonsin
mbrcvapdqhxlzyckwyfqjonsie
zbruvaxdghxlzyckwgfqjonsie
mtruvapdghxlilckwtfqjonsie
bbruvapdghxlzyckwtfmjonsxe
mbruvajdghxlzyckwtfqfwnsie
mbruvapdgkxlzyckwtfqionpie
rbruvapdghxlryckwdfqjonsie
mbruvandghxlzyckwmfvjonsie
mbruvahdghxlzeckwtfqjonsme
mbruvnpcghxlzyckwtfqjobsie
mbruvapdghqlzyckwtfbjonsiy
mbruvavdghxlzyckwufqjodsie
mbruvapdghxlzyckwtfzmovsie
mbruvlpdghxuzyckwtfqjoesie
mbruvopdghxlzycwwtfqjansie
obruvapdghglzybkwtfqjonsie
mbpuvlpdghxlcyckwtfqjonsie
mbruvaidghxlzyckwtfmjonoie
mbruvapdihxzzyckwtfqjonsiy
mbquvapdghxlzyckwtfqconsme
mbruvapdghslzyckqtfqjojsie
mbrzdapdghxmzyckwtfqjonsie
mwruvapdghxozyckwtfqjonsxe
muruvapdgfxlzyckwtfqjojsie
wtruvapdghxlzyckvtfqjonsie
mbruvapdghxlzyckysfqjxnsie
mbruvrpdghxczyckktfqjonsie
mbquvapdghxlryckwtfqjonsne
mbruvapdghflzycvwtfqjpnsie
mbruvapughclzyckwtfqjonsin
mbrhvapdghxlpyckwtfqjonsre
mbruvapdgtxlzyckwtfqjoosit
mbrupapnghxhzyckwtfqjonsie
mmvuvapdvhxlzyckwtfqjonsie
mbruvaptghxlzyckwtfqjotsse
mgruvapvghxlzyckwtfqjonsix
mbrupapdghxszyckwtfqjunsie
mbruvkpdghelzyckwtfqjpnsie
mbruvrrdghjlzyckwtfqjonsie
mbruvapdghnlzyckwtfkjonsze
mbruvwpdghxlzyckwtfqhoysie
mbrsvapdfhxlzyckwtfqjobsie
mbruvapdgexezymkwtfqjonsie
ybruvapdghxlzyckwtfqxonsiw
mrruvapdghxdzyckwtfqjossie
mbruvapdghtlzyckwtfqconsiu
mbrpvapdghxlzlckwpfqjonsie
mbruvjpdghslzyckwtfqjjnsie
mhruvapoghxlzyckwtfvjonsie
mbrubqpdghvlzyckwtfqjonsie
mbruvapdghxlzackwtfqconsiw
mbruvapdgnxlzkckwtfqjdnsie
mbrudapgghelzyckwtfqjonsie
mbruvapdghxlzlakwbfqjonsie
mbpuvapdghxlzyckwtuqjonjie
abruvapdghxlzykkwtfqjonzie
mbrupupdghxlsyckwtfqjonsie
mbrsvupdghxlzyckwtfqjonkie
mxruvgpdghxllyckwtfqjonsie
mbrnvapdghxlzycbwtfqfonsie
mbrbxapdghxlzyckttfqjonsie
mbnuvapdghxlzyxkwtmqjonsie
mbrfvapdghjlzickwtfqjonsie
mbhuvupdghxlzyxkwtfqjonsie
mbrcvapdghxluyckwtfqjznsie
mbruvapdghxlzyckwofqjoxsiz
mbrevapdghxloyckwtfqjonnie
mbruvipdghnlzyckwtfqjopsie
mbxxvaptghxlzyckwtfqjonsie
mbruvcpdghxlztckwtjqjonsie
mqruvlpdghxlzyckotfqjonsie
mbruvapdgqxlzyckwtfqjpvsie
mbruvapdgvxlzyjkwtfqjbnsie
mbruvapdghxlgyckwtfqcocsie
mbruvapdghxkwyckwtfqjoqsie
mbrgvavdghxlzyckwxfqjonsie
qbruqapdgvxlzyckwtfqjonsie
mbauvapdghxlzgckwtfqjunsie
mbruvapdgdxluyckwtfqjoosie
mbruvapdghxlzykkwtfqwobsie
mbruvapdghxlzhcnwtfqjonqie
mbruvapdghxlzycbhmfqjonsie
mbruvapdghxluyczwtfqjontie
mbruvapnghxlzyckwnfqjonbie
moruvapdghxlzcckwtfqponsie
mbruvapfgxxlzyckwtfqjunsie
mbruvapdghxlryckvtfejonsie
mbrzvapdghxlzvcbwtfqjonsie
mbruvapdgqxlzyckwcfqjonsce
abruvupdrhxlzyckwtfqjonsie
mbrubaptghxlzyckwtfqjondie
mgruvapdgpxlzyckwtfijonsie
mbruvapdghxczlckwtfujonsie
mbruvapdgmmlzyckwtfqjonsir
mbruvapdhhxltyckwtfdjonsie
mbruvapdghxlzyckwtfdjjnste
mbrdvzpdghxlcyckwtfqjonsie
mbruvapdghxlzyckwtnqbonsim
mbrovapdghxlzyckwtfpjousie
mymuvapdghxlzyjkwtfqjonsie
mbpuvapdghxlzyckwtfljcnsie
mbrxvapdghxlzyclwtfqjonpie
mbrueapdghxlzyckwtfqjopsia
mbruvapdghxlzycdwtfqjbfsie
tbruvavdghxlzyckwtmqjonsie
mbduvapdghxlzyckwrfqjrnsie
mkrsvapughxlzyckwtfqjonsie
mbruvapdghylzyckwtfqtolsie
mgruvapdglxldyckwtfqjonsie
mbrunapdghclzyckwtfqjonsiy
mbruvapdgrxlxyckwtfgjonsie
mbruvapdghxpzbckftfqjonsie
mbruvcpdghxyzyckotfqjonsie
mbruvapdghxlsyckwtfqcqnsie
mbruvapdghxlzzckwtfqjonskf
mbruvppdghxlzfckwtfqjgnsie
mbhuvapdghxlzytkwtfqjonoie
mbruvapdghxlzvrkwtfqjjnsie
mbmuvapdghxuzyckwtfqjonsze
mbruvapdghnlzycnwtfqjonsil
mbruvapdgholzyckitfqjonsia
mbruxapdghxlmyckwtfqbonsie
mbauvapdgholzyckwtfqjolsie
mbruvapdghxlzyckwtfqjotslq
dbrutapdghxlzyckwtfqjonsiv
mbruvapdzhxlyyckwtfbjonsie
mmruaapsghxlzyckwtfqjonsie
mbruvaldgqxqzyckwtfqjonsie
mbruvaodghxdzyjkwtfqjonsie
mbrcmatdghxlzyckwtfqjonsie
mbrqvapdgtxlzycewtfqjonsie
mjruvapdghzlzyckwtfqjonrie
mbruvapdghxopcckwtfqjonsie
mbruvapdghxszycwwtfqjoqsie
ybruvapdgixszyckwtfqjonsie
mbruvapxghslyyckwtfqjonsie
mbruvapdghslzyckwtkujonsie
rwruvapdghxlzyckwtfqjcnsie
obruvapdgtxlzyckwtfqionsie
lbruvapdghxqzyckwtfqjfnsie
mbrunapdghxlzyccatfqjonsie
mbruvapdghxlzyokltfqjdnsie
ybruvapdghxlzmckwtfqjmnsie
mbruwaadghxdzyckwtfqjonsie
muruvapdghxlzyckvtfqjonsim
mbruvapdghxlkyckwtxqjonjie
mbruvaqdghxlzyckwtfqjrnnie
mwruvapdghdlzyckttfqjonsie
mbruvapdgtelzyckwxfqjonsie
mbruvapdohxlzvckwtfqjonhie
mbrugapdgbxlzyckwtfqjynsie
mbruvapdghxlzyckwtlqjonjiu
mbruvapwghxlzyckwafqjonbie
wbruvapdghxlhyckwtfqjonsii
mbruvapdghxlzyckwtcqnonsiq
mbyuvapighxlzybkwtfqjonsie
mbrrvapdghxvzyckwtfqjonsio
mhruvapdghrlzyckwtfzjonsie
mtruvapvghxlzyckwtfnjonsie
mmrlhapdghxlzyckwtfqjonsie
mbruvapdgpxlzyjkwtfqjovsie
mbrucapdghxlzymkwtzqjonsie
mbeuvafdghxlzyckwtfqjonwie
mbruvapcghxlayckwtfqjonsii
mbruvabdghxlzyckwtfqyansie
mbruvjpdghxlzyckwtfqgfnsie
lbruvapdghxlzyckwtfqjonriv
mbrupapdghxlzycjwtfqronsie
mbpuvapdthxlzymkwtfqjonsie
mbiuvapdgixlzyckwxfqjonsie
mbruvapdghxyzyckwtfcjonsbe
mbrurapkghxlzyckwtfqjonzie
mbrufapdrhxlzyciwtfqjonsie
mbruvapdghxlzbckwtfqjoisae
ubruhapdghxlzuckwtfqjonsie
mbruvapdjhulzyckwtfqjonshe
mbruwapdgyxlzyckntfqjonsie
mwruvapdghplzyckwtfqjonsme
mbruvapjghtlzyckwtfqgonsie
pbruvapdghhlzyckwtfrjonsie
mbruvgpdihxqzyckwtfqjonsie
mbruvahdohxlzyckwtfijonsie
ibuuvapdghxlzyckwtfqjofsie
mbruvandghxlzyckwtfqjrnxie
mbrjvlpdghxlzyckwgfqjonsie
mbruvapogfxlzyckotfqjonsie
mbruvrpdghxlzyckutfejonsie
mbruvbpdghxlzyhkwtfqjonsip
mbruvapdghxlzyckmnfqjensie
mbruvapdghvlzyckwtfqjowsix
mbruvakdgholzwckwtfqjonsie
mbruvapdghxlzackwtfqconsae
mbruvapdghxlzyqvwtfqjlnsie
mprrvapdgfxlzyckwtfqjonsie
mbrunacdghxlhyckwtfqjonsie
obruvapdgsxlzyckwtfqjonvie
murcvapdghslzyckwtfqjonsie
mbruvapdghxlzyzkwmftjonsie
mbrwvapdgtvlzyckwtfqjonsie
mbxuvapdghxlzqcnwtfqjonsie
mbruvaddghxboyckwtfqjonsie
mhruvwndghxlzyckwtfqjonsie
mbrdvapdghxlzyckwmpqjonsie
mbruvapdgyxlzyckizfqjonsie
mbruvapdghxlzlckwtfqeowsie
mbruvbpdgrxlzyckwtfqjonsxe
mbruqapoghxlzyckwtvqjonsie
mbouhapdghmlzyckwtfqjonsie
mbruvapjghxidyckwtfqjonsie
mbsuvapkghxlkyckwtfqjonsie
mbruvlpdghxlzycrwtfqjonsis
mcrueapdghxlzyckwtfqjynsie
muruvapngbxlzyckwtfqjonsie
mbruvapdghxlzycawtfyjojsie
mbruvbpdghxczyjkwtfqjonsie
ybduvapdghxnzyckwtfqjonsie
mbruvbpdghxlzyckwtfbjousie
mbouvapdghxlzycbwtfqponsie
mbruvaedghplzycgwtfqjonsie
mbrhvapdghxlzyckytfqjgnsie
mbruvapdqbxleyckwtfqjonsie
mbruvapddhhldyckwtfqjonsie
mbruvapdghxlwrckwtfqjondie
mbruvapdmhxlzyckwtfqkonsve
xbbuvapdghxlzyckwtfkjonsie
mbruvapdghxlzyckwcfqjunkie
mbruvapdghxlzyckwtfqxonfib
mbrtvapkghxlzyckwtfqeonsie
mbruvazdghxldymkwtfqjonsie
kbruvapddhxlzfckwtfqjonsie
mbouvapdghxlpyckwtfqjoosie
mbauvapdghxlzyckwtfqjszsie
mbruvapdghtlzyckntfqtonsie
mbruvipdggxlzbckwtfqjonsie
mbruqapdghrlzyckwtfqjznsie
myruvacdghxlzyckwifqjonsie
mbruvapdghxlzuckwtfkjocsie
mwjuvapdghxlzyckwtfqjonsxe
mbruvapxghxlzickwtfqjobsie
mbrupapdghxtlyckwtfqjonsie
meruvapdjjxlzyckwtfqjonsie
mbruvkodghxlzyckwofqjonsie
mbruvapdgexlzyckwtgkjonsie
mbruvapwghxlzyckwtcqjonsiw
mbruvapdghxlzykkwtfqtoxsie
mbruvapdahxlzycgwtfwjonsie
mbruvapdgwxlhyckhtfqjonsie
mbruvapbghxlzycbhmfqjonsie
mbruvapdghxvzyzkwtfqjonsin
mbrcvapdqhxlzyckwyfqjonsie
zbruvaxdghxlzyckwgfqjonsie
mtruvapdghxlilckwtfqjonsie
bbruvapdghxlzyckwtfmjonsxe
mbruvajdghxlzyckwtfqfwnsie
mbruvapdgkxlzyckwtfqionpie
rbruvapdghxlryckwdfqjonsie
mbruvandghxlzyckwmfvjonsie
mbruvahdghxlzeckwtfqjonsme
mbruvnpcghxlzyckwtfqjobsie
mbruvapdghqlzyckwtfbjonsiy
mbruvavdghxlzyckwufqjodsie
mbruvapdghxlzyckwtfzmovsie
mbruvlpdghxuzyckwtfqjoesie
mbruvopdghxlzycwwtfqjansie
obruvapdghglzybkwtfqjonsie
mbpuvlpdghxlcyckwtfqjonsie
mbruvaidghxlzyckwtfmjonoie
mbruvapdihxzzyckwtfqjonsiy
mbquvapdghxlzyckwtfqconsme
mbruvapdghslzyckqtfqjojsie
mbrzdapdghxmzyckwtfqjonsie
mwruvapdghxozyckwtfqjonsxe
muruvapdgfxlzyckwtfqjojsie
wtruvapdghxlzyckvtfqjonsie
mbruvapdghxlzyckysfqjxnsie
mbruvrpdghxczyckktfqjonsie
mbquvapdghxlryckwtfqjonsne
mbruvapdghflzycvwtfqjpnsie
mbruvapughclzyckwtfqjonsin
mbrhvapdghxlpyckwtfqjonsre
mbruvapdgtxlzyckwtfqjoosit
mbrupapnghxhzyckwtfqjonsie
mmvuvapdvhxlzyckwtfqjonsie
mbruvaptghxlzyckwtfqjotsse
mgruvapvghxlzyckwtfqjonsix
mbrupapdghxszyckwtfqjunsie
mbruvkpdghelzyckwtfqjpnsie
mbruvrrdghjlzyckwtfqjonsie
mbruvapdghnlzyckwtfkjonsze
mbruvwpdghxlzyckwtfqhoysie
mbrsvapdfhxlzyckwtfqjobsie
mbruvapdgexezymkwtfqjonsie
ybruvapdghxlzyckwtfqxonsiw
mrruvapdghxdzyckwtfqjossie
mbruvapdghtlzyckwtfqconsiu
mbrpvapdghxlzlckwpfqjonsie
mbruvjpdghslzyckwtfqjjnsie
mhruvapoghxlzyckwtfvjonsie
mbrubqpdghvlzyckwtfqjonsie
mbruvapdghxlzackwtfqconsiw
mbruvapdgnxlzkckwtfqjdnsie
mbrudapgghelzyckwtfqjonsie
mbruvapdghxlzlakwbfqjonsie
mbpuvapdghxlzyckwtuqjonjie
abruvapdghxlzykkwtfqjonzie
mbrupupdghxlsyckwtfqjonsie
mbrsvupdghxlzyckwtfqjonkie
mxruvgpdghxllyckwtfqjonsie
mbrnvapdghxlzycbwtfqfonsie
mbrbxapdghxlzyckttfqjonsie
mbnuvapdghxlzyxkwtmqjonsie
mbrfvapdghjlzickwtfqjonsie
mbhuvupdghxlzyxkwtfqjonsie
mbrcvapdghxluyckwtfqjznsie
mbruvapdghxlzyckwofqjoxsiz
mbrevapdghxloyckwtfqjonnie
mbruvipdghnlzyckwtfqjopsie
mbxxvaptghxlzyckwtfqjonsie
mbruvcpdghxlztckwtjqjonsie
mqruvlpdghxlzyckotfqjonsie
mbruvapdgqxlzyckwtfqjpvsie
mbruvapdgvxlzyjkwtfqjbnsie
mbruvapdghxlgyckwtfqcocsie
mbruvapdghxkwyckwtfqjoqsie
mbrgvavdghxlzyckwxfqjonsie
qbruqapdgvxlzyckwtfqjonsie
mbauvapdghxlzgckwtfqjunsie
mbruvapdgdxluyckwtfqjoosie
mbruvapdghxlzykkwtfqwobsie
mbruvapdghxlzhcnwtfqjonqie
mbruvapdghxlzycbhmfqjonsie
mbruvapdghxluyczwtfqjontie
mbruvapnghxlzyckwnfqjonbie
moruvapdghxlzcckwtfqponsie
mbruvapfgxxlzyckwtfqjunsie
mbruvapdghxlryckvtfejonsie
mbrzvapdghxlzvcbwtfqjonsie
mbruvapdgqxlzyckwcfqjonsce
abruvupdrhxlzyckwtfqjonsie
mbrubaptghxlzyckwtfqjondie
mgruvapdgpxlzyckwtfijonsie
mbruvapdghxczlckwtfujonsie
mbruvapdgmmlzyckwtfqjonsir
mbruvapdhhxltyckwtfdjonsie
mbruvapdghxlzyckwtfdjjnste
mbrdvzpdghxlcyckwtfqjonsie
mbruvapdghxlzyckwtnqbonsim
mbrovapdghxlzyckwtfpjousie
mymuvapdghxlzyjkwtfqjonsie
mbpuvapdghxlzyckwtfljcnsie
mbrxvapdghxlzyclwtfqjonpie
mbrueapdghxlzyckwtfqjopsia
mbruvapdghxlzycdwtfqjbfsie
tbruvavdghxlzyckwtmqjonsie
mbduvapdghxlzyckwrfqjrnsie
mkrsvapughxlzyckwtfqjonsie
mbruvapdghylzyckwtfqtolsie
mgruvapdglxldyckwtfqjonsie
mbrunapdghclzyckwtfqjonsiy
mbruvapdgrxlxyckwtfgjonsie
mbruvapdghxpzbckftfqjonsie
mbruvcpdghxyzyckotfqjonsie
mbruvapdghxlsyckwtfqcqnsie
mbruvapdghxlzzckwtfqjonskf
mbruvppdghxlzfckwtfqjgnsie
mbhuvapdghxlzytkwtfqjonoie
mbruvapdghxlzvrkwtfqjjnsie
mbmuvapdghxuzyckwtfqjonsze
mbruvapdghnlzycnwtfqjonsil
mbruvapdgholzyckitfqjonsia
mbruxapdghxlmyckwtfqbonsie
mbauvapdgholzyckwtfqjolsie
mbruvapdghxlzyckwtfqjotslq
dbrutapdghxlzyckwtfqjonsiv
mbruvapdzhxlyyckwtfbjonsie
mmruaapsghxlzyckwtfqjonsie
mbruvaldgqxqzyckwtfqjonsie
mbruvaodghxdzyjkwtfqjonsie
mbrcmatdghxlzyckwtfqjonsie
mbrqvapdgtxlzycewtfqjonsie
mjruvapdghzlzyckwtfqjonrie
mbruvapdghxopcckwtfqjonsie
mbruvapdghxszycwwtfqjoqsie
mbruvapdgoxezyckwtjqjonsie

View File

@@ -1,50 +1,50 @@
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
std::string equalLetters(std::string fileName) {
std::fstream input(fileName);
std::string line;
std::string a;
std::string b;
std::string answer;
std::vector<std::string> lines;
while (!input.eof()) {
std::getline(input, line);
lines.push_back(line);
}
for (size_t i = 0; i < lines.size(); ++i) {
int count = 0;
a = lines[i];
for (size_t j = i + 1; j < lines.size(); ++j) {
b = lines[j];
count = 0;
for (size_t k = 0; k < a.size(); ++k) {
if (a[k] != b[k])
++count;
if (count > 1)
break;
}
if (count == 1)
break;
}
if (count == 1)
break;
}
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] == b[i])
answer += a[i];
}
return answer;
}
int main(int argc, char** argv) {
std::cout << "Found: " << equalLetters("input.txt") << std::endl;
}
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
std::string equalLetters(std::string fileName) {
std::fstream input(fileName);
std::string line;
std::string a;
std::string b;
std::string answer;
std::vector<std::string> lines;
while (!input.eof()) {
std::getline(input, line);
lines.push_back(line);
}
for (size_t i = 0; i < lines.size(); ++i) {
int count = 0;
a = lines[i];
for (size_t j = i + 1; j < lines.size(); ++j) {
b = lines[j];
count = 0;
for (size_t k = 0; k < a.size(); ++k) {
if (a[k] != b[k])
++count;
if (count > 1)
break;
}
if (count == 1)
break;
}
if (count == 1)
break;
}
for (size_t i = 0; i < a.size(); ++i) {
if (a[i] == b[i])
answer += a[i];
}
return answer;
}
int main(int argc, char** argv) {
std::cout << "Found: " << equalLetters("input.txt") << std::endl;
}

View File

@@ -1,107 +1,107 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace challenge1 {
class Program {
static string[] Lines {get; set;}
static List<Claim> Claims {get; set;}
static void Main(string[] args) {
Claims = new List<Claim>();
Console.WriteLine("Reading input.txt");
Lines = File.ReadAllLines("./input.txt");
foreach (string line in Lines) {
Claim current = new Claim(line);
Claims.Add(current);
current.Print();
}
Fabric fabric = new Fabric(Claims);
Console.WriteLine("Found: " + fabric.CalculateOverlaps() + " overlaps");
}
public static void printArray(string[] arr) {
foreach (string str in arr) {
Console.WriteLine(str);
}
}
}
class Claim {
public int ID {get; set;}
public int X {get; set;}
public int Y {get; set;}
public int W {get; set;}
public int H {get; set;}
public string Source {get; set;}
public Claim(string source) {
this.Source = source;
ParseSource();
}
private void ParseSource() {
Console.WriteLine("Parsing: " + this.Source);
string[] source = this.Source.Split('#');
string[] idAndRest = source[1].Split('@');
this.ID = int.Parse(idAndRest[0]);
string[] fromSides = idAndRest[1].Split(',');
string[] afterColon = fromSides[1].Split(':');
this.X = int.Parse(fromSides[0]);
this.Y = int.Parse(afterColon[0]);
string[] dimensions = afterColon[1].Split('x');
this.W = int.Parse(dimensions[0]);
this.H = int.Parse(dimensions[1]);
}
public void Print() {
Console.WriteLine("Claim ID: " + this.ID);
Console.WriteLine("X, Y: " + this.X + ',' + this.Y);
Console.WriteLine("Dimensions: " + this.W + 'x' + this.H);
Console.WriteLine("Parsed from source: " + this.Source);
Console.WriteLine();
}
}
class Fabric {
public List<Claim> Claims;
private int[,] Board;
public Fabric(List<Claim> claims) {
this.Claims = claims;
this.Board = new int[10000, 10000];
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
Board[i, j] = 0;
}
}
}
public int CalculateOverlaps() {
int overlaps = 0;
int i = 0;
foreach (Claim claim in this.Claims) {
Console.WriteLine("Processing claim ID: " + claim.ID);
for (int x = claim.X; x < claim.X + claim.W; x++) {
for (int y = claim.Y; y < claim.Y + claim.H; y++) {
Board[x, y]++;
}
}
i++;
}
for (i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
if (Board[i,j] > 1) overlaps++;
}
}
return overlaps;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace challenge1 {
class Program {
static string[] Lines {get; set;}
static List<Claim> Claims {get; set;}
static void Main(string[] args) {
Claims = new List<Claim>();
Console.WriteLine("Reading input.txt");
Lines = File.ReadAllLines("./input.txt");
foreach (string line in Lines) {
Claim current = new Claim(line);
Claims.Add(current);
current.Print();
}
Fabric fabric = new Fabric(Claims);
Console.WriteLine("Found: " + fabric.CalculateOverlaps() + " overlaps");
}
public static void printArray(string[] arr) {
foreach (string str in arr) {
Console.WriteLine(str);
}
}
}
class Claim {
public int ID {get; set;}
public int X {get; set;}
public int Y {get; set;}
public int W {get; set;}
public int H {get; set;}
public string Source {get; set;}
public Claim(string source) {
this.Source = source;
ParseSource();
}
private void ParseSource() {
Console.WriteLine("Parsing: " + this.Source);
string[] source = this.Source.Split('#');
string[] idAndRest = source[1].Split('@');
this.ID = int.Parse(idAndRest[0]);
string[] fromSides = idAndRest[1].Split(',');
string[] afterColon = fromSides[1].Split(':');
this.X = int.Parse(fromSides[0]);
this.Y = int.Parse(afterColon[0]);
string[] dimensions = afterColon[1].Split('x');
this.W = int.Parse(dimensions[0]);
this.H = int.Parse(dimensions[1]);
}
public void Print() {
Console.WriteLine("Claim ID: " + this.ID);
Console.WriteLine("X, Y: " + this.X + ',' + this.Y);
Console.WriteLine("Dimensions: " + this.W + 'x' + this.H);
Console.WriteLine("Parsed from source: " + this.Source);
Console.WriteLine();
}
}
class Fabric {
public List<Claim> Claims;
private int[,] Board;
public Fabric(List<Claim> claims) {
this.Claims = claims;
this.Board = new int[10000, 10000];
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
Board[i, j] = 0;
}
}
}
public int CalculateOverlaps() {
int overlaps = 0;
int i = 0;
foreach (Claim claim in this.Claims) {
Console.WriteLine("Processing claim ID: " + claim.ID);
for (int x = claim.X; x < claim.X + claim.W; x++) {
for (int y = claim.Y; y < claim.Y + claim.H; y++) {
Board[x, y]++;
}
}
i++;
}
for (i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
if (Board[i,j] > 1) overlaps++;
}
}
return overlaps;
}
}
}

View File

@@ -1,40 +1,40 @@
--- 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.)
--- 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?

View File

@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>

File diff suppressed because it is too large Load Diff

View File

@@ -1,115 +1,115 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace challenge2 {
class Program {
static string[] Lines {get; set;}
static List<Claim> Claims {get; set;}
static void Main(string[] args) {
Claims = new List<Claim>();
Console.WriteLine("Reading input.txt");
Lines = File.ReadAllLines("./input.txt");
foreach (string line in Lines) {
Claim current = new Claim(line);
Claims.Add(current);
current.Print();
}
Fabric fabric = new Fabric(Claims);
Console.WriteLine("Found ID: " + fabric.CalculateOverlaps());
}
public static void printArray(string[] arr) {
foreach (string str in arr) {
Console.WriteLine(str);
}
}
}
class Claim {
public int ID {get; set;}
public int X {get; set;}
public int Y {get; set;}
public int W {get; set;}
public int H {get; set;}
public string Source {get; set;}
public Claim(string source) {
this.Source = source;
ParseSource();
}
private void ParseSource() {
Console.WriteLine("Parsing: " + this.Source);
string[] source = this.Source.Split('#');
string[] idAndRest = source[1].Split('@');
this.ID = int.Parse(idAndRest[0]);
string[] fromSides = idAndRest[1].Split(',');
string[] afterColon = fromSides[1].Split(':');
this.X = int.Parse(fromSides[0]);
this.Y = int.Parse(afterColon[0]);
string[] dimensions = afterColon[1].Split('x');
this.W = int.Parse(dimensions[0]);
this.H = int.Parse(dimensions[1]);
}
public void Print() {
Console.WriteLine("Claim ID: " + this.ID);
Console.WriteLine("X, Y: " + this.X + ',' + this.Y);
Console.WriteLine("Dimensions: " + this.W + 'x' + this.H);
Console.WriteLine("Parsed from source: " + this.Source);
Console.WriteLine();
}
}
class Fabric {
public List<Claim> Claims;
private int[,] Board;
public Fabric(List<Claim> claims) {
this.Claims = claims;
this.Board = new int[10000, 10000];
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
Board[i, j] = 0;
}
}
}
public int CalculateOverlaps() {
int overlaps = 0;
foreach (Claim claim in this.Claims) {
for (int x = claim.X; x < claim.X + claim.W; x++) {
for (int y = claim.Y; y < claim.Y + claim.H; y++) {
Board[x, y]++;
}
}
}
foreach (Claim claim in this.Claims) {
bool isValid = true;
Console.WriteLine("Processing claim ID: " + claim.ID);
for (int x = claim.X; x < claim.X + claim.W; x++) {
for (int y = claim.Y; y < claim.Y + claim.H; y++) {
if(this.Board[x,y] != 1) {
isValid = false;
break;
}
}
if (!isValid) break;
}
if (isValid) {
overlaps = claim.ID;
break;
}
}
return overlaps;
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace challenge2 {
class Program {
static string[] Lines {get; set;}
static List<Claim> Claims {get; set;}
static void Main(string[] args) {
Claims = new List<Claim>();
Console.WriteLine("Reading input.txt");
Lines = File.ReadAllLines("./input.txt");
foreach (string line in Lines) {
Claim current = new Claim(line);
Claims.Add(current);
current.Print();
}
Fabric fabric = new Fabric(Claims);
Console.WriteLine("Found ID: " + fabric.CalculateOverlaps());
}
public static void printArray(string[] arr) {
foreach (string str in arr) {
Console.WriteLine(str);
}
}
}
class Claim {
public int ID {get; set;}
public int X {get; set;}
public int Y {get; set;}
public int W {get; set;}
public int H {get; set;}
public string Source {get; set;}
public Claim(string source) {
this.Source = source;
ParseSource();
}
private void ParseSource() {
Console.WriteLine("Parsing: " + this.Source);
string[] source = this.Source.Split('#');
string[] idAndRest = source[1].Split('@');
this.ID = int.Parse(idAndRest[0]);
string[] fromSides = idAndRest[1].Split(',');
string[] afterColon = fromSides[1].Split(':');
this.X = int.Parse(fromSides[0]);
this.Y = int.Parse(afterColon[0]);
string[] dimensions = afterColon[1].Split('x');
this.W = int.Parse(dimensions[0]);
this.H = int.Parse(dimensions[1]);
}
public void Print() {
Console.WriteLine("Claim ID: " + this.ID);
Console.WriteLine("X, Y: " + this.X + ',' + this.Y);
Console.WriteLine("Dimensions: " + this.W + 'x' + this.H);
Console.WriteLine("Parsed from source: " + this.Source);
Console.WriteLine();
}
}
class Fabric {
public List<Claim> Claims;
private int[,] Board;
public Fabric(List<Claim> claims) {
this.Claims = claims;
this.Board = new int[10000, 10000];
for (int i = 0; i < 10000; i++) {
for (int j = 0; j < 10000; j++) {
Board[i, j] = 0;
}
}
}
public int CalculateOverlaps() {
int overlaps = 0;
foreach (Claim claim in this.Claims) {
for (int x = claim.X; x < claim.X + claim.W; x++) {
for (int y = claim.Y; y < claim.Y + claim.H; y++) {
Board[x, y]++;
}
}
}
foreach (Claim claim in this.Claims) {
bool isValid = true;
Console.WriteLine("Processing claim ID: " + claim.ID);
for (int x = claim.X; x < claim.X + claim.W; x++) {
for (int y = claim.Y; y < claim.Y + claim.H; y++) {
if(this.Board[x,y] != 1) {
isValid = false;
break;
}
}
if (!isValid) break;
}
if (isValid) {
overlaps = claim.ID;
break;
}
}
return overlaps;
}
}
}

View File

@@ -1,11 +1,11 @@
Your puzzle answer was 109143.
The first half of this puzzle is complete! It provides one gold star: *
--- 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.
Your puzzle answer was 109143.
The first half of this puzzle is complete! It provides one gold star: *
--- 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?

View File

@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
</Project>

File diff suppressed because it is too large Load Diff

View File

@@ -1,37 +1,37 @@
{
"files.associations": {
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"*.tcc": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"utility": "cpp"
}
{
"files.associations": {
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"*.tcc": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"unordered_map": "cpp",
"vector": "cpp",
"exception": "cpp",
"fstream": "cpp",
"initializer_list": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"new": "cpp",
"optional": "cpp",
"ostream": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"type_traits": "cpp",
"tuple": "cpp",
"typeinfo": "cpp",
"utility": "cpp"
}
}

View File

@@ -1,49 +1,49 @@
--- 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.
--- 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.)

View File

@@ -1,71 +1,71 @@
class Guard {
constructor (id) {
this.id = id;
this.minutesAsleep = Array.from({ length: 60 }).map(() => 0);
this.fellAsleepAt = null;
}
sleep (fellAsleepAt) {
this.fellAsleepAt = fellAsleepAt;
}
awake (awokeAt) {
for (let i = this.fellAsleepAt; i < awokeAt; i++) {
this.minutesAsleep[i] += 1;
}
}
get totalSleepMinutes () {
return this.minutesAsleep.reduce((a, b) => a + b, 0);
}
get sleepiestMinute () {
return this.minutesAsleep
.map((value, index) => ({ index, value }))
.sort((a, b) => b.value - a.value)[0].index;
}
}
const sleep = (input) => {
const guards = {};
const schedule = input
.split('\n')
.map((x) => x.trim())
.sort()
.map((x) => {
const parts = x.match(/\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.*)/);
return {
minute: +parts[5],
message: parts[6],
};
});
let currentGuard = null;
for (let i = 0; i < schedule.length; i++) {
const { message, minute } = schedule[i];
if (/Guard/.test(message)) {
const parts = message.match(/Guard #(\d+) begins shift/);
const guardId = +parts[1];
const guard = guards[guardId] ? guards[guardId] : new Guard(guardId);
currentGuard = guards[guardId] = guard;
} else if (message === 'falls asleep') {
currentGuard.sleep(minute);
} else if (message === 'wakes up') {
currentGuard.awake(minute);
}
}
const sleepiestGuard = Object
.keys(guards)
.map((guardId) => guards[guardId])
.sort((a, b) => b.totalSleepMinutes - a.totalSleepMinutes)[0];
return sleepiestGuard.id * sleepiestGuard.sleepiestMinute;
}
const fs = require('fs');
console.log(sleep(fs.readFileSync("input.txt").toString()));
class Guard {
constructor (id) {
this.id = id;
this.minutesAsleep = Array.from({ length: 60 }).map(() => 0);
this.fellAsleepAt = null;
}
sleep (fellAsleepAt) {
this.fellAsleepAt = fellAsleepAt;
}
awake (awokeAt) {
for (let i = this.fellAsleepAt; i < awokeAt; i++) {
this.minutesAsleep[i] += 1;
}
}
get totalSleepMinutes () {
return this.minutesAsleep.reduce((a, b) => a + b, 0);
}
get sleepiestMinute () {
return this.minutesAsleep
.map((value, index) => ({ index, value }))
.sort((a, b) => b.value - a.value)[0].index;
}
}
const sleep = (input) => {
const guards = {};
const schedule = input
.split('\n')
.map((x) => x.trim())
.sort()
.map((x) => {
const parts = x.match(/\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.*)/);
return {
minute: +parts[5],
message: parts[6],
};
});
let currentGuard = null;
for (let i = 0; i < schedule.length; i++) {
const { message, minute } = schedule[i];
if (/Guard/.test(message)) {
const parts = message.match(/Guard #(\d+) begins shift/);
const guardId = +parts[1];
const guard = guards[guardId] ? guards[guardId] : new Guard(guardId);
currentGuard = guards[guardId] = guard;
} else if (message === 'falls asleep') {
currentGuard.sleep(minute);
} else if (message === 'wakes up') {
currentGuard.awake(minute);
}
}
const sleepiestGuard = Object
.keys(guards)
.map((guardId) => guards[guardId])
.sort((a, b) => b.totalSleepMinutes - a.totalSleepMinutes)[0];
return sleepiestGuard.id * sleepiestGuard.sleepiestMinute;
}
const fs = require('fs');
console.log(sleep(fs.readFileSync("input.txt").toString()));

File diff suppressed because it is too large Load Diff

View File

@@ -1,180 +1,180 @@
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <iomanip>
enum class State {begin, sleep, awaken};
struct Action
{
State state;
int64_t guard_id;
};
struct Event
{
int64_t month, day, hour, minute;
Action action;
};
std::istream &operator>>(std::istream &is, Action &action)
{
std::string element;
is >> element;
if(element=="Guard")
{
action.state=State::begin;
char c;
is >> c >> action.guard_id;
}
else if(element=="falls")
{
action.state=State::sleep;
}
else if(element=="wakes")
{
action.state=State::awaken;
}
else
{
throw std::runtime_error("Invalid input");
}
std::getline(is,element);
return is;
}
std::istream &operator>>(std::istream &is, Event &event)
{
char c;
int64_t year;
is >> c;
if(is.good())
{
is >> year >> c >> event.month >> c >> event.day
>> event.hour >> c >> event.minute >> c
>> event.action;
if(event.hour==23)
{
event.minute=0;
}
}
return is;
}
std::ostream &operator<<(std::ostream &os, const Action &action)
{
switch(action.state)
{
case State::begin:
os << "Guard #" << action.guard_id << " begins shift";
break;
case State::sleep:
os << "falls asleep";
break;
case State::awaken:
os << "wakes up";
break;
default:
os << "What?";
break;
}
return os;
}
std::ostream &operator<<(std::ostream &os, const Event &event)
{
os << "[1518-"
<< std::setw(2) << std::setfill('0') << event.month << "-"
<< std::setw(2) << std::setfill('0') << event.day << " "
<< std::setw(2) << std::setfill('0') << event.hour << ":"
<< std::setw(2) << std::setfill('0') << event.minute << "] "
<< event.action;
return os;
}
std::pair<int64_t,int64_t> max_incident(const std::vector<std::pair<int64_t,
int64_t>> &incidents)
{
std::vector<int64_t> sleep_incidents(60,0);
for(auto &times: incidents)
{
for(size_t time=times.first; time<times.second; ++time)
{ ++(sleep_incidents[time]); }
}
int64_t max_incidents(0), max_minute(0);
for(size_t time=0; time<sleep_incidents.size(); ++time)
{
if(max_incidents<sleep_incidents[time])
{
max_incidents=sleep_incidents[time];
max_minute=time;
}
}
return std::make_pair(max_incidents,max_minute);
}
int main(int argc, char *argv[])
{
std::ifstream infile(argv[1]);
std::vector<Event> events(std::istream_iterator<Event>(infile), {});
std::map<int,std::vector<std::pair<int64_t,int64_t>>> guards;
int64_t guard_id;
int64_t start(-1);
for(auto &event: events)
{
switch(event.action.state)
{
case State::begin:
guard_id=event.action.guard_id;
break;
case State::sleep:
start=event.minute;
break;
case State::awaken:
guards[guard_id].emplace_back(start,event.minute);
break;
}
}
int64_t max_time_guard, max_time(0);
for(auto &guard: guards)
{
int64_t total_time(0);
for(auto &time: guard.second)
{
total_time+=time.second-time.first;
}
if(total_time>max_time)
{
max_time_guard=guard.first;
max_time=total_time;
}
}
auto [max_incidents,max_minute]=max_incident(guards[max_time_guard]);
std::cout << "Part 1: " << (max_minute*max_time_guard) << "\n";
std::vector<int64_t> num_incidents(guards.size());
int64_t max_guard_incidents(0), max_guard_minute(0), max_guard_id(-1);
for(auto &guard: guards)
{
auto [incidents,minute]=max_incident(guard.second);
if(incidents>max_guard_incidents)
{
max_guard_incidents=incidents;
max_guard_minute=minute;
max_guard_id=guard.first;
}
}
std::cout << "Part 2: " << (max_guard_id * max_guard_minute) << "\n";
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <vector>
#include <map>
#include <iomanip>
enum class State {begin, sleep, awaken};
struct Action
{
State state;
int64_t guard_id;
};
struct Event
{
int64_t month, day, hour, minute;
Action action;
};
std::istream &operator>>(std::istream &is, Action &action)
{
std::string element;
is >> element;
if(element=="Guard")
{
action.state=State::begin;
char c;
is >> c >> action.guard_id;
}
else if(element=="falls")
{
action.state=State::sleep;
}
else if(element=="wakes")
{
action.state=State::awaken;
}
else
{
throw std::runtime_error("Invalid input");
}
std::getline(is,element);
return is;
}
std::istream &operator>>(std::istream &is, Event &event)
{
char c;
int64_t year;
is >> c;
if(is.good())
{
is >> year >> c >> event.month >> c >> event.day
>> event.hour >> c >> event.minute >> c
>> event.action;
if(event.hour==23)
{
event.minute=0;
}
}
return is;
}
std::ostream &operator<<(std::ostream &os, const Action &action)
{
switch(action.state)
{
case State::begin:
os << "Guard #" << action.guard_id << " begins shift";
break;
case State::sleep:
os << "falls asleep";
break;
case State::awaken:
os << "wakes up";
break;
default:
os << "What?";
break;
}
return os;
}
std::ostream &operator<<(std::ostream &os, const Event &event)
{
os << "[1518-"
<< std::setw(2) << std::setfill('0') << event.month << "-"
<< std::setw(2) << std::setfill('0') << event.day << " "
<< std::setw(2) << std::setfill('0') << event.hour << ":"
<< std::setw(2) << std::setfill('0') << event.minute << "] "
<< event.action;
return os;
}
std::pair<int64_t,int64_t> max_incident(const std::vector<std::pair<int64_t,
int64_t>> &incidents)
{
std::vector<int64_t> sleep_incidents(60,0);
for(auto &times: incidents)
{
for(size_t time=times.first; time<times.second; ++time)
{ ++(sleep_incidents[time]); }
}
int64_t max_incidents(0), max_minute(0);
for(size_t time=0; time<sleep_incidents.size(); ++time)
{
if(max_incidents<sleep_incidents[time])
{
max_incidents=sleep_incidents[time];
max_minute=time;
}
}
return std::make_pair(max_incidents,max_minute);
}
int main(int argc, char *argv[])
{
std::ifstream infile(argv[1]);
std::vector<Event> events(std::istream_iterator<Event>(infile), {});
std::map<int,std::vector<std::pair<int64_t,int64_t>>> guards;
int64_t guard_id;
int64_t start(-1);
for(auto &event: events)
{
switch(event.action.state)
{
case State::begin:
guard_id=event.action.guard_id;
break;
case State::sleep:
start=event.minute;
break;
case State::awaken:
guards[guard_id].emplace_back(start,event.minute);
break;
}
}
int64_t max_time_guard, max_time(0);
for(auto &guard: guards)
{
int64_t total_time(0);
for(auto &time: guard.second)
{
total_time+=time.second-time.first;
}
if(total_time>max_time)
{
max_time_guard=guard.first;
max_time=total_time;
}
}
auto [max_incidents,max_minute]=max_incident(guards[max_time_guard]);
std::cout << "Part 1: " << (max_minute*max_time_guard) << "\n";
std::vector<int64_t> num_incidents(guards.size());
int64_t max_guard_incidents(0), max_guard_minute(0), max_guard_id(-1);
for(auto &guard: guards)
{
auto [incidents,minute]=max_incident(guard.second);
if(incidents>max_guard_incidents)
{
max_guard_incidents=incidents;
max_guard_minute=minute;
max_guard_id=guard.first;
}
}
std::cout << "Part 2: " << (max_guard_id * max_guard_minute) << "\n";
}

View File

@@ -1,7 +1,7 @@
--- 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.)
--- 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.)

View File

@@ -1,68 +1,68 @@
class Guard {
constructor (id) {
this.id = id;
this.minutesAsleep = Array.from({ length: 60 }).map(() => 0);
this.fellAsleepAt = null;
}
sleep (fellAsleepAt) {
this.fellAsleepAt = fellAsleepAt;
}
awake (awokeAt) {
for (let i = this.fellAsleepAt; i < awokeAt; i++) {
this.minutesAsleep[i] += 1;
}
}
get sleepiestMinute () {
return this.minutesAsleep
.map((value, index) => ({ index, value }))
.sort((a, b) => b.value - a.value)[0].index;
}
}
const sleep = (input) => {
const guards = {};
const schedule = input
.split('\n')
.map((x) => x.trim())
.sort()
.map((x) => {
const parts = x.match(/\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.*)/);
return {
minute: +parts[5],
message: parts[6],
};
});
let currentGuard = null;
for (let i = 0; i < schedule.length; i++) {
const { message, minute } = schedule[i];
if (/Guard/.test(message)) {
const parts = message.match(/Guard #(\d+) begins shift/);
const guardId = +parts[1];
const guard = guards[guardId] ? guards[guardId] : new Guard(guardId);
currentGuard = guards[guardId] = guard;
} else if (message === 'falls asleep') {
currentGuard.sleep(minute);
} else if (message === 'wakes up') {
currentGuard.awake(minute);
}
}
const sleepiestGuard = Object
.keys(guards)
.map((guardId) => guards[guardId])
.sort((a, b) => b.minutesAsleep[b.sleepiestMinute] -
a.minutesAsleep[a.sleepiestMinute])[0];
return sleepiestGuard.id * sleepiestGuard.sleepiestMinute;
};
const fs = require('fs');
console.log(sleep(fs.readFileSync("input.txt").toString()));
class Guard {
constructor (id) {
this.id = id;
this.minutesAsleep = Array.from({ length: 60 }).map(() => 0);
this.fellAsleepAt = null;
}
sleep (fellAsleepAt) {
this.fellAsleepAt = fellAsleepAt;
}
awake (awokeAt) {
for (let i = this.fellAsleepAt; i < awokeAt; i++) {
this.minutesAsleep[i] += 1;
}
}
get sleepiestMinute () {
return this.minutesAsleep
.map((value, index) => ({ index, value }))
.sort((a, b) => b.value - a.value)[0].index;
}
}
const sleep = (input) => {
const guards = {};
const schedule = input
.split('\n')
.map((x) => x.trim())
.sort()
.map((x) => {
const parts = x.match(/\[(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})\] (.*)/);
return {
minute: +parts[5],
message: parts[6],
};
});
let currentGuard = null;
for (let i = 0; i < schedule.length; i++) {
const { message, minute } = schedule[i];
if (/Guard/.test(message)) {
const parts = message.match(/Guard #(\d+) begins shift/);
const guardId = +parts[1];
const guard = guards[guardId] ? guards[guardId] : new Guard(guardId);
currentGuard = guards[guardId] = guard;
} else if (message === 'falls asleep') {
currentGuard.sleep(minute);
} else if (message === 'wakes up') {
currentGuard.awake(minute);
}
}
const sleepiestGuard = Object
.keys(guards)
.map((guardId) => guards[guardId])
.sort((a, b) => b.minutesAsleep[b.sleepiestMinute] -
a.minutesAsleep[a.sleepiestMinute])[0];
return sleepiestGuard.id * sleepiestGuard.sleepiestMinute;
};
const fs = require('fs');
console.log(sleep(fs.readFileSync("input.txt").toString()));

File diff suppressed because it is too large Load Diff

View File

@@ -1,5 +1,5 @@
{
"files.associations": {
"ostream": "cpp"
}
{
"files.associations": {
"ostream": "cpp"
}
}

View File

@@ -1,22 +1,22 @@
--- 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.
--- 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.)

View File

@@ -1,5 +1,5 @@
const day4 = require('./istolethisfromsomeone');
const fs = require('fs');
console.log('Part 1: ' + day4.part1(fs.readFileSync('input.txt').toString()));
console.log('Part 2: ' + day4.part2(fs.readFileSync('input.txt').toString()));
const day4 = require('./istolethisfromsomeone');
const fs = require('fs');
console.log('Part 1: ' + day4.part1(fs.readFileSync('input.txt').toString()));
console.log('Part 2: ' + day4.part2(fs.readFileSync('input.txt').toString()));

File diff suppressed because one or more lines are too long

View File

@@ -1,44 +1,44 @@
function part1(data) {
const polymer = data.split("");
const removeChar = ""; // No removal
const stack = collapsePloymer(polymer, removeChar);
return stack.length;
}
function collapsePloymer(ploymerChars, removeChar) {
const stack = [];
for (let i = 0; i < ploymerChars.length; i++) {
const char = ploymerChars[i];
if (char.toLowerCase() === removeChar.toLowerCase()) {
continue;
}
const last = stack.pop();
if (!last) {
stack.push(char);
continue;
}
if (last.toLowerCase() === char.toLowerCase() && last !== char) {
continue;
}
stack.push(last);
stack.push(char);
}
return stack;
}
function part2(data) {
const polymer = data.split("");
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
const collapsedPolymers = alphabet.map(letter =>
collapsePloymer(polymer, letter)
);
const shortestCollapse = collapsedPolymers.reduce((longest, curr) =>
longest.length < curr.length ? longest : curr
);
return shortestCollapse.length;
}
module.exports = {
part1: part1,
part2: part2
function part1(data) {
const polymer = data.split("");
const removeChar = ""; // No removal
const stack = collapsePloymer(polymer, removeChar);
return stack.length;
}
function collapsePloymer(ploymerChars, removeChar) {
const stack = [];
for (let i = 0; i < ploymerChars.length; i++) {
const char = ploymerChars[i];
if (char.toLowerCase() === removeChar.toLowerCase()) {
continue;
}
const last = stack.pop();
if (!last) {
stack.push(char);
continue;
}
if (last.toLowerCase() === char.toLowerCase() && last !== char) {
continue;
}
stack.push(last);
stack.push(char);
}
return stack;
}
function part2(data) {
const polymer = data.split("");
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
const collapsedPolymers = alphabet.map(letter =>
collapsePloymer(polymer, letter)
);
const shortestCollapse = collapsedPolymers.reduce((longest, curr) =>
longest.length < curr.length ? longest : curr
);
return shortestCollapse.length;
}
module.exports = {
part1: part1,
part2: part2
};

View File

@@ -1,43 +1,43 @@
#include <iostream>
#include <fstream>
#include <string.h>
int main(int argc, char** argv) {
if (argc != 2) return 0;
std::fstream input;
input.open(*(argv + 1));
std::string polymerString;
std::getline(input, polymerString);
while (!false) {
int numOfMods = 0;
std::string eh = "";
for (unsigned int i = 0; i < polymerString.length() - 1; i++) {
if (islower((char)polymerString[i])) {
if (toupper((char)polymerString[i]) == (char)polymerString[i+1]) {
eh += polymerString.substr(0, i);
eh += polymerString.substr(i + 2, polymerString.length());
numOfMods++;
break;
}
}
if (isupper((char)polymerString[i])) {
if (tolower((char)polymerString[i]) == (char)polymerString[i+1]) {
eh += polymerString.substr(0, i);
eh += polymerString.substr(i + 2, polymerString.length());
numOfMods++;
break;
}
}
}
if (eh != "") {
polymerString = eh;
}
std::cout << "Current length: " <<polymerString.length() << std::endl;
if (numOfMods == 0) break;
}
std::cout << "Found polymers: " << polymerString << std::endl;
std::cout << "At length: " << polymerString.length() << std::endl;
}
#include <iostream>
#include <fstream>
#include <string.h>
int main(int argc, char** argv) {
if (argc != 2) return 0;
std::fstream input;
input.open(*(argv + 1));
std::string polymerString;
std::getline(input, polymerString);
while (!false) {
int numOfMods = 0;
std::string eh = "";
for (unsigned int i = 0; i < polymerString.length() - 1; i++) {
if (islower((char)polymerString[i])) {
if (toupper((char)polymerString[i]) == (char)polymerString[i+1]) {
eh += polymerString.substr(0, i);
eh += polymerString.substr(i + 2, polymerString.length());
numOfMods++;
break;
}
}
if (isupper((char)polymerString[i])) {
if (tolower((char)polymerString[i]) == (char)polymerString[i+1]) {
eh += polymerString.substr(0, i);
eh += polymerString.substr(i + 2, polymerString.length());
numOfMods++;
break;
}
}
}
if (eh != "") {
polymerString = eh;
}
std::cout << "Current length: " <<polymerString.length() << std::endl;
if (numOfMods == 0) break;
}
std::cout << "Found polymers: " << polymerString << std::endl;
std::cout << "At length: " << polymerString.length() << std::endl;
}

View File

@@ -1,14 +1,14 @@
--- 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?
--- 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?

File diff suppressed because one or more lines are too long

View File

@@ -1,81 +1,81 @@
--- 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?
--- 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?
--- 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?
--- 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?

View File

@@ -1,50 +1,50 @@
194, 200
299, 244
269, 329
292, 55
211, 63
123, 311
212, 90
292, 169
359, 177
354, 95
101, 47
95, 79
95, 287
294, 126
81, 267
330, 78
202, 165
225, 178
266, 272
351, 326
180, 62
102, 178
151, 101
343, 145
205, 312
74, 193
221, 56
89, 89
242, 172
59, 138
83, 179
223, 88
297, 234
147, 351
226, 320
358, 338
321, 172
54, 122
263, 165
126, 341
64, 132
264, 306
72, 202
98, 49
238, 67
310, 303
277, 281
222, 318
357, 169
194, 200
299, 244
269, 329
292, 55
211, 63
123, 311
212, 90
292, 169
359, 177
354, 95
101, 47
95, 79
95, 287
294, 126
81, 267
330, 78
202, 165
225, 178
266, 272
351, 326
180, 62
102, 178
151, 101
343, 145
205, 312
74, 193
221, 56
89, 89
242, 172
59, 138
83, 179
223, 88
297, 234
147, 351
226, 320
358, 338
321, 172
54, 122
263, 165
126, 341
64, 132
264, 306
72, 202
98, 49
238, 67
310, 303
277, 281
222, 318
357, 169
123, 225

View File

@@ -1,131 +1,131 @@
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
struct Point
{
int64_t x, y;
Point(const int64_t &X, const int64_t &Y) : x(X), y(Y) {}
Point() = default;
};
int64_t distance(const Point &a, const Point &b)
{
return std::abs(a.x - b.x) + std::abs(a.y - b.y);
}
std::istream &operator>>(std::istream &is, Point &p)
{
char c;
is >> p.x >> c >> p.y;
return is;
}
std::ostream &operator<<(std::ostream &os, Point &p)
{
char c;
os << p.x << ", " << p.y;
return os;
}
size_t min_index(const size_t &invalid, const Point &point,
const std::vector<Point> &points)
{
size_t result;
int64_t min_dist(std::numeric_limits<int64_t>::max());
for(size_t p = 0; p < points.size(); ++p)
{
int64_t d(distance(point, points[p]));
if(min_dist > d)
{
min_dist = d;
result = p;
}
else if(min_dist == d)
{
result = invalid;
}
}
return result;
}
int main(int argc, char *argv[])
{
std::ifstream infile(argv[1]);
std::vector<Point> points(std::istream_iterator<Point>(infile), {});
int64_t min_x(std::numeric_limits<int64_t>::max()), min_y(min_x),
max_x(std::numeric_limits<int64_t>::min()), max_y(max_x);
for(auto &p : points)
{
min_x = std::min(min_x, p.x);
min_y = std::min(min_y, p.y);
max_x = std::max(max_x, p.x);
max_y = std::max(max_y, p.y);
}
int64_t width(max_x - min_x + 1), height(max_y - min_y + 1);
const size_t invalid(points.size());
std::vector<int64_t> num_claimed(points.size() + 1, 0);
std::set<size_t> invalid_points;
for(int64_t x = min_x; x <= max_x; ++x)
{
invalid_points.insert(min_index(invalid, Point(x, min_y), points));
invalid_points.insert(min_index(invalid, Point(x, max_y), points));
}
for(int64_t y = min_y; y <= max_y; ++y)
{
invalid_points.insert(min_index(invalid, Point(min_x, y), points));
invalid_points.insert(min_index(invalid, Point(max_x, y), points));
}
for(int64_t x = 0; x < width; ++x)
for(int64_t y = 0; y < height; ++y)
{
int64_t min_dist(std::numeric_limits<int64_t>::max());
size_t min_index;
for(size_t p = 0; p < points.size(); ++p)
{
int64_t d(distance(Point(x + min_x, y + min_y), points[p]));
if(min_dist > d)
{
min_dist = d;
min_index = p;
}
else if(min_dist == d)
{
min_index = invalid;
}
}
if(invalid_points.find(min_index) == invalid_points.end())
++num_claimed[min_index];
}
std::cout << "Part 1: "
<< *std::max_element(num_claimed.begin(), num_claimed.end())
<< "\n";
int64_t area(0);
constexpr int64_t cutoff(10000);
const int64_t padding(cutoff / points.size() + 1);
const int64_t x_lower(min_x - padding), x_upper(max_x + 1 + padding),
y_lower(min_y - padding), y_upper(max_y + 1 + padding);
for(int64_t x = x_lower; x < x_upper; ++x)
for(int64_t y = y_lower; y < y_upper; ++y)
{
int64_t total_dist(0);
for(auto &point : points)
{
total_dist += distance(Point(x, y), point);
if(total_dist > cutoff)
break;
}
if(total_dist < cutoff)
++area;
}
std::cout << "Part 2: " << area << "\n";
#include <algorithm>
#include <iterator>
#include <iostream>
#include <fstream>
#include <vector>
#include <set>
struct Point
{
int64_t x, y;
Point(const int64_t &X, const int64_t &Y) : x(X), y(Y) {}
Point() = default;
};
int64_t distance(const Point &a, const Point &b)
{
return std::abs(a.x - b.x) + std::abs(a.y - b.y);
}
std::istream &operator>>(std::istream &is, Point &p)
{
char c;
is >> p.x >> c >> p.y;
return is;
}
std::ostream &operator<<(std::ostream &os, Point &p)
{
char c;
os << p.x << ", " << p.y;
return os;
}
size_t min_index(const size_t &invalid, const Point &point,
const std::vector<Point> &points)
{
size_t result;
int64_t min_dist(std::numeric_limits<int64_t>::max());
for(size_t p = 0; p < points.size(); ++p)
{
int64_t d(distance(point, points[p]));
if(min_dist > d)
{
min_dist = d;
result = p;
}
else if(min_dist == d)
{
result = invalid;
}
}
return result;
}
int main(int argc, char *argv[])
{
std::ifstream infile(argv[1]);
std::vector<Point> points(std::istream_iterator<Point>(infile), {});
int64_t min_x(std::numeric_limits<int64_t>::max()), min_y(min_x),
max_x(std::numeric_limits<int64_t>::min()), max_y(max_x);
for(auto &p : points)
{
min_x = std::min(min_x, p.x);
min_y = std::min(min_y, p.y);
max_x = std::max(max_x, p.x);
max_y = std::max(max_y, p.y);
}
int64_t width(max_x - min_x + 1), height(max_y - min_y + 1);
const size_t invalid(points.size());
std::vector<int64_t> num_claimed(points.size() + 1, 0);
std::set<size_t> invalid_points;
for(int64_t x = min_x; x <= max_x; ++x)
{
invalid_points.insert(min_index(invalid, Point(x, min_y), points));
invalid_points.insert(min_index(invalid, Point(x, max_y), points));
}
for(int64_t y = min_y; y <= max_y; ++y)
{
invalid_points.insert(min_index(invalid, Point(min_x, y), points));
invalid_points.insert(min_index(invalid, Point(max_x, y), points));
}
for(int64_t x = 0; x < width; ++x)
for(int64_t y = 0; y < height; ++y)
{
int64_t min_dist(std::numeric_limits<int64_t>::max());
size_t min_index;
for(size_t p = 0; p < points.size(); ++p)
{
int64_t d(distance(Point(x + min_x, y + min_y), points[p]));
if(min_dist > d)
{
min_dist = d;
min_index = p;
}
else if(min_dist == d)
{
min_index = invalid;
}
}
if(invalid_points.find(min_index) == invalid_points.end())
++num_claimed[min_index];
}
std::cout << "Part 1: "
<< *std::max_element(num_claimed.begin(), num_claimed.end())
<< "\n";
int64_t area(0);
constexpr int64_t cutoff(10000);
const int64_t padding(cutoff / points.size() + 1);
const int64_t x_lower(min_x - padding), x_upper(max_x + 1 + padding),
y_lower(min_y - padding), y_upper(max_y + 1 + padding);
for(int64_t x = x_lower; x < x_upper; ++x)
for(int64_t y = y_lower; y < y_upper; ++y)
{
int64_t total_dist(0);
for(auto &point : points)
{
total_dist += distance(Point(x, y), point);
if(total_dist > cutoff)
break;
}
if(total_dist < cutoff)
++area;
}
std::cout << "Part 2: " << area << "\n";
}

View File

@@ -1,32 +1,32 @@
--- Day 1: The Tyranny of the Rocket Equation ---
Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.
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 Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
For example:
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
For a mass of 1969, the fuel required is 654.
For a mass of 100756, the fuel required is 33583.
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
What is the sum of the fuel requirements for all of the modules on your spacecraft?
--- Part Two ---
During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.
Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:
A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)
--- Day 1: The Tyranny of the Rocket Equation ---
Santa has become stranded at the edge of the Solar System while delivering presents to other planets! To accurately calculate his position in space, safely align his warp drive, and return to Earth in time to save Christmas, he needs you to bring him measurements from fifty stars.
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 Elves quickly load you into a spacecraft and prepare to launch.
At the first Go / No Go poll, every Elf is Go until the Fuel Counter-Upper. They haven't determined the amount of fuel required yet.
Fuel required to launch a given module is based on its mass. Specifically, to find the fuel required for a module, take its mass, divide by three, round down, and subtract 2.
For example:
For a mass of 12, divide by 3 and round down to get 4, then subtract 2 to get 2.
For a mass of 14, dividing by 3 and rounding down still yields 4, so the fuel required is also 2.
For a mass of 1969, the fuel required is 654.
For a mass of 100756, the fuel required is 33583.
The Fuel Counter-Upper needs to know the total fuel requirement. To find it, individually calculate the fuel needed for the mass of each module (your puzzle input), then add together all the fuel values.
What is the sum of the fuel requirements for all of the modules on your spacecraft?
--- Part Two ---
During the second Go / No Go poll, the Elf in charge of the Rocket Equation Double-Checker stops the launch sequence. Apparently, you forgot to include additional fuel for the fuel you just added.
Fuel itself requires fuel just like a module - take its mass, divide by three, round down, and subtract 2. However, that fuel also requires fuel, and that fuel requires fuel, and so on. Any mass that would require negative fuel should instead be treated as if it requires zero fuel; the remaining mass, if any, is instead handled by wishing really hard, which has no mass and is outside the scope of this calculation.
So, for each module mass, calculate its fuel and add it to the total. Then, treat the fuel amount you just calculated as the input mass and repeat the process, continuing until a fuel requirement is zero or negative. For example:
A module of mass 14 requires 2 fuel. This fuel requires no further fuel (2 divided by 3 and rounded down is 0, which would call for a negative fuel), so the total fuel required is still just 2.
At first, a module of mass 1969 requires 654 fuel. Then, this fuel requires 216 more fuel (654 / 3 - 2). 216 then requires 70 more fuel, which requires 21 fuel, which requires 5 fuel, which requires no further fuel. So, the total fuel required for a module of mass 1969 is 654 + 216 + 70 + 21 + 5 = 966.
The fuel required by a module of mass 100756 and its fuel is: 33583 + 11192 + 3728 + 1240 + 411 + 135 + 43 + 12 + 2 = 50346.
What is the sum of the fuel requirements for all of the modules on your spacecraft when also taking into account the mass of the added fuel? (Calculate the fuel requirements for each module separately, then add them all up at the end.)

View File

@@ -1,32 +1,32 @@
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
int accumilator = 0;
// for ( std::string line; !infile.eof(); std::getline( infile, line ) )
// {
// if ( line.length() == 0 ) continue;
// accumilator += (std::stoi( line ) / 3) - 2;
// }
std::string line;
while (!infile.eof())
{
std::getline(infile, line);
if ( line.length() == 0 ) continue;
accumilator += (std::stoi( line ) / 3) - 2;
}
std::cout << "Sum of the fuel requrements : " << accumilator << std::endl;
}
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
int accumilator = 0;
// for ( std::string line; !infile.eof(); std::getline( infile, line ) )
// {
// if ( line.length() == 0 ) continue;
// accumilator += (std::stoi( line ) / 3) - 2;
// }
std::string line;
while (!infile.eof())
{
std::getline(infile, line);
if ( line.length() == 0 ) continue;
accumilator += (std::stoi( line ) / 3) - 2;
}
std::cout << "Sum of the fuel requrements : " << accumilator << std::endl;
}

View File

@@ -1,36 +1,36 @@
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
int fuelForMass ( int mass, int accumilator = 0 )
{
int res = (mass / 3) - 2;
if ( res > 0 )
return fuelForMass( res, accumilator + res );
return accumilator;
};
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
int accumilator = 0;
std::string line;
while (!infile.eof())
{
std::getline(infile, line);
if ( line.length() == 0 ) continue;
accumilator += fuelForMass( std::stoi( line ) );
}
std::cout << "Sum of the fuel requrements : " << accumilator << std::endl;
}
#include <string>
#include <iostream>
#include <fstream>
#include <cmath>
int fuelForMass ( int mass, int accumilator = 0 )
{
int res = (mass / 3) - 2;
if ( res > 0 )
return fuelForMass( res, accumilator + res );
return accumilator;
};
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
int accumilator = 0;
std::string line;
while (!infile.eof())
{
std::getline(infile, line);
if ( line.length() == 0 ) continue;
accumilator += fuelForMass( std::stoi( line ) );
}
std::cout << "Sum of the fuel requrements : " << accumilator << std::endl;
}

View File

@@ -1,100 +1,100 @@
54755
96495
111504
53923
118158
118082
137413
135315
87248
127646
79201
52399
77966
129568
63880
128973
55491
111226
126447
87017
112469
83975
51280
60239
120524
57122
136517
117378
93629
55125
68990
70336
115119
68264
148122
70075
106770
54976
123852
61813
113373
53924
59660
67111
52825
81568
110842
134870
135529
78689
129451
96041
91627
70863
100098
121908
96623
143752
149936
116283
149488
126158
106499
124927
109574
70711
139078
67212
124251
123803
73569
145668
96045
59748
123238
68005
121412
97236
104800
86786
141680
123807
82310
76593
146092
82637
92339
93821
56247
58328
90159
105700
57317
69011
125544
102372
63797
92127
111207
54755
96495
111504
53923
118158
118082
137413
135315
87248
127646
79201
52399
77966
129568
63880
128973
55491
111226
126447
87017
112469
83975
51280
60239
120524
57122
136517
117378
93629
55125
68990
70336
115119
68264
148122
70075
106770
54976
123852
61813
113373
53924
59660
67111
52825
81568
110842
134870
135529
78689
129451
96041
91627
70863
100098
121908
96623
143752
149936
116283
149488
126158
106499
124927
109574
70711
139078
67212
124251
123803
73569
145668
96045
59748
123238
68005
121412
97236
104800
86786
141680
123807
82310
76593
146092
82637
92339
93821
56247
58328
90159
105700
57317
69011
125544
102372
63797
92127
111207
77596

View File

@@ -1,64 +1,64 @@
--- Day 2: 1202 Program Alarm ---
On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.
You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"
An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.
Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.
For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.
Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.
Once you're done processing an opcode, move to the next one by stepping forward 4 positions.
For example, suppose you have the following program:
1,9,10,3,2,3,11,0,99,30,40,50
For the purposes of illustration, here is the same program split into multiple lines:
1,9,10,3,
2,3,11,0,
99,
30,40,50
The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:
1,9,10,70,
2,3,11,0,
99,
30,40,50
Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:
3500,9,10,70,
2,3,11,0,
99,
30,40,50
Stepping forward 4 more positions arrives at opcode 99, halting the program.
Here are the initial and final states of a few more small programs:
1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
Once you have a working computer, the first step is to restore the gravity assist program (your puzzle input) to the "1202 program alarm" state it had just before the last computer caught fire. To do this, before running the program, replace position 1 with the value 12 and replace position 2 with the value 2. What value is left at position 0 after the program halts?
--- Part Two ---
"Good, the new computer seems to be working correctly! Keep it nearby during this mission - you'll probably use it again. Real Intcode computers support many more features than your new one, but we'll let you know what they are as you need them."
"However, your current priority should be to complete your gravity assist around the Moon. For this mission to succeed, we should settle on some terminology for the parts you've already built."
Intcode programs are given as a list of integers; these values are used as the initial state for the computer's memory. When you run an Intcode program, make sure to start by initializing memory to the program's values. A position in memory is called an address (for example, the first value in memory is at "address 0").
Opcodes (like 1, 2, or 99) mark the beginning of an instruction. The values used immediately after an opcode, if any, are called the instruction's parameters. For example, in the instruction 1,2,3,4, 1 is the opcode; 2, 3, and 4 are the parameters. The instruction 99 contains only an opcode and has no parameters.
The address of the current instruction is called the instruction pointer; it starts at 0. After an instruction finishes, the instruction pointer increases by the number of values in the instruction; until you add more instructions to the computer, this is always 4 (1 opcode + 3 parameters) for the add and multiply instructions. (The halt instruction would increase the instruction pointer by 1, but it halts the program instead.)
"With terminology out of the way, we're ready to proceed. To complete the gravity assist, you need to determine what pair of inputs produces the output 19690720."
The inputs should still be provided to the program by replacing the values at addresses 1 and 2, just like before. In this program, the value placed in address 1 is called the noun, and the value placed in address 2 is called the verb. Each of the two input values will be between 0 and 99, inclusive.
Once the program has halted, its output is available at address 0, also just like before. Each time you try a pair of inputs, make sure you first reset the computer's memory to the values in the program (your puzzle input) - in other words, don't reuse memory from a previous attempt.
Find the input noun and verb that cause the program to produce the output 19690720. What is 100 * noun + verb? (For example, if noun=12 and verb=2, the answer would be 1202.)
--- Day 2: 1202 Program Alarm ---
On the way to your gravity assist around the Moon, your ship computer beeps angrily about a "1202 program alarm". On the radio, an Elf is already explaining how to handle the situation: "Don't worry, that's perfectly norma--" The ship computer bursts into flames.
You notify the Elves that the computer's magic smoke seems to have escaped. "That computer ran Intcode programs like the gravity assist program it was working on; surely there are enough spare parts up there to build a new Intcode computer!"
An Intcode program is a list of integers separated by commas (like 1,0,0,3,99). To run one, start by looking at the first integer (called position 0). Here, you will find an opcode - either 1, 2, or 99. The opcode indicates what to do; for example, 99 means that the program is finished and should immediately halt. Encountering an unknown opcode means something went wrong.
Opcode 1 adds together numbers read from two positions and stores the result in a third position. The three integers immediately after the opcode tell you these three positions - the first two indicate the positions from which you should read the input values, and the third indicates the position at which the output should be stored.
For example, if your Intcode computer encounters 1,10,20,30, it should read the values at positions 10 and 20, add those values, and then overwrite the value at position 30 with their sum.
Opcode 2 works exactly like opcode 1, except it multiplies the two inputs instead of adding them. Again, the three integers after the opcode indicate where the inputs and outputs are, not their values.
Once you're done processing an opcode, move to the next one by stepping forward 4 positions.
For example, suppose you have the following program:
1,9,10,3,2,3,11,0,99,30,40,50
For the purposes of illustration, here is the same program split into multiple lines:
1,9,10,3,
2,3,11,0,
99,
30,40,50
The first four integers, 1,9,10,3, are at positions 0, 1, 2, and 3. Together, they represent the first opcode (1, addition), the positions of the two inputs (9 and 10), and the position of the output (3). To handle this opcode, you first need to get the values at the input positions: position 9 contains 30, and position 10 contains 40. Add these numbers together to get 70. Then, store this value at the output position; here, the output position (3) is at position 3, so it overwrites itself. Afterward, the program looks like this:
1,9,10,70,
2,3,11,0,
99,
30,40,50
Step forward 4 positions to reach the next opcode, 2. This opcode works just like the previous, but it multiplies instead of adding. The inputs are at positions 3 and 11; these positions contain 70 and 50 respectively. Multiplying these produces 3500; this is stored at position 0:
3500,9,10,70,
2,3,11,0,
99,
30,40,50
Stepping forward 4 more positions arrives at opcode 99, halting the program.
Here are the initial and final states of a few more small programs:
1,0,0,0,99 becomes 2,0,0,0,99 (1 + 1 = 2).
2,3,0,3,99 becomes 2,3,0,6,99 (3 * 2 = 6).
2,4,4,5,99,0 becomes 2,4,4,5,99,9801 (99 * 99 = 9801).
1,1,1,4,99,5,6,0,99 becomes 30,1,1,4,2,5,6,0,99.
Once you have a working computer, the first step is to restore the gravity assist program (your puzzle input) to the "1202 program alarm" state it had just before the last computer caught fire. To do this, before running the program, replace position 1 with the value 12 and replace position 2 with the value 2. What value is left at position 0 after the program halts?
--- Part Two ---
"Good, the new computer seems to be working correctly! Keep it nearby during this mission - you'll probably use it again. Real Intcode computers support many more features than your new one, but we'll let you know what they are as you need them."
"However, your current priority should be to complete your gravity assist around the Moon. For this mission to succeed, we should settle on some terminology for the parts you've already built."
Intcode programs are given as a list of integers; these values are used as the initial state for the computer's memory. When you run an Intcode program, make sure to start by initializing memory to the program's values. A position in memory is called an address (for example, the first value in memory is at "address 0").
Opcodes (like 1, 2, or 99) mark the beginning of an instruction. The values used immediately after an opcode, if any, are called the instruction's parameters. For example, in the instruction 1,2,3,4, 1 is the opcode; 2, 3, and 4 are the parameters. The instruction 99 contains only an opcode and has no parameters.
The address of the current instruction is called the instruction pointer; it starts at 0. After an instruction finishes, the instruction pointer increases by the number of values in the instruction; until you add more instructions to the computer, this is always 4 (1 opcode + 3 parameters) for the add and multiply instructions. (The halt instruction would increase the instruction pointer by 1, but it halts the program instead.)
"With terminology out of the way, we're ready to proceed. To complete the gravity assist, you need to determine what pair of inputs produces the output 19690720."
The inputs should still be provided to the program by replacing the values at addresses 1 and 2, just like before. In this program, the value placed in address 1 is called the noun, and the value placed in address 2 is called the verb. Each of the two input values will be between 0 and 99, inclusive.
Once the program has halted, its output is available at address 0, also just like before. Each time you try a pair of inputs, make sure you first reset the computer's memory to the values in the program (your puzzle input) - in other words, don't reuse memory from a previous attempt.
Find the input noun and verb that cause the program to produce the output 19690720. What is 100 * noun + verb? (For example, if noun=12 and verb=2, the answer would be 1202.)

View File

@@ -1,79 +1,79 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
std::vector<int> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<int> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) ) {
stream.push_back( std::stoi( s ) );
}
return stream;
}
enum Opcodes
{
Add = 1,
Multiply = 2,
End = 99
};
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<int> program = tokenise( input );
program[1] = 12;
program[2] = 2;
for ( int i = 0; i < program.size(); i += 4 )
{
int opcode = program[i];
std::cout << "Opcode : " << opcode << std::endl;
int operandLocA = program[i + 1];
int operandLocB = program[i + 2];
std::cout << "Operand locations : " << operandLocA << ", " << operandLocB << std::endl;
int operandA = program[operandLocA];
int operandB = program[operandLocB];
int resLoc = program[i + 3];
std::cout << "Result location : " << resLoc << std::endl;
if ( opcode == Add )
program[resLoc] = operandA + operandB;
else if ( opcode == Multiply )
program[resLoc] = operandA * operandB;
else if ( opcode == End )
{
std::cout << "Program end" << std::endl << std::endl;
break;
}
std::cout << std::endl;
}
std::cout << "Program end state : ";
for ( auto& i : program )
std::cout << i << " ";
std::cout << std::endl;
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
std::vector<int> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<int> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) ) {
stream.push_back( std::stoi( s ) );
}
return stream;
}
enum Opcodes
{
Add = 1,
Multiply = 2,
End = 99
};
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<int> program = tokenise( input );
program[1] = 12;
program[2] = 2;
for ( int i = 0; i < program.size(); i += 4 )
{
int opcode = program[i];
std::cout << "Opcode : " << opcode << std::endl;
int operandLocA = program[i + 1];
int operandLocB = program[i + 2];
std::cout << "Operand locations : " << operandLocA << ", " << operandLocB << std::endl;
int operandA = program[operandLocA];
int operandB = program[operandLocB];
int resLoc = program[i + 3];
std::cout << "Result location : " << resLoc << std::endl;
if ( opcode == Add )
program[resLoc] = operandA + operandB;
else if ( opcode == Multiply )
program[resLoc] = operandA * operandB;
else if ( opcode == End )
{
std::cout << "Program end" << std::endl << std::endl;
break;
}
std::cout << std::endl;
}
std::cout << "Program end state : ";
for ( auto& i : program )
std::cout << i << " ";
std::cout << std::endl;
}

View File

@@ -1,81 +1,81 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
std::vector<int> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<int> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) )
{
stream.push_back( std::stoi( s ) );
}
return stream;
}
enum Opcodes
{
Add = 1,
Multiply = 2,
End = 99
};
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<int> program;
for ( int noun = 0; noun < 100; noun++ )
for ( int verb = 0; verb < 100; verb++ )
{
program = tokenise( input );
// Noun
program[1] = noun;
// Verb
program[2] = verb;
for ( int i = 0; i < program.size(); i += 4 )
{
int opcode = program[i];
int operandA = program[program[i + 1]];
int operandB = program[program[i + 2]];
int resLoc = program[i + 3];
if ( opcode == Add )
program[resLoc] = operandA + operandB;
else if ( opcode == Multiply )
program[resLoc] = operandA * operandB;
else if ( opcode == End )
break;
}
if ( program[0] == 19690720 )
{
std::cout << "Solution found : " << noun << " " << verb << std::endl;
std::cout << "Solution answer : " << 100 * noun + verb << std::endl;
}
}
std::cout << std::endl << "Program end state : ";
for ( auto& i : program )
std::cout << i << " ";
std::cout << std::endl;
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
std::vector<int> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<int> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) )
{
stream.push_back( std::stoi( s ) );
}
return stream;
}
enum Opcodes
{
Add = 1,
Multiply = 2,
End = 99
};
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<int> program;
for ( int noun = 0; noun < 100; noun++ )
for ( int verb = 0; verb < 100; verb++ )
{
program = tokenise( input );
// Noun
program[1] = noun;
// Verb
program[2] = verb;
for ( int i = 0; i < program.size(); i += 4 )
{
int opcode = program[i];
int operandA = program[program[i + 1]];
int operandB = program[program[i + 2]];
int resLoc = program[i + 3];
if ( opcode == Add )
program[resLoc] = operandA + operandB;
else if ( opcode == Multiply )
program[resLoc] = operandA * operandB;
else if ( opcode == End )
break;
}
if ( program[0] == 19690720 )
{
std::cout << "Solution found : " << noun << " " << verb << std::endl;
std::cout << "Solution answer : " << 100 * noun + verb << std::endl;
}
}
std::cout << std::endl << "Program end state : ";
for ( auto& i : program )
std::cout << i << " ";
std::cout << std::endl;
}

View File

@@ -1,70 +1,70 @@
--- Day 3: Crossed Wires ---
The gravity assist was successful, and you're well on your way to the Venus refuelling station. During the rush back on Earth, the fuel management system wasn't completely installed, so that's next on the priority list.
Opening the front panel reveals a jumble of wires. Specifically, two wires are connected to a central port and extend outward on a grid. You trace the path each wire takes as it leaves the central port, one wire per line of text (your puzzle input).
The wires twist and turn, but the two wires occasionally cross paths. To fix the circuit, you need to find the intersection point closest to the central port. Because the wires are on a grid, use the Manhattan distance for this measurement. While the wires do technically cross right at the central port where they both start, this point does not count, nor does a wire count as crossing with itself.
For example, if the first wire's path is R8,U5,L5,D3, then starting from the central port (o), it goes right 8, up 5, left 5, and finally down 3:
...........
...........
...........
....+----+.
....|....|.
....|....|.
....|....|.
.........|.
.o-------+.
...........
Then, if the second wire's path is U7,R6,D4,L4, it goes up 7, right 6, down 4, and left 4:
...........
.+-----+...
.|.....|...
.|..+--X-+.
.|..|..|.|.
.|.-X--+.|.
.|..|....|.
.|.......|.
.o-------+.
...........
These wires cross at two locations (marked X), but the lower-left one is closer to the central port: its distance is 3 + 3 = 6.
Here are a few more examples:
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83 = distance 159
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = distance 135
What is the Manhattan distance from the central port to the closest intersection?
--- Part Two ---
It turns out that this circuit is very timing-sensitive; you actually need to minimize the signal delay.
To do this, calculate the number of steps each wire takes to reach each intersection; choose the intersection where the sum of both wires' steps is lowest. If a wire visits a position on the grid multiple times, use the steps value from the first time it visits that position when calculating the total value of a specific intersection.
The number of steps a wire takes is the total number of grid squares the wire has entered to get to that location, including the intersection being considered. Again consider the example from above:
...........
.+-----+...
.|.....|...
.|..+--X-+.
.|..|..|.|.
.|.-X--+.|.
.|..|....|.
.|.......|.
.o-------+.
...........
In the above example, the intersection closest to the central port is reached after 8+5+5+2 = 20 steps by the first wire and 7+6+4+3 = 20 steps by the second wire for a total of 20+20 = 40 steps.
However, the top-right intersection is better: the first wire takes only 8+5+2 = 15 and the second wire takes only 7+6+2 = 15, a total of 15+15 = 30 steps.
Here are the best steps for the extra examples from above:
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83 = 610 steps
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = 410 steps
What is the fewest combined steps the wires must take to reach an intersection?
--- Day 3: Crossed Wires ---
The gravity assist was successful, and you're well on your way to the Venus refuelling station. During the rush back on Earth, the fuel management system wasn't completely installed, so that's next on the priority list.
Opening the front panel reveals a jumble of wires. Specifically, two wires are connected to a central port and extend outward on a grid. You trace the path each wire takes as it leaves the central port, one wire per line of text (your puzzle input).
The wires twist and turn, but the two wires occasionally cross paths. To fix the circuit, you need to find the intersection point closest to the central port. Because the wires are on a grid, use the Manhattan distance for this measurement. While the wires do technically cross right at the central port where they both start, this point does not count, nor does a wire count as crossing with itself.
For example, if the first wire's path is R8,U5,L5,D3, then starting from the central port (o), it goes right 8, up 5, left 5, and finally down 3:
...........
...........
...........
....+----+.
....|....|.
....|....|.
....|....|.
.........|.
.o-------+.
...........
Then, if the second wire's path is U7,R6,D4,L4, it goes up 7, right 6, down 4, and left 4:
...........
.+-----+...
.|.....|...
.|..+--X-+.
.|..|..|.|.
.|.-X--+.|.
.|..|....|.
.|.......|.
.o-------+.
...........
These wires cross at two locations (marked X), but the lower-left one is closer to the central port: its distance is 3 + 3 = 6.
Here are a few more examples:
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83 = distance 159
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = distance 135
What is the Manhattan distance from the central port to the closest intersection?
--- Part Two ---
It turns out that this circuit is very timing-sensitive; you actually need to minimize the signal delay.
To do this, calculate the number of steps each wire takes to reach each intersection; choose the intersection where the sum of both wires' steps is lowest. If a wire visits a position on the grid multiple times, use the steps value from the first time it visits that position when calculating the total value of a specific intersection.
The number of steps a wire takes is the total number of grid squares the wire has entered to get to that location, including the intersection being considered. Again consider the example from above:
...........
.+-----+...
.|.....|...
.|..+--X-+.
.|..|..|.|.
.|.-X--+.|.
.|..|....|.
.|.......|.
.o-------+.
...........
In the above example, the intersection closest to the central port is reached after 8+5+5+2 = 20 steps by the first wire and 7+6+4+3 = 20 steps by the second wire for a total of 20+20 = 40 steps.
However, the top-right intersection is better: the first wire takes only 8+5+2 = 15 and the second wire takes only 7+6+2 = 15, a total of 15+15 = 30 steps.
Here are the best steps for the extra examples from above:
R75,D30,R83,U83,L12,D49,R71,U7,L72
U62,R66,U55,R34,D71,R55,D58,R83 = 610 steps
R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51
U98,R91,D20,R16,D67,R40,U7,R15,U6,R7 = 410 steps
What is the fewest combined steps the wires must take to reach an intersection?

View File

@@ -1,154 +1,154 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
std::vector<std::string> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<std::string> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) ) {
stream.push_back( s );
}
return stream;
}
struct vec2
{
int x;
int y;
bool operator==(const vec2& v) const
{
return x == v.x && y == v.y;
}
};
namespace std
{
template <>
struct hash<vec2>
{
size_t operator()( const vec2& v ) const
{
return v.x ^ v.y;
}
};
}
struct Wire
{
vec2 Head = { 0, 0 };
vec2 Origin = { 0, 0 };
std::vector<vec2> Visited;
};
void step( std::vector<vec2>& path, vec2& pos, int steps, char dir )
{
auto up = []( vec2& p ) { p.y -= 1; };
auto down = []( vec2& p ) { p.y += 1; };
auto left = []( vec2& p ) { p.x -= 1; };
auto right = []( vec2& p ) { p.x += 1; };
void (*stepSingle)(vec2&);
switch ( dir )
{
case 'U': stepSingle = up; break;
case 'D': stepSingle = down; break;
case 'R': stepSingle = right; break;
case 'L': stepSingle = left; break;
default: break;
}
for( int i = 0; i < steps; i++ )
{
stepSingle( pos );
path.push_back( pos );
}
}
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<std::string> wireS1 = tokenise( input );
std::getline( infile, input );
std::vector<std::string> wireS2 = tokenise( input );
// Construct map of nodes
// for each point a wire
// lays on, one is added
// to the node
// Wires are assumed to start at 0,0
Wire wire1;
for ( auto& element : wireS1 )
{
// Seperate the direction from the displacement
char direction = element[0];
int displacement = std::stoi( element.substr( 1, element.size() ) );
step(wire1.Visited, wire1.Head, displacement, direction);
std::cout << "Wire 1 " << element << std::endl;
}
Wire wire2;
for ( auto& element : wireS2 )
{
// Seperate the direction from the displacement
char direction = element[0];
int displacement = std::stoi( element.substr( 1, element.size() ) );
step(wire2.Visited, wire2.Head, displacement, direction);
std::cout << "Wire 2 " << element << std::endl;
}
auto find = []( std::vector<vec2> arr, vec2 el ) {
for ( int i = 0; i < arr.size(); i++ )
if ( arr[i] == el ) return i;
return -1;
};
std::vector<int> intersectDistances;
// This takes a good few seconds
for ( int i = 0; i < wire1.Visited.size(); i++ )
{
vec2 v = wire1.Visited[i];
if ( find( wire2.Visited, v ) != -1 )
{
std::cout << "Found : " << v.x << " " << v.y << std::endl;
intersectDistances.push_back( std::abs( v.x ) + std::abs( v.y ) );
}
}
int minDistance = *std::min_element( intersectDistances.cbegin(), intersectDistances.cend() );
std::cout << "Minimum Distance to Intersect : " << minDistance << std::endl;
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
std::vector<std::string> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<std::string> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) ) {
stream.push_back( s );
}
return stream;
}
struct vec2
{
int x;
int y;
bool operator==(const vec2& v) const
{
return x == v.x && y == v.y;
}
};
namespace std
{
template <>
struct hash<vec2>
{
size_t operator()( const vec2& v ) const
{
return v.x ^ v.y;
}
};
}
struct Wire
{
vec2 Head = { 0, 0 };
vec2 Origin = { 0, 0 };
std::vector<vec2> Visited;
};
void step( std::vector<vec2>& path, vec2& pos, int steps, char dir )
{
auto up = []( vec2& p ) { p.y -= 1; };
auto down = []( vec2& p ) { p.y += 1; };
auto left = []( vec2& p ) { p.x -= 1; };
auto right = []( vec2& p ) { p.x += 1; };
void (*stepSingle)(vec2&);
switch ( dir )
{
case 'U': stepSingle = up; break;
case 'D': stepSingle = down; break;
case 'R': stepSingle = right; break;
case 'L': stepSingle = left; break;
default: break;
}
for( int i = 0; i < steps; i++ )
{
stepSingle( pos );
path.push_back( pos );
}
}
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<std::string> wireS1 = tokenise( input );
std::getline( infile, input );
std::vector<std::string> wireS2 = tokenise( input );
// Construct map of nodes
// for each point a wire
// lays on, one is added
// to the node
// Wires are assumed to start at 0,0
Wire wire1;
for ( auto& element : wireS1 )
{
// Seperate the direction from the displacement
char direction = element[0];
int displacement = std::stoi( element.substr( 1, element.size() ) );
step(wire1.Visited, wire1.Head, displacement, direction);
std::cout << "Wire 1 " << element << std::endl;
}
Wire wire2;
for ( auto& element : wireS2 )
{
// Seperate the direction from the displacement
char direction = element[0];
int displacement = std::stoi( element.substr( 1, element.size() ) );
step(wire2.Visited, wire2.Head, displacement, direction);
std::cout << "Wire 2 " << element << std::endl;
}
auto find = []( std::vector<vec2> arr, vec2 el ) {
for ( int i = 0; i < arr.size(); i++ )
if ( arr[i] == el ) return i;
return -1;
};
std::vector<int> intersectDistances;
// This takes a good few seconds
for ( int i = 0; i < wire1.Visited.size(); i++ )
{
vec2 v = wire1.Visited[i];
if ( find( wire2.Visited, v ) != -1 )
{
std::cout << "Found : " << v.x << " " << v.y << std::endl;
intersectDistances.push_back( std::abs( v.x ) + std::abs( v.y ) );
}
}
int minDistance = *std::min_element( intersectDistances.cbegin(), intersectDistances.cend() );
std::cout << "Minimum Distance to Intersect : " << minDistance << std::endl;
}

View File

@@ -1,116 +1,116 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <math.h>
std::vector<std::string> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<std::string> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) ) {
stream.push_back( s );
}
return stream;
}
struct vec2
{
int x;
int y;
bool operator==(const vec2& v) const
{
return x == v.x && y == v.y;
}
};
namespace std
{
template <>
struct hash<vec2>
{
size_t operator()( const vec2& v ) const
{
return v.x ^ v.y;
}
};
}
std::unordered_map<vec2, int> genPath( std::vector<std::string> wire )
{
std::unordered_map<vec2, int> path;
vec2 pos = { 0, 0 };
path[pos] = 0;
auto up = []( vec2& p ) { p.y -= 1; };
auto down = []( vec2& p ) { p.y += 1; };
auto left = []( vec2& p ) { p.x -= 1; };
auto right = []( vec2& p ) { p.x += 1; };
void (*stepSingle)(vec2&);
int stepCounter = 0;
for ( auto& element : wire )
{
char dir = element[0];
int steps = std::stoi( element.substr( 1, element.size() ) );
switch ( dir )
{
case 'U': stepSingle = up; break;
case 'D': stepSingle = down; break;
case 'R': stepSingle = right; break;
case 'L': stepSingle = left; break;
default: break;
}
for( int i = 0; i < steps; i++ )
{
stepSingle( pos );
path[pos] = ++stepCounter;
}
}
return path;
}
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<std::string> wireS1 = tokenise( input );
std::getline( infile, input );
std::vector<std::string> wireS2 = tokenise( input );
auto path1 = genPath(wireS1);
auto path2 = genPath(wireS2);
int res = INFINITY;
for ( auto [pos, steps] : path2 )
{
// Intersect
if ( path1.count( pos ) > 0 )
{
std::cout << pos.x << ":" << pos.y << " " << steps << std::endl;
if ( steps == 0 ) continue;
res = std::min( res, path1[pos] + path2[pos] );
}
}
std::cout << "Shortest Distance to Complete : " << res << std::endl;
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
#include <math.h>
std::vector<std::string> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<std::string> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) ) {
stream.push_back( s );
}
return stream;
}
struct vec2
{
int x;
int y;
bool operator==(const vec2& v) const
{
return x == v.x && y == v.y;
}
};
namespace std
{
template <>
struct hash<vec2>
{
size_t operator()( const vec2& v ) const
{
return v.x ^ v.y;
}
};
}
std::unordered_map<vec2, int> genPath( std::vector<std::string> wire )
{
std::unordered_map<vec2, int> path;
vec2 pos = { 0, 0 };
path[pos] = 0;
auto up = []( vec2& p ) { p.y -= 1; };
auto down = []( vec2& p ) { p.y += 1; };
auto left = []( vec2& p ) { p.x -= 1; };
auto right = []( vec2& p ) { p.x += 1; };
void (*stepSingle)(vec2&);
int stepCounter = 0;
for ( auto& element : wire )
{
char dir = element[0];
int steps = std::stoi( element.substr( 1, element.size() ) );
switch ( dir )
{
case 'U': stepSingle = up; break;
case 'D': stepSingle = down; break;
case 'R': stepSingle = right; break;
case 'L': stepSingle = left; break;
default: break;
}
for( int i = 0; i < steps; i++ )
{
stepSingle( pos );
path[pos] = ++stepCounter;
}
}
return path;
}
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<std::string> wireS1 = tokenise( input );
std::getline( infile, input );
std::vector<std::string> wireS2 = tokenise( input );
auto path1 = genPath(wireS1);
auto path2 = genPath(wireS2);
int res = INFINITY;
for ( auto [pos, steps] : path2 )
{
// Intersect
if ( path1.count( pos ) > 0 )
{
std::cout << pos.x << ":" << pos.y << " " << steps << std::endl;
if ( steps == 0 ) continue;
res = std::min( res, path1[pos] + path2[pos] );
}
}
std::cout << "Shortest Distance to Complete : " << res << std::endl;
}

View File

@@ -1,2 +1,2 @@
R992,U284,L447,D597,R888,D327,R949,U520,R27,U555,L144,D284,R538,U249,R323,U297,R136,U838,L704,D621,R488,U856,R301,U539,L701,U363,R611,D94,L734,D560,L414,U890,R236,D699,L384,D452,R702,D637,L164,U410,R649,U901,L910,D595,R339,D346,R959,U777,R218,D667,R534,D762,R484,D914,L25,U959,R984,D922,R612,U999,L169,D599,L604,D357,L217,D327,L730,D949,L565,D332,L114,D512,R460,D495,L187,D697,R313,U319,L8,D915,L518,D513,R738,U9,R137,U542,L188,U440,R576,D307,R734,U58,R285,D401,R166,U156,L859,U132,L10,U753,L933,U915,R459,D50,R231,D166,L253,U844,R585,D871,L799,U53,R785,U336,R622,D108,R555,D918,L217,D668,L220,U738,L997,D998,R964,D456,L54,U930,R985,D244,L613,D116,L994,D20,R949,D245,L704,D564,L210,D13,R998,U951,L482,U579,L793,U680,L285,U770,L975,D54,R79,U613,L907,U467,L256,D783,R883,U810,R409,D508,L898,D286,L40,U741,L759,D549,R210,U411,R638,D643,L784,U538,L739,U771,L773,U491,L303,D425,L891,U182,R412,U951,L381,U501,R482,D625,R870,D320,L464,U555,R566,D781,L540,D754,L211,U73,L321,D869,R994,D177,R496,U383,R911,U819,L651,D774,L591,U666,L883,U767,R232,U822,L499,U44,L45,U873,L98,D487,L47,U803,R855,U256,R567,D88,R138,D678,L37,U38,R783,U569,L646,D261,L597,U275,L527,U48,R433,D324,L631,D160,L145,D128,R894,U223,R664,U510,R756,D700,R297,D361,R837,U996,L769,U813,L477,U420,L172,U482,R891,D379,L329,U55,R284,U155,L816,U659,L671,U996,R997,U252,R514,D718,L661,D625,R910,D960,L39,U610,R853,U859,R174,U215,L603,U745,L587,D736,R365,U78,R306,U158,L813,U885,R558,U631,L110,D232,L519,D366,R909,D10,R294
R992,U284,L447,D597,R888,D327,R949,U520,R27,U555,L144,D284,R538,U249,R323,U297,R136,U838,L704,D621,R488,U856,R301,U539,L701,U363,R611,D94,L734,D560,L414,U890,R236,D699,L384,D452,R702,D637,L164,U410,R649,U901,L910,D595,R339,D346,R959,U777,R218,D667,R534,D762,R484,D914,L25,U959,R984,D922,R612,U999,L169,D599,L604,D357,L217,D327,L730,D949,L565,D332,L114,D512,R460,D495,L187,D697,R313,U319,L8,D915,L518,D513,R738,U9,R137,U542,L188,U440,R576,D307,R734,U58,R285,D401,R166,U156,L859,U132,L10,U753,L933,U915,R459,D50,R231,D166,L253,U844,R585,D871,L799,U53,R785,U336,R622,D108,R555,D918,L217,D668,L220,U738,L997,D998,R964,D456,L54,U930,R985,D244,L613,D116,L994,D20,R949,D245,L704,D564,L210,D13,R998,U951,L482,U579,L793,U680,L285,U770,L975,D54,R79,U613,L907,U467,L256,D783,R883,U810,R409,D508,L898,D286,L40,U741,L759,D549,R210,U411,R638,D643,L784,U538,L739,U771,L773,U491,L303,D425,L891,U182,R412,U951,L381,U501,R482,D625,R870,D320,L464,U555,R566,D781,L540,D754,L211,U73,L321,D869,R994,D177,R496,U383,R911,U819,L651,D774,L591,U666,L883,U767,R232,U822,L499,U44,L45,U873,L98,D487,L47,U803,R855,U256,R567,D88,R138,D678,L37,U38,R783,U569,L646,D261,L597,U275,L527,U48,R433,D324,L631,D160,L145,D128,R894,U223,R664,U510,R756,D700,R297,D361,R837,U996,L769,U813,L477,U420,L172,U482,R891,D379,L329,U55,R284,U155,L816,U659,L671,U996,R997,U252,R514,D718,L661,D625,R910,D960,L39,U610,R853,U859,R174,U215,L603,U745,L587,D736,R365,U78,R306,U158,L813,U885,R558,U631,L110,D232,L519,D366,R909,D10,R294
L1001,D833,L855,D123,R36,U295,L319,D700,L164,U576,L68,D757,R192,D738,L640,D660,R940,D778,R888,U772,R771,U900,L188,D464,L572,U184,R889,D991,L961,U751,R560,D490,L887,D748,R37,U910,L424,D401,L385,U415,L929,U193,R710,D855,L596,D323,L966,D505,L422,D139,L108,D135,R737,U176,R538,D173,R21,D951,R949,D61,L343,U704,R127,U468,L240,D834,L858,D127,R328,D863,R329,U477,R131,U864,R997,D38,R418,U611,R28,U705,R148,D414,R786,U264,L785,D650,R201,D250,R528,D910,R670,U309,L658,U190,R704,U21,R288,D7,R930,U62,R782,U621,R328,D725,R305,U700,R494,D137,R969,U142,L867,U577,R300,U162,L13,D698,R333,U865,R941,U796,L60,U902,L784,U832,R78,D578,R196,D390,R728,D922,R858,D994,L457,U547,R238,D345,R329,D498,R873,D212,R501,U474,L657,U910,L335,U133,R213,U417,R698,U829,L2,U704,L273,D83,R231,D247,R675,D23,L692,D472,L325,D659,L408,U746,L715,U395,L596,U296,R52,D849,L713,U815,R684,D551,L319,U768,R176,D182,R557,U731,R314,D543,L9,D256,R38,D809,L567,D332,R375,D572,R81,D479,L71,U968,L831,D247,R989,U390,R463,D576,R740,D539,R488,U367,L596,U375,L763,D824,R70,U448,R979,D977,L744,D379,R488,D671,L516,D334,L542,U517,L488,D390,L713,D932,L28,U924,L448,D229,L488,D501,R19,D910,L979,D411,R711,D824,L973,U291,R794,D485,R208,U370,R655,U450,L40,D804,L374,D671,R962,D829,L209,U111,L84,D876,L832,D747,L733,D560,L702,D972,R188,U817,L111,U26,L492,U485,L71,D59,L269,D870,L152,U539,R65,D918,L932,D260,L485,U77,L699,U254,R924,U643,L264,U96,R395,D917,R360,U354,R101,D682,R854,U450,L376,D378,R872,D311,L881,U630,R77,D766,R672

View File

@@ -1,25 +1,25 @@
--- Day 4: Secure Container ---
You arrive at the Venus fuel depot only to discover it's protected by a password. The Elves had written the password on a sticky note, but someone threw it out.
However, they do remember a few key facts about the password:
It is a six-digit number.
The value is within the range given in your puzzle input.
Two adjacent digits are the same (like 22 in 122345).
Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
Other than the range rule, the following are true:
111111 meets these criteria (double 11, never decreases).
223450 does not meet these criteria (decreasing pair of digits 50).
123789 does not meet these criteria (no double).
How many different passwords within the range given in your puzzle input meet these criteria?
--- Part Two ---
An Elf just remembered one more important detail: the two adjacent matching digits are not part of a larger group of matching digits.
Given this additional criterion, but still ignoring the range rule, the following are now true:
112233 meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
123444 no longer meets the criteria (the repeated 44 is part of a larger group of 444).
111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
How many different passwords within the range given in your puzzle input meet all of the criteria?
--- Day 4: Secure Container ---
You arrive at the Venus fuel depot only to discover it's protected by a password. The Elves had written the password on a sticky note, but someone threw it out.
However, they do remember a few key facts about the password:
It is a six-digit number.
The value is within the range given in your puzzle input.
Two adjacent digits are the same (like 22 in 122345).
Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
Other than the range rule, the following are true:
111111 meets these criteria (double 11, never decreases).
223450 does not meet these criteria (decreasing pair of digits 50).
123789 does not meet these criteria (no double).
How many different passwords within the range given in your puzzle input meet these criteria?
--- Part Two ---
An Elf just remembered one more important detail: the two adjacent matching digits are not part of a larger group of matching digits.
Given this additional criterion, but still ignoring the range rule, the following are now true:
112233 meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
123444 no longer meets the criteria (the repeated 44 is part of a larger group of 444).
111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
How many different passwords within the range given in your puzzle input meet all of the criteria?

View File

@@ -1,45 +1,45 @@
#include <iostream>
#include <string>
bool checkPassword( std::string pass )
{
// Is less than 6
if ( pass.length() > 6 || pass.length() < 6 ) return false;
// Have any repeats
bool repeats = false;
char lastr = pass[0];
for ( int i = 1; i < pass.length(); i++ )
{
if ( lastr == pass[i] ) repeats = true;
lastr = pass[i];
}
if ( !repeats ) return false;
// Digets never decrease
bool decreases = true;
for ( int i = 1; i < pass.length(); i++ )
if ( pass[i - 1] > pass[i] ) decreases = false;
if ( !decreases ) return false;
return true;
}
int main(int argc, char** argv)
{
int upper = 893698;
int lower = 367479;
int accumilator = 0;
for ( int i = lower; i < upper; i++ )
if ( checkPassword( std::to_string( i ) ) )
accumilator++;
std::cout << "There are : " << accumilator << " valid passwords" << std::endl;
}
#include <iostream>
#include <string>
bool checkPassword( std::string pass )
{
// Is less than 6
if ( pass.length() > 6 || pass.length() < 6 ) return false;
// Have any repeats
bool repeats = false;
char lastr = pass[0];
for ( int i = 1; i < pass.length(); i++ )
{
if ( lastr == pass[i] ) repeats = true;
lastr = pass[i];
}
if ( !repeats ) return false;
// Digets never decrease
bool decreases = true;
for ( int i = 1; i < pass.length(); i++ )
if ( pass[i - 1] > pass[i] ) decreases = false;
if ( !decreases ) return false;
return true;
}
int main(int argc, char** argv)
{
int upper = 893698;
int lower = 367479;
int accumilator = 0;
for ( int i = lower; i < upper; i++ )
if ( checkPassword( std::to_string( i ) ) )
accumilator++;
std::cout << "There are : " << accumilator << " valid passwords" << std::endl;
}

View File

@@ -1,44 +1,44 @@
#include <algorithm>
#include <iostream>
#include <string>
bool checkPassword( std::string pass )
{
// Is less than 6
if ( pass.length() > 6 || pass.length() < 6 ) return false;
// Digets never decrease
bool decreases = true;
for ( int i = 1; i < pass.length(); i++ )
if ( pass[i - 1] > pass[i] ) decreases = false;
if ( !decreases ) return false;
// No repeats ( conform w part 2 rule )
if ( std::any_of( pass.begin(), pass.end(),
[&]( auto d ) {
return std::count( pass.begin(), pass.end(), d ) == 2;
} ) )
{
return true;
}
return false;
}
int main(int argc, char** argv)
{
int upper = 893698;
int lower = 367479;
int accumilator = 0;
for ( int i = lower; i < upper; i++ )
if ( checkPassword( std::to_string( i ) ) )
accumilator++;
std::cout << "There are : " << accumilator << " valid passwords" << std::endl;
}
#include <algorithm>
#include <iostream>
#include <string>
bool checkPassword( std::string pass )
{
// Is less than 6
if ( pass.length() > 6 || pass.length() < 6 ) return false;
// Digets never decrease
bool decreases = true;
for ( int i = 1; i < pass.length(); i++ )
if ( pass[i - 1] > pass[i] ) decreases = false;
if ( !decreases ) return false;
// No repeats ( conform w part 2 rule )
if ( std::any_of( pass.begin(), pass.end(),
[&]( auto d ) {
return std::count( pass.begin(), pass.end(), d ) == 2;
} ) )
{
return true;
}
return false;
}
int main(int argc, char** argv)
{
int upper = 893698;
int lower = 367479;
int accumilator = 0;
for ( int i = lower; i < upper; i++ )
if ( checkPassword( std::to_string( i ) ) )
accumilator++;
std::cout << "There are : " << accumilator << " valid passwords" << std::endl;
}

View File

@@ -1,47 +1,47 @@
--- Day 5: Sunny with a Chance of Asteroids ---
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.
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:
First, you'll need to add two new instructions:
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.
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
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.
Second, you'll need to add support for parameter modes:
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 memory. Until now, all parameters have been in position mode.
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.
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.
For example, consider the program 1002,4,3,4,33.
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):
ABCDE
1002
DE - two-digit opcode, 02 == opcode 2
C - mode of 1st parameter, 0 == position mode
B - mode of 2nd parameter, 1 == immediate mode
A - mode of 3rd parameter, 0 == position mode,
omitted due to being a leading zero
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.
Parameters that an instruction writes to will never be in immediate mode.
Finally, some notes:
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.
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
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.
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.
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.
After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?
--- Day 5: Sunny with a Chance of Asteroids ---
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.
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:
First, you'll need to add two new instructions:
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.
Opcode 4 outputs the value of its only parameter. For example, the instruction 4,50 would output the value at address 50.
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.
Second, you'll need to add support for parameter modes:
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 memory. Until now, all parameters have been in position mode.
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.
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.
For example, consider the program 1002,4,3,4,33.
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):
ABCDE
1002
DE - two-digit opcode, 02 == opcode 2
C - mode of 1st parameter, 0 == position mode
B - mode of 2nd parameter, 1 == immediate mode
A - mode of 3rd parameter, 0 == position mode,
omitted due to being a leading zero
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.
Parameters that an instruction writes to will never be in immediate mode.
Finally, some notes:
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.
Integers can be negative: 1101,100,-1,4,0 is a valid program (find 100 + -1, store the result in position 4).
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.
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.
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.
After providing 1 to the only input instruction and passing all the tests, what diagnostic code does the program produce?

View File

@@ -1,53 +1,53 @@
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
std::vector<int> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<int> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) ) {
stream.push_back( std::stoi( s ) );
}
return stream;
}
class IntCodeCPU
{
public:
IntCodeCPU();
std::vector<int> Program;
int ProgramCounter;
void SetProgram( std::vector<int> program )
{
Program = program;
}
void RegisterOpcode( int opcode )
{
}
};
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<int> program = tokenise( input );
}
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
std::vector<int> tokenise( std::string input )
{
std::stringstream ssInput( input );
std::vector<int> stream;
std::string s;
while( std::getline( ssInput, s, ',' ) ) {
stream.push_back( std::stoi( s ) );
}
return stream;
}
class IntCodeCPU
{
public:
IntCodeCPU();
std::vector<int> Program;
int ProgramCounter;
void SetProgram( std::vector<int> program )
{
Program = program;
}
void RegisterOpcode( int opcode )
{
}
};
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::string input;
std::getline( infile, input );
std::vector<int> program = tokenise( input );
}

View File

@@ -1,51 +1,51 @@
--- Day 6: Universal Orbit Map ---
You've landed at the Universal Orbit Map facility on Mercury. Because navigation in space often involves transferring between orbits, the orbit maps here are useful for finding efficient routes between, for example, you and Santa. You download a map of the local orbits (your puzzle input).
Except for the universal Center of Mass (COM), every object in space is in orbit around exactly one other object. An orbit looks roughly like this:
\
\
|
|
AAA--> o o <--BBB
|
|
/
/
In this diagram, the object BBB is in orbit around AAA. The path that BBB takes around AAA (drawn with lines) is only partly shown. In the map data, this orbital relationship is written AAA)BBB, which means "BBB is in orbit around AAA".
Before you use your map data to plot a course, you need to make sure it wasn't corrupted during the download. To verify maps, the Universal Orbit Map facility uses orbit count checksums - the total number of direct orbits (like the one shown above) and indirect orbits.
Whenever A orbits B and B orbits C, then A indirectly orbits C. This chain can be any number of objects long: if A orbits B, B orbits C, and C orbits D, then A indirectly orbits D.
For example, suppose you have the following map:
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
Visually, the above map of orbits looks like this:
G - H J - K - L
/ /
COM - B - C - D - E - F
\
I
In this visual representation, when two objects are connected by a line, the one on the right directly orbits the one on the left.
Here, we can count the total number of orbits as follows:
D directly orbits C and indirectly orbits B and COM, a total of 3 orbits.
L directly orbits K and indirectly orbits J, E, D, C, B, and COM, a total of 7 orbits.
COM orbits nothing.
The total number of direct and indirect orbits in this example is 42.
What is the total number of direct and indirect orbits in your map data?
--- Day 6: Universal Orbit Map ---
You've landed at the Universal Orbit Map facility on Mercury. Because navigation in space often involves transferring between orbits, the orbit maps here are useful for finding efficient routes between, for example, you and Santa. You download a map of the local orbits (your puzzle input).
Except for the universal Center of Mass (COM), every object in space is in orbit around exactly one other object. An orbit looks roughly like this:
\
\
|
|
AAA--> o o <--BBB
|
|
/
/
In this diagram, the object BBB is in orbit around AAA. The path that BBB takes around AAA (drawn with lines) is only partly shown. In the map data, this orbital relationship is written AAA)BBB, which means "BBB is in orbit around AAA".
Before you use your map data to plot a course, you need to make sure it wasn't corrupted during the download. To verify maps, the Universal Orbit Map facility uses orbit count checksums - the total number of direct orbits (like the one shown above) and indirect orbits.
Whenever A orbits B and B orbits C, then A indirectly orbits C. This chain can be any number of objects long: if A orbits B, B orbits C, and C orbits D, then A indirectly orbits D.
For example, suppose you have the following map:
COM)B
B)C
C)D
D)E
E)F
B)G
G)H
D)I
E)J
J)K
K)L
Visually, the above map of orbits looks like this:
G - H J - K - L
/ /
COM - B - C - D - E - F
\
I
In this visual representation, when two objects are connected by a line, the one on the right directly orbits the one on the left.
Here, we can count the total number of orbits as follows:
D directly orbits C and indirectly orbits B and COM, a total of 3 orbits.
L directly orbits K and indirectly orbits J, E, D, C, B, and COM, a total of 7 orbits.
COM orbits nothing.
The total number of direct and indirect orbits in this example is 42.
What is the total number of direct and indirect orbits in your map data?

View File

@@ -1,56 +1,56 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
std::unordered_map<std::string, std::string> mapise( std::vector<std::string> input )
{
std::unordered_map<std::string, std::string> ret;
for ( auto& str : input )
{
std::string first = str.substr(0, 3);
std::string second = str.substr(4, str.size());
ret[first] = second;
std::cout << first << " " << second << std::endl;
}
return ret;
}
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::vector<std::string> lines;
std::string line;
while (!infile.eof())
{
std::getline(infile, line);
if ( line.length() == 0 ) continue;
lines.push_back(line);
}
int accumilator = 0;
auto map = mapise( lines );
std::unordered_map<std::string, std::string>::iterator it;
for (it = map.begin(); it != map.end(); it++)
{
}
std::cout << "Sum of direct and indirect orbits : " << accumilator << std::endl;
}
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <algorithm>
std::unordered_map<std::string, std::string> mapise( std::vector<std::string> input )
{
std::unordered_map<std::string, std::string> ret;
for ( auto& str : input )
{
std::string first = str.substr(0, 3);
std::string second = str.substr(4, str.size());
ret[first] = second;
std::cout << first << " " << second << std::endl;
}
return ret;
}
int main(int argc, char** argv)
{
std::ifstream infile( argv[1] );
std::vector<std::string> lines;
std::string line;
while (!infile.eof())
{
std::getline(infile, line);
if ( line.length() == 0 ) continue;
lines.push_back(line);
}
int accumilator = 0;
auto map = mapise( lines );
std::unordered_map<std::string, std::string>::iterator it;
for (it = map.begin(); it != map.end(); it++)
{
}
std::cout << "Sum of direct and indirect orbits : " << accumilator << std::endl;
}

File diff suppressed because it is too large Load Diff

30
2020/3.js Normal file
View File

@@ -0,0 +1,30 @@
const fs = require('fs');
const input = fs.readFileSync('3.txt').toString().split('\n');
function countTrees(lines, xoff, yoff)
{
let treeCount = 0;
let length = lines[0].length - 1;
let x = 0;
for (let i = 0; i < lines.length; i += yoff)
{
if (lines[i][x % length] == '#') treeCount++;
x += xoff;
}
return treeCount;
}
let count = countTrees(input, 3, 1);
console.log(`Part 1, tree count: ${count}`);
let total1 = countTrees(input, 1, 1);
let total2 = countTrees(input, 3, 1);
let total3 = countTrees(input, 5, 1);
let total4 = countTrees(input, 7, 1);
let total5 = countTrees(input, 1, 2);
console.log(`Part 2, tree count: ${total1 * total2 * total3 * total4 * total5}`);

323
2020/3.txt Normal file
View File

@@ -0,0 +1,323 @@
.....##.#.....#........#....##.
....#...#...#.#.......#........
.....##.#......#.......#.......
...##.........#...#............
........#...#.......#.........#
..........#......#..#....#....#
..................#..#..#....##
.....##...#..#..#..#..#.##.....
..##.###....#.#.........#......
#.......#......#......#....##..
.....#..#.#.......#......#.....
............#............#.....
...#.#........#........#.#.##.#
.#..#...#.....#....##..........
##..........#...#...#..........
...........#...###...#.......##
.#..#............#........#....
##.#..#.....#.......#.#.#......
.##.....#....#.#.......#.##....
..##...........#.......#..##.#.
##...#.#........#..#...#...#..#
.#..#........#.#.......#..#...#
.##.##.##...#.#............##..
..#.#..###......#..#......#....
.#..#..#.##.#.##.#.#...........
...#....#..#.#.#.........#..#..
......#.#....##.##......#......
#....#.##.##....#..#...........
...#.#.#.#..#.#..#.#..#.##.....
#.....#######.###.##.#.#.#.....
..#.##.....##......#...#.......
..#....#..#...##.#..#..#..#..#.
.............#.##....#.........
.#....#.##.....#...............
.#............#....#...#.##....
.#.....#.##.###.......#..#.....
.#...#.........#.......#..#....
..#.#..#.##.......##...........
.....##..#..#..#..#.##..#.....#
..##............##...#..#......
...#..#....#..##.....##..#.#...
#.....##....#.#.#...#...#..##.#
#.#..#.........#.##.#...#.#.#..
.....#.#....##....#............
#.......#..#.....##..#...#...#.
.....#.#...#...#..#......#.....
..##....#.#.#.#.#..#...........
##..#...#.........#......#...#.
..#...#.#.#.#..#.#.##..##......
#............###.....###.......
..........#...#........###.....
.......##...#...#...#........#.
.#..#.##.#.....................
.#..##........##.##...#.......#
.......##......#.....#......#..
.##.#.....#......#......#......
#...##.#.#...#.#...............
........#..#...#.##.......#....
...................#...#...##..
...#...#.........#.....#..#.#..
.###..#........#..##.##..#.##..
#...#.....#.....#.....#..#..#..
###..#.....#.#.#.#......#....#.
#........#....##.#...##........
.#.#..##........##....##.#.#...
#...#....#.###.#.#.........#...
...#...##..###.......#.........
......#....#..##..#.....#.#....
........#...##...###......##...
..........##.#.......##........
...#....#......#...##.....#....
###.#.....#.#..#..#....#...#..#
.#.....#.#....#...............#
..#....#....####....###....#.#.
....##........#..#.##.#....#...
.......##...#...#..#....####...
#...##.#......##...#..#........
..##..#.##....#.......##.#.#...
..#.#...............#...#.#....
....#.....#.#.....#.##.......#.
...#.#..##.#.#..............##.
..#.....#...#.............#.##.
##..#.#...#........#..#.....##.
...........##...#.#.###...#....
...#.#.#..#..................#.
.#...##.............#...#......
..#..#...#.#.......#...#.....#.
..##.......#.#.................
.##..#........###.....#....#.##
......#..###.......#....##....#
....#.....#.................#..
........#...#...#..............
...#..#.###.......#..#.#.#.##..
..#...#.....#....#.........#...
...#.............#........###..
......#..............#......#..
#..#...........#...#..........#
...##...#.###..#...#.....#.#...
....#..##......#.......##......
....#....##.#...#.#..#....#...#
.#...........#..#....##...#..##
..#.#.................###.#...#
..#.#.#...##...........#.......
..........#..##...#.#..##....##
........#........#.##..#.#...#.
.....#...##.......##......#...#
....#...#..#..#.....#..........
.#..#......#..#..#..###.......#
.##..........#...#...#.#.....##
..#..........#.#.#...###.......
....#................#...##....
.##..#....#..........#.#.#.....
..##...#.#........#.....#.##...
....####.....#..#.........##..#
......#.........#...#..........
....#...................#..##..
.##....#.#.........#....#...#..
....##...##.....#..####........
..##.#....#.#.......##...#.....
#...#.#.#...#..#..##.....#.....
#..................###.....#...
#.#.....#.......#.#...###.#....
.#..#....#............#........
#.#....#..#.#...............#..
..#..#..#.............#......#.
..#.......##...................
.#....#.........#....#.#.#..#..
....#....#..#...............#..
......#..#..##......#.........#
..#.##........##......#..#..#.#
#.....#.#....#.........##...#..
###..............#....###...##.
....#..##......#.......##......
......#...#.##......##....#..#.
..........#....#..##.......#..#
.#..#...##..#...........#..#..#
.....#....#...#..###...###....#
.#####..#...#.#.#..#.#.###...##
..##............##.#...#.##...#
.##..#...#...#....##.#..#..##..
.#....#...#............##..#...
.#.#......#....#....#..##..##..
.........#...#.......#.##..#...
#.........#.....##.....#..#..#.
...##.#...#...#..#..#....##..##
.#............#...#....##......
..#...#.##.........#.#......#.#
....#.##........#.........#..##
#.........#......#.#......#..#.
........#.#.......#.#........#.
..#..........##.#...#..#.#.....
..#...#....#...#...#..#.#..#.#.
.#.........#....#..#####..#....
#.#....#.#.###...#.............
..##...........##......##......
#.....#..#....#...............#
...#.#..#....##......#...##....
...#........#.....#...#..#.....
.#......##.........#......#....
..#..###.##...#.#.....#........
.............#......#..#.......
..#...............#.#...#..#..#
.......#..#...#.#####......#..#
.........#.....#...............
##........#............#.#.....
.#...#.....#..#..#...#....#...#
..#....#....##......##.....#.#.
#...##..##......#...#....#.....
....#.#.#.....###....##.##....#
..........##...##.......#......
..#.......#...##.#....##.##....
....#........................#.
...#...#.#.##...#.....#...#..#.
.#....##..#..#..........##..##.
.#.....#..#...#.##.....#.......
.#.##...#.#..#.....##....#...#.
.##...#........##....#..#......
.....#........#..........#.#..#
....#..##.......#..#.....#.....
...........#...#........#.##..#
.....#..#....#..#.#.....#....##
.....#....#.##.#..##...........
...##.......##.........#.......
...............##..#....#.#....
.......###..#........#..####.##
.......#.##...#.#....#.####....
....#...............#..........
##.#.......#.....#......#...#..
......##.....#....#.....#..#..#
.....#...##.............#......
#.#.##.#.....#..#........#.....
......##....#..#........#......
............#........#..#.#....
##.......#......#...####..#.##.
..##..#...#.............#.##...
.....#..##......#.##......###..
............#........#........#
#.#.#.#...#.#.....#.........#..
.........#...............#.....
.............###.#.......#....#
###.##..#..#..........#....#...
#......#...#..#..#.....#.##....
............#....#....#..#.....
..#.#....#...#......#.#..#..##.
...#........................#..
#.#...#..........#......#.#....
.........#................#...#
##.....#....#........##.......#
#...##........#...#...........#
...#...#..........##.......#.#.
..#.#.#....#......##...........
...#.#...#.##.#..#.#.##........
#....##.....###..#.......#.....
###.....#.#.#...#..#.........##
..#......#..###...#.#.#.....#.#
.#....#.....#............#..##.
....#....##..........#.....##..
#...........#....#...#..#...##.
..#.......#.....#..........#...
.#..#................#......#..
..#......#.#...#..#.#....#....#
...#..#...###..#..##....#.#....
..#..............#.....#.......
...#.#...#.........#.#.........
##......##...........##.#.##..#
..#..##..#....#.#......#.#...##
...#.###....###...#.....#......
#.#................#......#....
..#.....#.....#....##.......#..
.#.#...............##..#.......
...#....#.......#.#.....##..#..
.........#....#.......#.#...##.
#....#......##.#.........##...#
#.............#..##.#.#..##....
...#....#..#...#....#.#.#.#...#
.#....#....#..##.....#.#...###.
##............#.#...##.#..#.#..
##.#....##.....#..#..###....#..
##....#................##......
...##..#...#..###....#.....##..
.#...##......#..#.#.....#...#..
..##......##...#.##.......#....
......#.....#.....##........#.#
##....#...........#............
#.......#....#..#.##..##.#..#..
.#....##.#.....#..#..#.........
.#....#.#.#...#.....##.....#.#.
.......##.#.#........#......##.
##........#.##.......#...#..#..
...###..##....#.#....#.#.......
......#.......#...##.....#...#.
..#......##.#......#.....#.....
.....#.....###...#.............
#...#.#...#...#..#......#......
#.....#.......###.#....###.#...
...#.......#....####....##..#..
#.#.....#....#........#.......#
.........#.......#......#.#...#
..##....#.....##...............
..........#..#.#..#......#.....
..................##...##.#....
........#.......#...#..#.#.#...
.....#.#..##..#..#.#..#.......#
.....#........#..#..#....#....#
##............#..#..#...#....#.
.....#....................##..#
........##.#....###............
##.......#.##................#.
.....###.#..#..#...#....###.##.
.#......#.#....#.....##.#......
...##......##.........#...#....
....####..............#........
#...#.#..##..##.........##.....
......#......#....#..#.........
#.....#.....#.##...............
..#.##..#...##.#.####..#....###
#..#......#....#.##..##...#.#..
#....#.......#.....#.....#.#...
##.......#.....##...#.....#....
...#...##..........#..##..##..#
.###..#..##...#....#...#..#....
......##..###.......###...#....
....#...#.#.......#.##...##..##
#.#......#..##.#.#..#..#..#....
......#........#.......#.......
..........#.#.....##...........
......#..#........#..#.#..###..
##..#.............##..#........
.........#....#.....#.........#
.....#..##...#..#..##.##......#
###..#...........#.......#....#
...............#....#.#........
.##.#...#.#........##....#.....
.##.###...##..###....#...#...#.
.##..#....#.#.#...#.#.#.#...#..
.###.#...#.......#....#..#.....
..#..#.#.#.#........#.....##...
.#.......#.#...#.#...........##
...#.....##....#.....##...#....
................#.....####...#.
.#.#......#.......##...#.##....
.###.........#.#......#..#.#...
#......#...#....#..##.......#..
.##..#....#..#...........#...#.
.#...#.......##........#.##....
..#...........#...##...........
.....##....##......#....#..#...
#......#.#...#.##.#...##....#..
#....................#...##...#
..#............#........#......
.............#.........##.....#
...#...#......##.#...#...#.#...
..#...#.#.................#....
....##...#....#...###.##......#
...#....#...#..#...#....#.....#
...##.#........#..#.........#..
..##.....#..##...#.....##...#..
#.........#.#.#...#......#...#.
#.#...........#...#..#..#..##..
..#..#..##....#..........#.###.
.....#..#....#.#...#...#..#..#.
###.....#..#.................#.
.#..##.##.#......#....##..#....

15
2020/4.js Normal file
View File

@@ -0,0 +1,15 @@
const fs = require('fs');
const input = fs.readFileSync('4.txt').toString().split('\n');
const requiredFields = ['byr', 'iyr', 'eyr', 'hgt', 'hcl', 'ecl', 'pid'];
let currentPassport = {};
for (const line of input)
{
console.log(input + '\nbruh')
if (line == '')
{
console.log('new passport')
currentPassport = {};
}
}

1133
2020/4.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -1,3 +1,3 @@
# AdventOfCode
A collection of the source code of the programs i wrote to generate the answers for the advent of code challenges.
# AdventOfCode
A collection of the source code of the programs i wrote to generate the answers for the advent of code challenges.