day02 solution

This commit is contained in:
alexchao26
2020-09-06 20:56:52 -04:00
parent 62efd77655
commit 31a8f178be
6 changed files with 438 additions and 1 deletions
+1 -1
View File
@@ -8,4 +8,4 @@ Language: GoLang.
Day | Name | Type of Algo & Notes
--- | --- | ---
1 | Chronal Calibration | - Pretty simple, direct math problem
2 |
2 | Inventory Management System | - Pretty simple, brute force string comparison
+53
View File
@@ -0,0 +1,53 @@
--- 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?
Your puzzle answer was 497.
--- 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?
Your puzzle answer was 558.
Both parts of this puzzle are complete! They provide two gold stars: **
+250
View File
@@ -0,0 +1,250 @@
ayitmcjvlhedbsyoqfzukjpxwt
agirmcjvlheybsyogfzuknpxxt
wgirmcjvlvedbsyoqfzujnpxwt
agizmcjvlhedbsyoqfzuenlxwt
aryrmcjvlheebsyoqfzuknpxwt
agirmcjelhedbsyoqfzukosxwt
azirmcjvlhedbsooqfzuknpxvt
agirmcjvffedbsyoqfzudnpxwt
agilmcjvlhedbsyrqfzuknpxrt
agirmcjvlhndbsyoofzukcpxwt
awirmcjvlhedbsyoqfzuknpxlz
aghrmcjmlhewbsyoqfzuknpxwt
apirmcjvlmedbsyoqfzcknpxwt
jgiricjvlhedbsyrqfzuknpxwt
abirmcjvlbedbsyoqfzuknpxwo
agirmcjvlhedbsyojfzuknpgkt
agicmclvlhedbmyoqfzuknpxwt
aslrzcjvlhedbsyoqfzuknpxwt
agiqmcjvlhedbsymqfzurnpxwt
agirmcjvlwedbsyoqfzuknfxmt
agiumcjvlhedbsyoqfzuknpbyt
xgirxcjvlwedbsyoqfzuknpxwt
bgtrvcjvlhedbsyoqfzuknpxwt
afirmcjvlpedbsyoqvzuknpxwt
agirmcjjvhedbsyoqfzukmpxwt
ggirmcjvlhedbsyoqfzukypxvt
agirmdjulhekbsyoqfzuknpxwt
agirmcjqlhedbsyoqfztknixwt
agirmcjvjhedbsyomfduknpxwt
agirmcjvlhedbgyoqfzuknpxtq
agirmvjvlhbdbsyfqfzuknpxwt
agirmcjvlhedbsyatfzbknpxwt
agirmcjvlrlybsyoqfzuknpxwt
agirmajvlhedbsqovfzuknpxwt
abinmcrvlhedbsyoqfzuknpxwt
agnrmcjvlhedbsyurfzuknpxwt
agirmpjvlhedbsyoqezuknpxct
agirmxjvlhedbsgoqjzuknpxwt
agirmcjvlhehbstoqfzuknpxht
qgirmcjvlhepcsyoqfzuknpxwt
tgirmcjvlhkdbsyoqszuknpxwt
agirmcjvdhedbscoqftuknpxwt
agbrmcjvlhedbsyoqfzukqpxwj
agurmcjvlhedbsyaqfzuknpxmt
agirmcjvohudbsyoqfmuknpxwt
agirmcjvlhekbsyoqfbuktpxwt
agirmcjvlhedhsyoqfzugnnxwt
agirmcjvlhedbsyjqyzuknpxft
agirmcjvlhedbsymufznknpxwt
agirmcjhlheubsyoqfzuknpxmt
agirmcjvlhwdbsywqfzwknpxwt
agirmcjvljedbsgqqfzuknpxwt
aglrmcjelhedbsyoqfzuknpxkt
agxrmcjvlhxdbsyoqfquknpxwt
agirmcjvnhedbsyoqfzuenfxwt
agirmcjvlhedbsyoqfzatnqxwt
agirmcvvlhedbsboqfzuknuxwt
agirncjvlhezbsyoqfzulnpxwt
agiamcjvdiedbsyoqfzuknpxwt
agirmcjvwhedbskoqfzhknpxwt
agiwmcjflhedbsyoqfzulnpxwt
agirmcjvlhedboyoqfzuknpjwl
agivmcjslhedbsyoqfzdknpxwt
agirmcjvlcedbsyoqfzukepxyt
akirmcjvlhjdbssoqfzuknpxwt
agvrmcjvldedmsyoqfzuknpxwt
agirecjvlhidbsyoqfzukbpxwt
abirmcjvlhjdbsyoqfkuknpxwt
agirmcjelhedbfyoqfzuknpxwj
agirmcjvlhedbbyoqrzukwpxwt
akirmcjvlhedbsyoyfzuknplwt
agirmcjvlhedbsydsfzuknpxwq
agirrcjvlhedbsyoqazuknpmwt
aeirmcjvlhedbsyoqfvuknpwwt
akirmcjvlhedbsyoqpzudnpxwt
agijmcjvlhedbsyuqfzunnpxwt
agirmcjilhedasyoqizuknpxwt
agirmczvlhzdbsyoqfzuknpxwx
agirmcjvlhehbsyoifzuknpxwo
agirwcjvlhedbsyoqfzuenpxst
agirmcjvlhedbsyoquzuknhxft
agirmcqvlkedbsyoqfzrknpxwt
agirmcqvlhenbsyoqfzuknpuwt
agirmcjvleedbsyoqfzhhnpxwt
agirmcjvlhembsyrqfauknpxwt
agirmcjvlhedbssoqflcknpxwt
aqirmcjvlnedbsyoqfzuknpxpt
agirmcjqlhedbxpoqfzuknpxwt
fgirmcjvlhedbsyoqfzukqpqwt
aggrmcjvlhpdbsyoqfzuknpxjt
agirmwjvlhedbsywqfzuknpzwt
agirmcailhembsyoqfzuknpxwt
aglrmcjvlhxdbsyoqfzuknpxet
xgirmcjvlhzdbsyoqfzukrpxwt
agvrmcjvuhedbsyoqfzuknpxgt
agikmcjvlhecbsyoqfzuknpxwr
agyrmcjvlhezbsyoqfouknpxwt
agirmcjvfhjdbsyokfzuknpxwt
agkrmjjvlhedtsyoqfzuknpxwt
agirmgjvlhedbiyoqfzuknpxwv
wcirmcjvlhedbsyoqfzuknpxwo
aairmcjvlhedbstoqfguknpxwt
hgirmcjvlhedwfyoqfzuknpxwt
agirmcjvmhfdbmyoqfzuknpxwt
agirmcjvlhvdbsioqfzuanpxwt
agrrmcjvgsedbsyoqfzuknpxwt
agirmcjvlqetbsysqfzuknpxwt
agirccjvlhedbsyoqfzuknkcwt
agirmqjvlhedbsdoqfzkknpxwt
agirmcjvlheobsyopfzuknpxwg
agirmcjolhedbsyofpzuknpxwt
agirmcjnlhedbsyoqkzukfpxwt
agiumcjvlheabsyoqfzuknpxbt
agipmcjvlhedbsyoqfzukupxwz
atirmcrvlhedbsyoqfnuknpxwt
agirmcjvnhedfkyoqfzuknpxwt
agirmrjvlhedboyoqfzvknpxwt
abhrmcjvlhedbtyoqfzuknpxwt
cbirmcjvlhedbfyoqfzuknpxwt
agirmcjvlhedbsyoqfmwknjxwt
ahirmcjvlhedbsloqfzuknpfwt
agarmjjvlhedbsyoqfzyknpxwt
ajirmcjvlhevjsyoqfzuknpxwt
agirmcjvlhpdbstoqfzuknpewt
agirmcsvlhedbsyoqfbupnpxwt
agirmcjvlhexbsyodfzukqpxwt
auiymcjblhedbsyoqfzuknpxwt
azirmcjvchedbsyoqfziknpxwt
aeirmcjvlhedvsyoqfzuonpxwt
agirmcjvlhedbfyoqfbukjpxwt
ygirmcjvlhidbsyoqfzukncxwt
agirmxpvlhedbsyoqffuknpxwt
ztirmcjvlhedosyoqfzuknpxwt
agirmcjvlhepbsyoqfzuenppwt
agirmcjvshedbsyoqnzaknpxwt
awirmcjvlhydbsyoqfzuknoxwt
ucirmcjvlhedbsyoqfjuknpxwt
agirmwjvlhkbbsyoqfzuknpxwt
agirmcjvldedbsyohfzuknpxzt
agirmcjvwhedbsyoqfznknpxgt
agiricjvlhedxqyoqfzuknpxwt
agirmcjvlhzdbjyoqfzukapxwt
agirmcgvlhedbsyoqfzuknaowt
agidmcjvlhedbsyoqayuknpxwt
agirmcjvlhedisnoqfzuknpxnt
wkjrmcjvlhedbsyoqfzuknpxwt
agirmcjvlhedbuyojfzukxpxwt
agkrmcjvlhedbsybqfzurnpxwt
agirmcjvghedbsyoqfzuknexwj
agirmcjvnhedbsyoqfzuznpxit
agirmcjvlbedbsyoqfiukwpxwt
agirlctvlheabsyoqfzuknpxwt
agirmcjzzhedbsyoqfzcknpxwt
akirmcjvlnedbsyoqfzlknpxwt
agirmdjvlhedpsyoqfzuknpjwt
agiyjcuvlhedbsyoqfzuknpxwt
agirmcbvltedysyoqfzuknpxwt
agirmcjvlhedfdyoqfzubnpxwt
agidmcjvlhedesfoqfzuknpxwt
aeirmcjvlhedqsyoqfxuknpxwt
agifmcjvlhedbsyoqfquknptwt
agidmcjvlhedbsyfqfzuknpxwb
agirvcjvlhedbsroqfzuknjxwt
agirmcqvlhddbsyoqfzuknpxwj
agirmcjvlhmdqsyoqizuknpxwt
atirmcjvltedbsyoqfzuknpxwz
agirxnjvlhedbsyoqfzuknpxkt
agihmcjvlhedbsyoqfzukepxqt
agirmcjvlhedbsmoqzsuknpxwt
agirycjvlhedbuyoqfwuknpxwt
agirmcjvlhedbsyoqfzfkrfxwt
agirzcjvlhedbsyoqfhuknpxnt
agigmcjvlhedbsqnqfzuknpxwt
agirmgzvlhedbsyoqfzuonpxwt
agirmcjvqhedbqyoqfzukqpxwt
anarmcjvlhedbsyocfzuknpxwt
agirmcjuihedbshoqfzuknpxwt
agirdckvlhedbsyoqfzxknpxwt
ugirmujvlhwdbsyoqfzuknpxwt
mgirmcjvlheobsyovfzuknpxwt
agirmcjvghedbsyoqfzufxpxwt
agirmcjvlhedbsyoinzuknuxwt
agirmzjvlhbdbsyoqfzlknpxwt
agivmcjvlhedbsconfzuknpxwt
agirmwfvlhedtsyoqfzuknpxwt
agirmcjvlhedbbyoqrzukncxwt
agirmcjvlhelbsyoqfzupnlxwt
agirmmjvluedqsyoqfzuknpxwt
agjrmcjvlhedbsyaqfcuknpxwt
agiwmcjvlhedbsyoqzzuknpswt
agirxcjvlhedbsyoqfyvknpxwt
agirmljvlhedbsyoqkzuknpxjt
agirmcjvchedbsyoqfzmknyxwt
agirmcjvlhedbsyovfzuynpxwl
agtrmcjvlhedysyoqfzuknexwt
agirmcjvmhedbslonfzuknpxwt
agirmcjfshedbsyoqfziknpxwt
agirmcjvlhedbsygqfzkknpbwt
agyrmcivlhedbsyovfzuknpxwt
agirmcjvghedbsyoqjzuknkxwt
agirmcjvlhedqsyoqfzukspxmt
ayirmcjvhhedbsyomfzuknpxwt
agirmcjvlnembsypqfzuknpxwt
agirmcjqlhedbsyuvfzuknpxwt
agirmcjvlhembsybqfzuknpxwa
agirjcfvlhedbsyoqfuuknpxwt
agirmcjvohedbsyowfzuknxxwt
agirmcjvlhedroyoqfzukncxwt
agrrmijvlhedbsyoqfnuknpxwt
agirmjjvlhsdbsyoqfzumnpxwt
agirrcjvnhedbsyoqfzuktpxwt
agirmcjvlzedjsyoqfzuknpdwt
agirmkjvlhedbsyoqfzxinpxwt
agirmcjvlhedbzyojfzuknpvwt
arirmcjvlheddsyoqfzuknrxwt
agirmcjvlhedbsyoqhzuanpxmt
agirmcjvluedbsyoqozuknwxwt
afirmcjwlhedxsyoqfzuknpxwt
agirmcjvlhefbsyoqfkuinpxwt
agirycjvltedbsypqfzuknpxwt
agirmrxvlhedbsyoqfzeknpxwt
agfrmcqvlhedbsyoqxzuknpxwt
agormcjvuhexbsyoqfzuknpxwt
agyrmcjvehddbsyoqfzuknpxwt
agirmcjvlheqbsynqfzgknpxwt
agirmcjvlhedbsloufwuknpxwt
tgirmcjvlwedbsyoqfzuknpqwt
agirmcjvlhesbzyogfzuknpxwt
agitmdjvlhedpsyoqfzuknpjwt
bgirmejvlhtdbsyoqfzuknpxwt
aginmcjvlhedzsyoqfzuknoxwt
agvrzcjvlhedbsuoqfzuknpxwt
agormcjvlhedbsyoqfzuknpodt
agirmcevlhedbgyojfzuknpxwt
agirmcjblhedboytqfzuknpxwt
qgibmcjvlhedbsyoqfzuknbxwt
agirmcjvlhedbsyoafzutnnxwt
agiamcjvchkdbsyoqfzuknpxwt
agirmcjvehedblyoqwzuknpxwt
agirmcpvlhwdbsyoafzuknpxwt
agirmcjvlhtdbsyoqfzumnpxtt
agirmcjalhegtsyoqfzuknpxwt
agirdijvlhedbsyoqfzutnpxwt
agirmckvlhgdbsyovfzuknpxwt
qgmrmcjvlkedbsyoqfzuknpxwt
agirjcjvlhodbsyoqfzuanpxwt
ajirmcjvlhedbpyoqftuknpxwt
cgirmcjvlhedbsyoqfiuonpxwt
ayirmcjvlhedbsyaqfzuknwxwt
agirmcjvlhedbdyoqbzwknpxwt
+50
View File
@@ -0,0 +1,50 @@
package main
import (
"adventofcode/util"
"fmt"
"strings"
)
func main() {
input := util.ReadFile("../input.txt")
lines := strings.Split(input, "\n")
var twos, threes int
for _, boxID := range lines {
if hasTwo(boxID) {
twos++
}
if hasThree(boxID) {
threes++
}
}
fmt.Println("checksum", twos*threes)
}
func hasTwo(box string) bool {
chars := make(map[rune]int)
for _, c := range box {
chars[c]++
}
for _, v := range chars {
if v == 2 {
return true
}
}
return false
}
func hasThree(box string) bool {
chars := make(map[rune]int)
for _, c := range box {
chars[c]++
}
for _, v := range chars {
if v == 3 {
return true
}
}
return false
}
+39
View File
@@ -0,0 +1,39 @@
package main
import (
"adventofcode/util"
"fmt"
"strings"
)
// brute force it...
// assume only one valid answer
func main() {
input := util.ReadFile("../input.txt")
lines := strings.Split(input, "\n")
for i := 0; i < len(lines); i++ {
for j := i + 1; j < len(lines); j++ {
if sameChars := getSameCharacters(lines[i], lines[j]); sameChars != "" {
fmt.Println("common letters are", sameChars)
return
}
}
}
}
func getSameCharacters(str1, str2 string) string {
var mismatchSeen bool
var sameChars string
for i := 0; i < len(str1); i++ {
if str1[i] == str2[i] {
sameChars += string(str1[i])
} else if mismatchSeen {
// if a mismatch has already been seen, then it's 2 characters off
// return an empty string
return ""
} else {
mismatchSeen = true
}
}
return sameChars
}
+45
View File
@@ -0,0 +1,45 @@
--- Day 2: Inventory Management System ---
You stop falling through time, catch your breath, and check the screen on the device. "Destination reached. Current Year: 1518. Current Location: North Pole Utility Closet 83N10." You made it! Now, to find those anomalies.
Outside the utility closet, you hear footsteps and a voice. "...I'm not sure either. But now that so many people have chimneys, maybe he could sneak in that way?" Another voice responds, "Actually, we've been working on a new kind of suit that would let him fit through tight spaces like that. But, I heard that a few days ago, they lost the prototype fabric, the design plans, everything! Nobody on the team can even seem to remember important details of the project!"
"Wouldn't they have had enough fabric to fill several boxes in the warehouse? They'd be stored together, so the box IDs should be similar. Too bad it would take forever to search the warehouse for two similar box IDs..." They walk too far away to hear any more.
Late at night, you sneak to the warehouse - who knows what kinds of paradoxes you could cause if you were discovered - and use your fancy wrist device to quickly scan every box and produce a list of the likely candidates (your puzzle input).
To make sure you didn't miss any, you scan the likely candidate boxes again, counting the number that have an ID containing exactly two of any letter and then separately counting those with exactly three of any letter. You can multiply those two counts together to get a rudimentary checksum and compare it to what your device predicts.
For example, if you see the following box IDs:
abcdef contains no letters that appear exactly two or three times.
bababc contains two a and three b, so it counts for both.
abbcde contains two b, but no letter appears exactly three times.
abcccd contains three c, but no letter appears exactly two times.
aabcdd contains two a and two d, but it only counts once.
abcdee contains two e.
ababab contains three a and three b, but it only counts once.
Of these box IDs, four of them contain a letter which appears exactly twice, and three of them contain a letter which appears exactly three times. Multiplying these together produces a checksum of 4 * 3 = 12.
What is the checksum for your list of box IDs?
Your puzzle answer was 5434.
The first half of this puzzle is complete! It provides one gold star: *
--- 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.)