This commit is contained in:
Brian Buller 2022-12-08 06:48:58 -06:00
commit d5aacad011
30 changed files with 9226 additions and 1 deletions

2269
2022/day01/input Normal file

File diff suppressed because it is too large Load Diff

44
2022/day01/main.go Normal file
View File

@ -0,0 +1,44 @@
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
fmt.Println("# Answer")
solve(inp)
}
func solve(input []string) {
var elves []int
var top1, top2, top3 int
elf := 0
var sum int
record := func() {
elves = append(elves, sum)
if elf != 0 && sum > elves[top1] {
top3, top2, top1 = top2, top1, elf
} else if sum > elves[top2] {
top3, top2 = top2, elf
} else if sum > elves[top3] {
top3 = elf
}
elf++
sum = 0
}
for i := 0; i < len(input); i++ {
if input[i] == "" {
record()
continue
}
sum = sum + h.Atoi(input[i])
}
record()
fmt.Printf("The elf with the most calories is #%d (%d)\n", top1+1, elves[top1])
fmt.Printf("The elf with the second most calories is #%d (%d)\n", top2+1, elves[top2])
fmt.Printf("The elf with the third most calories is #%d (%d)\n", top3+1, elves[top3])
fmt.Printf("Total by top three: %d\n", elves[top1]+elves[top2]+elves[top3])
}

94
2022/day01/problem Normal file
View File

@ -0,0 +1,94 @@
Advent of Code
br0xen (AoC++) 2*
--- Day 1: Calorie Counting ---
Santa's reindeer typically eat regular reindeer food, but they need a lot of magical energy to deliver presents on Christmas.
For that, their favorite snack is a special type of star fruit that only grows deep in the jungle. The Elves have brought you
on their annual expedition to the grove where the fruit grows.
To supply enough magical energy, the expedition needs to retrieve a minimum of fifty stars by December 25th. Although the
Elves assure you that the grove has plenty of fruit, you decide to grab any fruit you see along the way, just in case.
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 jungle must be too overgrown and difficult to navigate in vehicles or access from the air; the Elves' expedition
traditionally goes on foot. As your boats approach land, the Elves begin taking inventory of their supplies. One important
consideration is food - in particular, the number of Calories each Elf is carrying (your puzzle input).
The Elves take turns writing down the number of Calories contained by the various meals, snacks, rations, etc. that they've
brought with them, one item per line. Each Elf separates their own inventory from the previous Elf's inventory (if any) by a
blank line.
For example, suppose the Elves finish writing their items' Calories and end up with the following list:
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
This list represents the Calories of the food carried by five Elves:
 The first Elf is carrying food with 1000, 2000, and 3000 Calories, a total of 6000 Calories.
 The second Elf is carrying one food item with 4000 Calories.
 The third Elf is carrying food with 5000 and 6000 Calories, a total of 11000 Calories.
 The fourth Elf is carrying food with 7000, 8000, and 9000 Calories, a total of 24000 Calories.
 The fifth Elf is carrying one food item with 10000 Calories.
In case the Elves get hungry and need extra snacks, they need to know which Elf to ask: they'd like to know how many Calories
are being carried by the Elf carrying the most Calories. In the example above, this is 24000 (carried by the fourth Elf).
Find the Elf carrying the most Calories. How many total Calories is that Elf carrying?
Your puzzle answer was 72070.
--- Part Two ---
By the time you calculate the answer to the Elves' question, they've already realized that the Elf carrying the most Calories
of food might eventually run out of snacks.
To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three Elves
carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups.
In the example above, the top three Elves are the fourth Elf (with 24000 Calories), then the third Elf (with 11000 Calories),
then the fifth Elf (with 10000 Calories). The sum of the Calories carried by these three elves is 45000.
Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total?
Your puzzle answer was 211805.
Both parts of this puzzle are complete! They provide two gold stars: **
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2022/about
. https://adventofcode.com/2022/events
. https://teespring.com/stores/advent-of-code
. https://adventofcode.com/2022/settings
. https://adventofcode.com/2022/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2022/support
. https://adventofcode.com/2022
. https://adventofcode.com/2022
. https://adventofcode.com/2022/support
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022/leaderboard
. https://adventofcode.com/2022/stats
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2018/day/25
. https://adventofcode.com/2022
. https://adventofcode.com/2022/day/1/input

14
2022/day01/testinput Normal file
View File

@ -0,0 +1,14 @@
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000

2500
2022/day02/input Normal file

File diff suppressed because it is too large Load Diff

56
2022/day02/main.go Normal file
View File

@ -0,0 +1,56 @@
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
fmt.Println("# Part 1")
fmt.Printf("Score: %d\n", part1(inp))
fmt.Println("# Part 2")
fmt.Printf("Score: %d\n", part2(inp))
}
func part1(input []string) int {
score := 0
for i := range input {
first, second := input[i][0], (input[i][2] - 23)
score += int(second - 'A' + 1)
switch second - first {
case 1, 254:
score += 6
case 0:
score += 3
}
}
return score
}
func part2(input []string) int {
score := 0
for i := range input {
against, to := input[i][0], input[i][2]
choose := byte(64)
switch to {
case 'X':
choose = (against - 1)
if choose < 'A' {
choose = 'C'
}
case 'Y':
score += 3
choose = against
case 'Z':
choose = (against + 1)
if choose > 'C' {
choose = 'A'
}
score += 6
}
score += int(choose - 64)
}
return score
}

77
2022/day02/problem Normal file
View File

@ -0,0 +1,77 @@
Advent of Code
br0xen (AoC++) 4*
--- Day 2: Rock Paper Scissors ---
The Elves begin to set up camp on the beach. To decide whose tent gets to be closest to the snack storage, a giant Rock Paper Scissors tournament is already in progress.
Rock Paper Scissors is a game between two players. Each game contains many rounds; in each round, the players each simultaneously choose one of Rock, Paper, or Scissors using a hand shape. Then, a winner for that round is selected: Rock defeats
Scissors, Scissors defeats Paper, and Paper defeats Rock. If both players choose the same shape, the round instead ends in a draw.
Appreciative of your help yesterday, one Elf gives you an encrypted strategy guide (your puzzle input) that they say will be sure to help you win. "The first column is what your opponent is going to play: A for Rock, B for Paper, and C for
Scissors. The second column--" Suddenly, the Elf is called away to help with someone's tent.
The second column, you reason, must be what you should play in response: X for Rock, Y for Paper, and Z for Scissors. Winning every time would be suspicious, so the responses must have been carefully chosen.
The winner of the whole tournament is the player with the highest score. Your total score is the sum of your scores for each round. The score for a single round is the score for the shape you selected (1 for Rock, 2 for Paper, and 3 for Scissors)
plus the score for the outcome of the round (0 if you lost, 3 if the round was a draw, and 6 if you won).
Since you can't be sure if the Elf is trying to help you or trick you, you should calculate the score you would get if you were to follow the strategy guide.
For example, suppose you were given the following strategy guide:
A Y
B X
C Z
This strategy guide predicts and recommends the following:
 In the first round, your opponent will choose Rock (A), and you should choose Paper (Y). This ends in a win for you with a score of 8 (2 because you chose Paper + 6 because you won).
 In the second round, your opponent will choose Paper (B), and you should choose Rock (X). This ends in a loss for you with a score of 1 (1 + 0).
 The third round is a draw with both players choosing Scissors, giving you a score of 3 + 3 = 6.
In this example, if you were to follow the strategy guide, you would get a total score of 15 (8 + 1 + 6).
What would your total score be if everything goes exactly according to your strategy guide?
Your puzzle answer was 13268.
--- Part Two ---
The Elf finishes helping with the tent and sneaks back over to you. "Anyway, the second column says how the round needs to end: X means you need to lose, Y means you need to end the round in a draw, and Z means you need to win. Good luck!"
The total score is still calculated in the same way, but now you need to figure out what shape to choose so the round ends as indicated. The example above now goes like this:
 In the first round, your opponent will choose Rock (A), and you need the round to end in a draw (Y), so you also choose Rock. This gives you a score of 1 + 3 = 4.
 In the second round, your opponent will choose Paper (B), and you choose Rock so you lose (X) with a score of 1 + 0 = 1.
 In the third round, you will defeat your opponent's Scissors with Rock for a score of 1 + 6 = 7.
Now that you're correctly decrypting the ultra top secret strategy guide, you would get a total score of 12.
Following the Elf's instructions for the second column, what would your total score be if everything goes exactly according to your strategy guide?
Your puzzle answer was 15508.
Both parts of this puzzle are complete! They provide two gold stars: **
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2022/about
. https://adventofcode.com/2022/events
. https://adventofcode.com/2022/settings
. https://adventofcode.com/2022/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2022/support
. https://adventofcode.com/2022
. https://adventofcode.com/2022
. https://adventofcode.com/2022/support
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022/leaderboard
. https://adventofcode.com/2022/stats
. https://adventofcode.com/2022/sponsors
. https://en.wikipedia.org/wiki/Rock_paper_scissors
. https://adventofcode.com/2022
. https://adventofcode.com/2022/day/2/input

3
2022/day02/testinput Normal file
View File

@ -0,0 +1,3 @@
A Y
B X
C Z

9
2022/day02/testinput2 Normal file
View File

@ -0,0 +1,9 @@
A X
B X
C X
A Y
B Y
C Y
A Z
B Z
C Z

300
2022/day03/input Normal file
View File

@ -0,0 +1,300 @@
lDDWVvlVVQfDMlWjGJTRjQCgTGLCLj
ZLZpwzLBhwZhSLBsjntGCtgJRjbnJgSG
qppdZzzsdsmZphrNsNwZhllDHLcVVDWFPvFWcWdFlv
ztdhgJDBJghmQtPFQPpmbw
lVlLRcnfllcfVcccGnSQVLcsTPFbpwsPFspTSqmbpswpbF
cCHRGcGcCRGlGrGcVGnrdWHWBDzBNhhQZWWNBhJz
NfnSSQpdnRSSpvWdSsjZDGNDjGDwTGTjHG
wlPzPqzPFbMmqPCFCJmbsjbHLDDHDZjDjbGHsT
gwMlgmtmPcqVVvhVnvcRnn
cBNBBCHhbhNhblBcCCvcBHSwTwDQSqRwDQpDRsjHST
dPmzMVWdmmMnnZJtZVdqjTSrjTjrpQrsTTVwQRSj
qzmZMmdZPtnGqclblGlbGvgBFc
ZfpmNDfRhzbbqDnD
SFtFjTTZVTFvVTjHrsVvqGBJqhnSSnbJznLGJwJq
TjdPPtdMPPWCcZgW
qsbmGCsjHNhmhmhzTDznpnlQZlbWlZ
LTSSfSvggVVgBgfLtvvtTSczzpWnZQZnlnzpBcnWpQWc
FrLvrrrVPgdPftSHHdNsjTHjmGThhm
wGQlMjvMwpvjvZjZTZlWjplWJJTggDTgfCnntPgTJPbtPgSP
qNhJmcVmdJqhHnPnDNtnPDCg
LrrchqhRdLVzRdmhJcFFQWGGMjpGlvZzlQQv
ZqZMbZMfQZptpjlF
PJCggvHlwWHvSSvCJNvSPvDBtFDQThFQjtRQhhFTsVThQtsF
PwWC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vjC
ppcnnBZqllTQfmrffbtGTDGt
dsDFsBZBhCFhshFrpBFnmLQmHmRgRbLqmmmRQDLm
PPBBNNNtGTwJNfTJffNttfLQgqLgHvHbqRwlgmblRvll
NSNTGTJTWPjGWMPSJJzrBSpzdFSddCFdncrs
bPzRlMPTzTMldJMnhswcjzfQVccQhc
HCCqNmNmQQmQssJn
pHptprtgRStTtMJt
nTmhrsPMsTfmHHGcSgtj
bJJwdlrJQLlvwlQDDwSbgffGVNjfgjNtVbcf
QlpDQFJdvdFqJdFpLvDFpLLnzZMnBMRRzMTqnrzqTRPznz
qRVRqBzgwqRpqRgVqQRPpQJJPrPhPGJnsGrCFdFJrZdG
ZvWDmMvmSvCndssrsJ
WcZcNWlcMjBQpzNTqVBp
DpLPZLDDlcgmDmhVgfgfWWRwhwwt
VrVMdbCrrBTjCMQQtMwQNSqMQW
VCBHdJHdvrrFsbsdrBJTdTzZcpmZGDGPlmzmlccFDZDn

74
2022/day03/main.go Normal file
View File

@ -0,0 +1,74 @@
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
fmt.Println("# Part 1")
fmt.Println(part1(inp))
fmt.Println("# Part 2")
fmt.Println(part2(inp))
}
func part1(inp []string) int {
var ret int
for i := range inp {
sack := inp[i]
c1, c2 := sack[:len(sack)/2], sack[len(sack)/2:]
var dupe byte
for _, c1w := range c1 {
for _, c2w := range c2 {
if c1w == c2w {
dupe = byte(c1w)
break
}
}
if dupe != 0 {
break
}
}
if dupe >= 'a' {
ret += int(dupe - 'a' + 1)
} else if dupe >= 'A' {
ret += int(dupe - 'A' + 27)
}
}
return ret
}
func part2(inp []string) int {
var ret int
elves := []string{"", "", ""}
for i := range inp {
elves[i%3] = inp[i]
if i%3 == 2 {
var badge byte
// Check 'em
for _, e1 := range elves[0] {
for _, e2 := range elves[1] {
for _, e3 := range elves[2] {
if e1 == e2 && e2 == e3 {
badge = byte(e1)
}
}
if badge != 0 {
break
}
}
if badge != 0 {
break
}
}
if badge >= 'a' {
ret += int(badge - 'a' + 1)
} else if badge >= 'A' {
ret += int(badge - 'A' + 27)
}
}
}
return ret
}

140
2022/day03/problem Normal file
View File

@ -0,0 +1,140 @@
Advent of Code
• [About]
• [Events]
• [Shop]
• [Settings]
• [Log Out]
br0xen (AoC++) 6*
      /*2022*/
• [Calendar]
• [AoC++]
• [Sponsors]
• [Leaderboard]
• [Stats]
Our sponsors help make Advent of Code possible:
Spotify - Follow our engineering blog to see how our developers solve complex tech problems, at scale, every day.
--- Day 3: Rucksack Reorganization ---
One Elf has the important job of loading all of the rucksacks with supplies for the jungle journey. Unfortunately, that
Elf didn't quite follow the packing instructions, and so a few items now need to be rearranged.
Each rucksack has two large compartments. All items of a given type are meant to go into exactly one of the two
compartments. The Elf that did the packing failed to follow this rule for exactly one item type per rucksack.
The Elves have made a list of all of the items currently in each rucksack (your puzzle input), but they need your help
finding the errors. Every item type is identified by a single lowercase or uppercase letter (that is, a and A refer to
different types of items).
The list of items for each rucksack is given as characters all on a single line. A given rucksack always has the same
number of items in each of its two compartments, so the first half of the characters represent items in the first
compartment, while the second half of the characters represent items in the second compartment.
For example, suppose you have the following list of contents from six rucksacks:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
 The first rucksack contains the items vJrwpWtwJgWrhcsFMMfFFhFp, which means its first compartment contains the items
vJrwpWtwJgWr, while the second compartment contains the items hcsFMMfFFhFp. The only item type that appears in both
compartments is lowercase p.
 The second rucksack's compartments contain jqHRNqRjqzjGDLGL and rsFMfFZSrLrFZsSL. The only item type that appears in
both compartments is uppercase L.
 The third rucksack's compartments contain PmmdzqPrV and vPwwTWBwg; the only common item type is uppercase P.
 The fourth rucksack's compartments only share item type v.
 The fifth rucksack's compartments only share item type t.
 The sixth rucksack's compartments only share item type s.
To help prioritize item rearrangement, every item type can be converted to a priority:
 Lowercase item types a through z have priorities 1 through 26.
 Uppercase item types A through Z have priorities 27 through 52.
In the above example, the priority of the item type that appears in both compartments of each rucksack is 16 (p), 38
(L), 42 (P), 22 (v), 20 (t), and 19 (s); the sum of these is 157.
Find the item type that appears in both compartments of each rucksack. What is the sum of the priorities of those item
types?
Your puzzle answer was 7967.
--- Part Two ---
As you finish identifying the misplaced items, the Elves come to you with another issue.
For safety, the Elves are divided into groups of three. Every Elf carries a badge that identifies their group. For
efficiency, within each group of three Elves, the badge is the only item type carried by all three Elves. That is, if a
group's badge is item type B, then all three Elves will have item type B somewhere in their rucksack, and at most two of
the Elves will be carrying any other item type.
The problem is that someone forgot to put this year's updated authenticity sticker on the badges. All of the badges need
to be pulled out of the rucksacks so the new authenticity stickers can be attached.
Additionally, nobody wrote down which item type corresponds to each group's badges. The only way to tell which item type
is the right one is by finding the one item type that is common between all three Elves in each group.
Every set of three lines in your list corresponds to a single group, but each group can have a different badge item
type. So, in the above example, the first group's rucksacks are the first three lines:
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
And the second group's rucksacks are the next three lines:
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw
In the first group, the only item type that appears in all three rucksacks is lowercase r; this must be their badges. In
the second group, their badge item type must be Z.
Priorities for these items must still be found to organize the sticker attachment efforts: here, they are 18 (r) for the
first group and 52 (Z) for the second group. The sum of these is 70.
Find the item type that corresponds to the badges of each three-Elf group. What is the sum of the priorities of those
item types?
Your puzzle answer was 2716.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
You can also [Shareon Twitter Mastodon] this puzzle.
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2022/about
. https://adventofcode.com/2022/events
. https://teespring.com/stores/advent-of-code
. https://adventofcode.com/2022/settings
. https://adventofcode.com/2022/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2022/support
. https://adventofcode.com/2022
. https://adventofcode.com/2022
. https://adventofcode.com/2022/support
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022/leaderboard
. https://adventofcode.com/2022/stats
. https://adventofcode.com/2022/sponsors
. https://engineering.atspotify.com/
. https://en.wikipedia.org/wiki/Rucksack
. https://adventofcode.com/2022
. https://adventofcode.com/2022/day/3/input
. https://twitter.com/intent/tweet?text=I%27ve+completed+%22Rucksack+Reorganization%22+%2D+Day+3+%2D+Advent+of+Code+2022&url=https%3A%2F%2Fadventofcode%2Ecom%2F2022%2Fday%2F3&related=ericwastl&hashtags=AdventOfCode
. javascript:void(0);

6
2022/day03/testinput Normal file
View File

@ -0,0 +1,6 @@
vJrwpWtwJgWrhcsFMMfFFhFp
jqHRNqRjqzjGDLGLrsFMfFZSrLrFZsSL
PmmdzqPrVvPwwTWBwg
wMqvLMZHhHMvwLHjbvcjnnSBnvTQFn
ttgJtRGJQctTZtZT
CrZsJsPPZsGzwwsLwLmpwMDw

1000
2022/day04/input Normal file

File diff suppressed because it is too large Load Diff

83
2022/day04/main.go Normal file
View File

@ -0,0 +1,83 @@
package main
import (
"fmt"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
part1(inp)
part2(inp)
}
func part1(inp []string) {
fmt.Println("# Part 1")
var ret int
for _, pair := range inp {
elves := strings.Split(pair, ",")
e1S, e1E := getRange(elves[0])
e2S, e2E := getRange(elves[1])
if rangesFullyOverlap(e1S, e1E, e2S, e2E) {
ret++
}
}
fmt.Printf("Fully Overlapping Ranges: %d\n", ret)
}
func part2(inp []string) {
fmt.Println("# Part 2")
var ret int
for _, pair := range inp {
elves := strings.Split(pair, ",")
e1S, e1E := getRange(elves[0])
e2S, e2E := getRange(elves[1])
if rangesOverlap(e1S, e1E, e2S, e2E) {
ret++
}
}
fmt.Printf("Overlapping Ranges: %d\n", ret)
}
func partXAllOverlaps(inp []string) {
fmt.Println("# Part x")
var overlaps int
sections := make(map[int]int)
for _, pair := range inp {
elves := strings.Split(pair, ",")
for elfIdx, elf := range elves {
var overlapped bool
i1, i2 := getRange(elf)
for i := i1; i <= i2; i++ {
if _, ok := sections[i]; ok {
overlapped = true
} else {
sections[i] = elfIdx
}
}
if overlapped {
overlaps++
}
}
}
fmt.Printf("Overlapping Assignments: %d\n", overlaps)
}
func rangesFullyOverlap(e1start, e1end, e2start, e2end int) bool {
return (e1start >= e2start && e1end <= e2end) ||
(e2start >= e1start && e2end <= e1end)
}
func rangesOverlap(e1start, e1end, e2start, e2end int) bool {
ret := (e1start >= e2start && e1start <= e2end) ||
(e1end >= e2start && e1end <= e2end) ||
(e2start >= e1start && e2start <= e1end) ||
(e2end >= e1start && e2end <= e1end)
return ret
}
func getRange(elf string) (int, int) {
idxsS := strings.Split(elf, "-")
return h.Atoi(idxsS[0]), h.Atoi(idxsS[1])
}

106
2022/day04/problem Normal file
View File

@ -0,0 +1,106 @@
Advent of Code
br0xen (AoC++) 7*
--- Day 4: Camp Cleanup ---
Space needs to be cleared before the last supplies can be unloaded from the ships, and so several Elves have been
assigned the job of cleaning up sections of the camp. Every section has a unique ID number, and each Elf is assigned a
range of section IDs.
However, as some of the Elves compare their section assignments with each other, they've noticed that many of the
assignments overlap. To try to quickly find overlaps and reduce duplicated effort, the Elves pair up and make a big list
of the section assignments for each pair (your puzzle input).
For example, consider the following list of section assignment pairs:
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8
For the first few pairs, this list means:
 Within the first pair of Elves, the first Elf was assigned sections 2-4 (sections 2, 3, and 4), while the second Elf
was assigned sections 6-8 (sections 6, 7, 8).
 The Elves in the second pair were each assigned two sections.
 The Elves in the third pair were each assigned three sections: one got sections 5, 6, and 7, while the other also
got 7, plus 8 and 9.
This example list uses single-digit section IDs to make it easier to draw; your actual list might contain larger
numbers. Visually, these pairs of section assignments look like this:
.234..... 2-4
.....678. 6-8
.23...... 2-3
...45.... 4-5
....567.. 5-7
......789 7-9
.2345678. 2-8
..34567.. 3-7
.....6... 6-6
...456... 4-6
.23456... 2-6
...45678. 4-8
Some of the pairs have noticed that one of their assignments fully contains the other. For example, 2-8 fully contains
3-7, and 6-6 is fully contained by 4-6. In pairs where one assignment fully contains the other, one Elf in the pair
would be exclusively cleaning sections their partner will already be cleaning, so these seem like the most in need of
reconsideration. In this example, there are 2 such pairs.
In how many assignment pairs does one range fully contain the other?
Your puzzle answer was 503.
The first half of this puzzle is complete! It provides one gold star: *
--- Part Two ---
It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would like to know the number of
pairs that overlap at all.
In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the remaining four pairs (5-7,7-9,
2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:
 5-7,7-9 overlaps in a single section, 7.
 2-8,3-7 overlaps all of the sections 3 through 7.
 6-6,4-6 overlaps in a single section, 6.
 2-6,4-8 overlaps in sections 4, 5, and 6.
So, in this example, the number of overlapping assignment pairs is 4.
In how many assignment pairs do the ranges overlap?
Your puzzle answer was 827.
Both parts of this puzzle are complete! They provide two gold stars: **
At this point, you should return to your Advent calendar and try another puzzle.
If you still want to see it, you can get your puzzle input.
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2022/about
. https://adventofcode.com/2022/events
. https://adventofcode.com/2022/settings
. https://adventofcode.com/2022/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2022/support
. https://adventofcode.com/2022
. https://adventofcode.com/2022
. https://adventofcode.com/2022/support
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022/leaderboard
. https://adventofcode.com/2022/stats
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022/day/4/input

6
2022/day04/testinput Normal file
View File

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

511
2022/day05/input Normal file
View File

@ -0,0 +1,511 @@
[N] [R] [C]
[T] [J] [S] [J] [N]
[B] [Z] [H] [M] [Z] [D]
[S] [P] [G] [L] [H] [Z] [T]
[Q] [D] [F] [D] [V] [L] [S] [M]
[H] [F] [V] [J] [C] [W] [P] [W] [L]
[G] [S] [H] [Z] [Z] [T] [F] [V] [H]
[R] [H] [Z] [M] [T] [M] [T] [Q] [W]
1 2 3 4 5 6 7 8 9
move 3 from 9 to 7
move 4 from 4 to 5
move 2 from 4 to 6
move 4 from 7 to 5
move 3 from 7 to 3
move 2 from 5 to 9
move 5 from 6 to 3
move 5 from 9 to 1
move 3 from 8 to 4
move 3 from 4 to 6
move 8 from 1 to 8
move 1 from 8 to 6
move 2 from 8 to 2
move 5 from 8 to 4
move 1 from 8 to 1
move 6 from 6 to 4
move 1 from 7 to 9
move 5 from 1 to 7
move 1 from 1 to 2
move 2 from 9 to 8
move 6 from 4 to 9
move 1 from 6 to 8
move 3 from 2 to 7
move 4 from 2 to 8
move 4 from 9 to 3
move 6 from 5 to 4
move 7 from 8 to 1
move 10 from 4 to 1
move 12 from 1 to 5
move 1 from 4 to 9
move 1 from 2 to 3
move 2 from 9 to 1
move 1 from 9 to 3
move 1 from 6 to 7
move 1 from 9 to 1
move 3 from 1 to 3
move 9 from 5 to 9
move 2 from 2 to 7
move 2 from 7 to 4
move 3 from 9 to 4
move 7 from 5 to 7
move 5 from 1 to 3
move 2 from 4 to 5
move 1 from 4 to 6
move 1 from 6 to 9
move 4 from 9 to 2
move 12 from 7 to 9
move 2 from 4 to 9
move 6 from 5 to 9
move 3 from 7 to 6
move 12 from 9 to 6
move 5 from 9 to 1
move 1 from 7 to 6
move 14 from 6 to 1
move 20 from 3 to 5
move 5 from 9 to 5
move 3 from 2 to 8
move 1 from 6 to 4
move 1 from 9 to 2
move 1 from 4 to 6
move 1 from 2 to 6
move 16 from 1 to 5
move 1 from 2 to 1
move 12 from 5 to 6
move 1 from 8 to 4
move 29 from 5 to 1
move 5 from 6 to 9
move 20 from 1 to 3
move 4 from 1 to 3
move 11 from 3 to 8
move 1 from 4 to 3
move 4 from 9 to 8
move 7 from 1 to 8
move 2 from 3 to 2
move 2 from 6 to 7
move 1 from 9 to 8
move 10 from 3 to 5
move 1 from 6 to 1
move 1 from 7 to 2
move 3 from 1 to 2
move 6 from 2 to 4
move 2 from 6 to 3
move 4 from 6 to 5
move 1 from 6 to 2
move 1 from 2 to 9
move 6 from 5 to 2
move 1 from 9 to 3
move 24 from 8 to 7
move 1 from 4 to 8
move 5 from 5 to 4
move 1 from 4 to 8
move 1 from 8 to 7
move 2 from 8 to 9
move 1 from 9 to 7
move 6 from 2 to 4
move 10 from 3 to 7
move 3 from 5 to 3
move 1 from 9 to 8
move 3 from 3 to 8
move 4 from 8 to 7
move 1 from 4 to 6
move 1 from 6 to 4
move 13 from 4 to 3
move 17 from 7 to 6
move 1 from 6 to 3
move 2 from 4 to 8
move 3 from 7 to 5
move 14 from 6 to 7
move 1 from 5 to 9
move 1 from 5 to 9
move 2 from 6 to 7
move 1 from 5 to 1
move 1 from 1 to 6
move 1 from 9 to 3
move 29 from 7 to 4
move 10 from 4 to 3
move 6 from 7 to 5
move 1 from 6 to 5
move 1 from 9 to 7
move 1 from 7 to 2
move 4 from 3 to 2
move 1 from 2 to 9
move 1 from 8 to 5
move 11 from 3 to 4
move 24 from 4 to 7
move 2 from 2 to 5
move 10 from 3 to 2
move 6 from 2 to 1
move 5 from 4 to 7
move 1 from 9 to 2
move 3 from 5 to 1
move 1 from 4 to 6
move 4 from 2 to 3
move 5 from 5 to 7
move 2 from 5 to 3
move 32 from 7 to 5
move 16 from 5 to 1
move 1 from 1 to 2
move 3 from 2 to 9
move 1 from 8 to 6
move 3 from 7 to 6
move 1 from 2 to 4
move 5 from 6 to 8
move 5 from 8 to 6
move 2 from 9 to 3
move 1 from 7 to 5
move 9 from 5 to 4
move 1 from 9 to 1
move 2 from 3 to 1
move 4 from 3 to 6
move 1 from 3 to 8
move 6 from 4 to 6
move 6 from 5 to 9
move 1 from 9 to 6
move 1 from 5 to 1
move 1 from 5 to 4
move 1 from 3 to 6
move 1 from 8 to 3
move 1 from 4 to 2
move 1 from 2 to 3
move 17 from 6 to 4
move 4 from 1 to 8
move 3 from 9 to 6
move 1 from 8 to 4
move 1 from 9 to 7
move 2 from 6 to 2
move 1 from 7 to 8
move 12 from 1 to 9
move 8 from 9 to 2
move 1 from 6 to 9
move 6 from 2 to 8
move 2 from 8 to 3
move 18 from 4 to 9
move 2 from 1 to 6
move 1 from 6 to 5
move 3 from 4 to 3
move 7 from 3 to 8
move 4 from 2 to 7
move 1 from 4 to 6
move 2 from 6 to 4
move 13 from 9 to 6
move 1 from 5 to 2
move 5 from 9 to 3
move 9 from 1 to 2
move 1 from 1 to 8
move 1 from 2 to 6
move 3 from 7 to 6
move 2 from 2 to 6
move 9 from 8 to 6
move 1 from 7 to 8
move 1 from 8 to 7
move 2 from 4 to 6
move 5 from 3 to 6
move 17 from 6 to 9
move 7 from 8 to 4
move 4 from 2 to 3
move 17 from 6 to 2
move 1 from 6 to 4
move 1 from 7 to 8
move 1 from 8 to 9
move 24 from 9 to 6
move 4 from 3 to 1
move 1 from 1 to 5
move 20 from 6 to 4
move 4 from 6 to 9
move 1 from 5 to 7
move 2 from 4 to 2
move 1 from 9 to 7
move 25 from 4 to 3
move 1 from 4 to 2
move 2 from 1 to 6
move 3 from 9 to 4
move 2 from 4 to 7
move 2 from 7 to 5
move 1 from 4 to 2
move 1 from 6 to 3
move 1 from 1 to 5
move 5 from 3 to 9
move 1 from 5 to 6
move 10 from 2 to 8
move 9 from 2 to 5
move 21 from 3 to 6
move 1 from 7 to 6
move 2 from 6 to 5
move 5 from 9 to 7
move 6 from 7 to 8
move 19 from 6 to 9
move 1 from 6 to 1
move 8 from 8 to 1
move 1 from 6 to 1
move 2 from 8 to 5
move 5 from 9 to 2
move 6 from 8 to 2
move 2 from 9 to 7
move 9 from 9 to 4
move 7 from 2 to 4
move 1 from 6 to 4
move 14 from 5 to 9
move 1 from 1 to 8
move 1 from 7 to 9
move 4 from 2 to 9
move 16 from 4 to 6
move 3 from 2 to 8
move 1 from 6 to 2
move 2 from 8 to 9
move 1 from 8 to 7
move 1 from 8 to 3
move 3 from 2 to 7
move 1 from 3 to 9
move 8 from 9 to 3
move 4 from 7 to 8
move 1 from 5 to 4
move 4 from 6 to 3
move 1 from 4 to 2
move 9 from 3 to 8
move 10 from 9 to 5
move 8 from 6 to 7
move 13 from 8 to 4
move 8 from 5 to 2
move 3 from 6 to 3
move 7 from 9 to 6
move 7 from 7 to 2
move 2 from 4 to 6
move 5 from 6 to 2
move 3 from 1 to 5
move 5 from 5 to 8
move 4 from 6 to 2
move 4 from 1 to 8
move 15 from 2 to 6
move 11 from 4 to 9
move 12 from 6 to 8
move 1 from 6 to 9
move 5 from 3 to 7
move 2 from 2 to 6
move 6 from 7 to 1
move 3 from 1 to 3
move 1 from 4 to 1
move 1 from 3 to 9
move 1 from 3 to 9
move 1 from 7 to 6
move 1 from 3 to 2
move 4 from 2 to 6
move 4 from 2 to 7
move 1 from 2 to 6
move 4 from 1 to 6
move 12 from 6 to 7
move 2 from 6 to 1
move 8 from 9 to 6
move 1 from 7 to 4
move 14 from 8 to 1
move 8 from 1 to 5
move 1 from 3 to 9
move 5 from 9 to 5
move 1 from 8 to 9
move 1 from 9 to 2
move 1 from 9 to 3
move 5 from 8 to 3
move 12 from 5 to 4
move 1 from 9 to 2
move 6 from 7 to 3
move 7 from 3 to 2
move 1 from 5 to 1
move 1 from 8 to 3
move 2 from 1 to 3
move 2 from 6 to 9
move 5 from 6 to 5
move 5 from 1 to 7
move 4 from 4 to 1
move 7 from 2 to 8
move 4 from 3 to 8
move 1 from 9 to 3
move 1 from 9 to 5
move 4 from 1 to 8
move 10 from 7 to 9
move 1 from 6 to 7
move 2 from 8 to 6
move 6 from 4 to 2
move 5 from 3 to 1
move 2 from 6 to 3
move 2 from 7 to 1
move 5 from 2 to 5
move 2 from 7 to 1
move 7 from 5 to 7
move 2 from 5 to 6
move 2 from 5 to 3
move 3 from 2 to 9
move 9 from 9 to 3
move 1 from 6 to 4
move 3 from 3 to 1
move 9 from 8 to 2
move 6 from 3 to 6
move 8 from 7 to 9
move 4 from 9 to 8
move 14 from 1 to 5
move 1 from 9 to 2
move 1 from 1 to 5
move 2 from 3 to 6
move 12 from 5 to 3
move 2 from 2 to 8
move 7 from 6 to 2
move 12 from 2 to 8
move 2 from 6 to 2
move 6 from 9 to 6
move 1 from 1 to 2
move 1 from 9 to 3
move 2 from 5 to 9
move 1 from 9 to 2
move 1 from 9 to 4
move 1 from 3 to 2
move 2 from 6 to 7
move 2 from 6 to 9
move 5 from 4 to 2
move 14 from 3 to 9
move 15 from 9 to 4
move 1 from 7 to 4
move 10 from 8 to 6
move 1 from 5 to 9
move 2 from 9 to 5
move 10 from 8 to 1
move 1 from 7 to 4
move 5 from 1 to 2
move 2 from 1 to 5
move 3 from 4 to 6
move 4 from 5 to 8
move 5 from 8 to 6
move 14 from 2 to 9
move 2 from 6 to 7
move 3 from 2 to 9
move 3 from 1 to 7
move 1 from 7 to 3
move 3 from 7 to 1
move 1 from 3 to 6
move 1 from 7 to 6
move 1 from 8 to 9
move 2 from 1 to 4
move 1 from 1 to 2
move 16 from 9 to 4
move 7 from 4 to 8
move 5 from 8 to 1
move 2 from 8 to 3
move 2 from 1 to 7
move 13 from 6 to 7
move 2 from 2 to 3
move 4 from 7 to 4
move 6 from 4 to 5
move 4 from 7 to 6
move 3 from 1 to 2
move 2 from 2 to 6
move 3 from 3 to 8
move 5 from 5 to 3
move 2 from 9 to 6
move 3 from 3 to 7
move 1 from 8 to 1
move 22 from 4 to 8
move 1 from 4 to 3
move 9 from 6 to 3
move 1 from 2 to 1
move 4 from 3 to 4
move 2 from 4 to 5
move 1 from 1 to 7
move 4 from 3 to 7
move 2 from 6 to 1
move 1 from 6 to 7
move 18 from 8 to 7
move 2 from 6 to 5
move 2 from 3 to 4
move 1 from 5 to 4
move 30 from 7 to 6
move 2 from 1 to 3
move 18 from 6 to 8
move 12 from 6 to 4
move 13 from 4 to 9
move 2 from 3 to 8
move 1 from 6 to 2
move 3 from 7 to 2
move 1 from 1 to 2
move 2 from 5 to 9
move 8 from 8 to 1
move 1 from 7 to 8
move 7 from 1 to 3
move 2 from 4 to 9
move 1 from 1 to 6
move 4 from 2 to 1
move 16 from 8 to 1
move 1 from 2 to 6
move 2 from 4 to 8
move 2 from 5 to 1
move 4 from 3 to 7
move 3 from 7 to 1
move 1 from 6 to 8
move 1 from 8 to 9
move 1 from 7 to 3
move 6 from 3 to 5
move 1 from 3 to 8
move 1 from 6 to 9
move 16 from 9 to 5
move 4 from 5 to 3
move 15 from 5 to 1
move 1 from 5 to 8
move 3 from 9 to 8
move 9 from 8 to 5
move 6 from 5 to 1
move 4 from 5 to 6
move 2 from 6 to 4
move 1 from 6 to 4
move 1 from 8 to 4
move 3 from 3 to 6
move 3 from 6 to 8
move 1 from 6 to 8
move 21 from 1 to 9
move 4 from 8 to 5
move 3 from 5 to 7
move 2 from 5 to 1
move 2 from 4 to 8
move 2 from 8 to 2
move 2 from 7 to 8
move 1 from 7 to 9
move 1 from 8 to 7
move 5 from 1 to 8
move 1 from 7 to 8
move 4 from 8 to 4
move 2 from 4 to 5
move 1 from 2 to 7
move 1 from 2 to 7
move 2 from 7 to 6
move 2 from 6 to 9
move 1 from 4 to 9
move 1 from 3 to 4
move 16 from 1 to 5
move 16 from 5 to 7
move 2 from 5 to 4
move 14 from 9 to 6
move 5 from 4 to 3
move 3 from 3 to 6
move 5 from 1 to 4
move 2 from 4 to 7
move 7 from 9 to 4
move 2 from 9 to 7
move 10 from 6 to 9
move 8 from 4 to 6
move 1 from 8 to 4
move 1 from 1 to 9
move 14 from 6 to 3
move 10 from 3 to 2
move 3 from 7 to 8
move 6 from 3 to 1
move 2 from 7 to 9
move 5 from 7 to 9
move 10 from 9 to 1
move 2 from 4 to 3
move 1 from 2 to 1
move 16 from 1 to 4
move 1 from 6 to 1
move 2 from 3 to 9
move 3 from 8 to 5
move 8 from 7 to 1
move 3 from 5 to 9
move 7 from 4 to 6
move 7 from 1 to 5
move 2 from 8 to 3
move 1 from 7 to 8

121
2022/day05/main.go Normal file
View File

@ -0,0 +1,121 @@
package main
import (
"fmt"
"sort"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
part1(inp)
fmt.Println("")
part2(inp)
}
func part1(inp []string) {
fmt.Println("# Part 1")
stacks, bottom := buildStacks(inp)
// Run instructions
// Instructions start at bottom+2
for i := bottom + 2; i < len(inp); i++ {
inst := strings.Fields(inp[i])
count := h.Atoi(inst[1])
from := h.Atoi(inst[3])
to := h.Atoi(inst[5])
for c := 0; c < count; c++ {
moveCrates(stacks, from, to, 1)
}
}
for i := range stacks {
fmt.Print(string(stacks[i][0]))
}
fmt.Println("")
}
func part2(inp []string) {
fmt.Println("# Part 2")
stacks, bottom := buildStacks(inp)
// Run instructions
// Instructions start at bottom+2
for i := bottom + 2; i < len(inp); i++ {
inst := strings.Fields(inp[i])
count := h.Atoi(inst[1])
from := h.Atoi(inst[3])
to := h.Atoi(inst[5])
moveCrates(stacks, from, to, count)
}
for i := range stacks {
fmt.Print(string(stacks[i][0]))
}
fmt.Println("")
}
func getCrateHash(stacks [][]byte) string {
var ret []byte
for i := range stacks {
for j := range stacks[i] {
ret = append(ret, stacks[i][j])
}
}
sort.Slice(ret, func(p, q int) bool {
return ret[p] > ret[q]
})
return string(ret)
}
func validateCrates(valid string, stacks [][]byte) bool {
return valid == getCrateHash(stacks)
}
func buildStacks(inp []string) ([][]byte, int) {
var stacks [][]byte
// Find the bottom
var bottom int
for i, row := range inp {
if len(row) > 1 && row[1] == '1' {
bottom = i
break
}
}
// Allocate stacks
stCnt := strings.Fields(inp[bottom])
for i := 0; i < len(stCnt); i++ {
stacks = append(stacks, []byte{})
}
// Build stacks
for i := bottom - 1; i >= 0; i-- {
for stI := 0; stI < len(stCnt); stI++ {
pos := stI*4 + 1
if len(inp[i]) >= pos && (inp[i][pos] >= 'A' && inp[i][pos] <= 'Z') {
stacks[stI] = append([]byte{inp[i][pos]}, stacks[stI]...)
}
}
}
return stacks, bottom
}
func moveCrates(stacks [][]byte, from, to, count int) {
var x []byte
if count > 1 {
x, stacks[from-1] = stacks[from-1][0:count], stacks[from-1][count:]
} else {
x, stacks[from-1] = []byte{stacks[from-1][0]}, stacks[from-1][count:]
}
for i := len(x) - 1; i >= 0; i-- {
stacks[to-1] = append([]byte{x[i]}, stacks[to-1]...)
}
}
func printStacks(stacks [][]byte) {
for i := range stacks {
fmt.Println(i+1, string(stacks[i]))
}
}

161
2022/day05/problem Normal file
View File

@ -0,0 +1,161 @@
Advent of Code
br0xen (AoC++) 10*
--- Day 5: Supply Stacks ---
The expedition can depart as soon as the final supplies have been unloaded from the
ships. Supplies are stored in stacks of marked crates, but because the needed supplies
are buried under many other crates, the crates need to be rearranged.
The ship has a giant cargo crane capable of moving crates between stacks. To ensure none
of the crates get crushed or fall over, the crane operator will rearrange them in a
series of carefully-planned steps. After the crates are rearranged, the desired crates
will be at the top of each stack.
The Elves don't want to interrupt the crane operator during this delicate procedure, but
they forgot to ask her which crate will end up where, and they want to be ready to unload
them as soon as possible so they can embark.
They do, however, have a drawing of the starting stacks of crates and the rearrangement
procedure (your puzzle input). For example:
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2
In this example, there are three stacks of crates. Stack 1 contains two crates: crate Z
is on the bottom, and crate N is on top. Stack 2 contains three crates; from bottom to
top, they are crates M, C, and D. Finally, stack 3 contains a single crate, P.
Then, the rearrangement procedure is given. In each step of the procedure, a quantity of
crates is moved from one stack to a different stack. In the first step of the above
rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this
configuration:
[D]
[N] [C]
[Z] [M] [P]
1 2 3
In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one
at a time, so the first crate to be moved (D) ends up below the second and third crates:
[Z]
[N]
[C] [D]
[M] [P]
1 2 3
Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one
at a time, crate C ends up below crate M:
[Z]
[N]
[M] [D]
[C] [P]
1 2 3
Finally, one crate is moved from stack 1 to stack 2:
[Z]
[N]
[D]
[C] [M] [P]
1 2 3
The Elves just need to know which crate will end up on top of each stack; in this
example, the top crates are C in stack 1, M in stack 2, and Z in stack 3, so you should
combine these together and give the Elves the message CMZ.
After the rearrangement procedure completes, what crate ends up on top of each stack?
Your puzzle answer was PTWLTDSJV.
--- Part Two ---
As you watch the crane operator expertly rearrange the crates, you notice the process
isn't following your prediction.
Some mud was covering the writing on the side of the crane, and you quickly wipe it away.
The crane isn't a CrateMover 9000 - it's a CrateMover 9001.
The CrateMover 9001 is notable for many new and exciting features: air conditioning,
leather seats, an extra cup holder, and the ability to pick up and move multiple crates
at once.
Again considering the example above, the crates begin in the same configuration:
[D]
[N] [C]
[Z] [M] [P]
1 2 3
Moving a single crate from stack 2 to stack 1 behaves the same as before:
[D]
[N] [C]
[Z] [M] [P]
1 2 3
However, the action of moving three crates from stack 1 to stack 3 means that those three
moved crates stay in the same order, resulting in this new configuration:
[D]
[N]
[C] [Z]
[M] [P]
1 2 3
Next, as both crates are moved from stack 2 to stack 1, they retain their order as well:
[D]
[N]
[C] [Z]
[M] [P]
1 2 3
Finally, a single crate is still moved from stack 1 to stack 2, but now it's crate C that
gets moved:
[D]
[N]
[Z]
[M] [C] [P]
1 2 3
In this example, the CrateMover 9001 has put the crates in a totally different order:
MCD.
Before the rearrangement process finishes, update your simulation so that the Elves know
where they should stand to be ready to unload the final supplies. After the rearrangement
procedure completes, what crate ends up on top of each stack?
Your puzzle answer was WZMFVGGZP.
Both parts of this puzzle are complete! They provide two gold stars: **
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2022/about
. https://adventofcode.com/2022/events
. https://adventofcode.com/2022/settings
. https://adventofcode.com/2022/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2022/support
. https://adventofcode.com/2022
. https://adventofcode.com/2022
. https://adventofcode.com/2022/support
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022/leaderboard
. https://adventofcode.com/2022/stats
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022
. https://adventofcode.com/2022/day/5/input

9
2022/day05/testinput Normal file
View File

@ -0,0 +1,9 @@
[D]
[N] [C]
[Z] [M] [P]
1 2 3
move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

1
2022/day06/input Normal file
View File

@ -0,0 +1 @@
mgwwjddqzqdqsstctjjsdjsdsrsfsmfsfwwltwlwhwnhhlffzddgffwlffbsfshfshhgvvdrrltlzlnzznrrnrsnnhgnnfjnnvpnnbjjnwwrcwrrhlhvlhhmzmqzqrqtqmqpmpwwmssgsrgrgtgmtgmtgtdtvdvmvsvsbvsbvbtthmmftmmdnmddcrcvcrrfjfhhfjhffjllcpllmcctjtrttwmtwmwffrlrqlqzzpddsqdqqgjqgjgngwnncjnnvsnswwbzbtzzflzzqsqbsbvbmbnnjpnpnnpfpmpmnpmmjljtltssqnsqslstswtwswwjddvmmzlzqlzqzqjjlttmtrtbtmtgmtmsttrctrrsqrqvvrzrcrhhlnhllbfbtthrhdhllmwlmlgglgsgmgsmszzprpwpfprfftffpssjzjgzjzddqfqmmwqwvwlvlqqtbtwwrwttmsmppbmmpcmctcnnhssnjncnlcnctcjjrzrwrfwfcwffczztrtsrtstlsssljssmvssjzssrqqrcqqwlqwlwffsflssrrzhzzhrzzdgdppspwplpqptttvddggzszccrrnzzwwdwjddrvvwggpvgpvvhdhqddffrnngcncjcjlcchrrftrrjccrcrqqgcglcgcscmmlzmmtcmcffwfcfrcrggdmggdvvnrvnnphnngzzpdpgpspqqgrrnffmfpmffmgfmmjmzztlljlggljjcnnrqnqpnqndnffnwwbpwpjwjjlslmsmtmtjttsvsggrmmdpmmcjjswsqqwfwwrwffczfzggqvggdlldhllsdsfdsdhhmmzmjjmpjpddsccqrrjhjlhjjcnnpwnnffjwwcsszrrnmnsmnnjbnndwnnnhnwwjtwtlwtwqqbnqnbnqqfjfdjdbbwbqwqpqggbcbhhtrtqrrddpdwdlwdddzvzwvvdfdpdcdvdtdpttwwdzdzmdmqmzmnzmnmhmwmjwwshhcqcpcvvzgzdggnjnnhwnhhswwvccqrqlqggcngnmggmffblbglltlstshhrjjlvlppsqslljtjtgglvltvlvmllhrhdrrmqrmqmjjdcjjppqwwllvsvsrszslsvvghvvhmmfbfvfpfmmvdvppwggtrrjvvsbbzffbmffpqqqhnhncclzczwcwpwssrprfrsrbsbnbvnnwzwqqpsqsspmssztssstrtcczsznzvvpvttnssdjdhjddngdgvvmsszbzsbsmbbgsbgsgmmhwhghrhjhphshchgglmlvlhlbhlldwdggdsscvcbcssfbbvggvwwtstltrrwttjdtdvttlsttfhfmhhcbclbcbffqqslshlldhdqhhjwwlffrbrdbrrgcrrfmffbhhlslrslrslrlsrsnsnvvqfqnnfdfmfmttmcmcrrcmmmjttjvtvvjbjqjnnbtbnblbtlblplgltlqltlztzvvtdvvtpvvwdwfflbflfrrhbrbbmjmcjmccztzwwjzwwzwdzdnnwcclbllqgghjhlhthwrdglrmcpbmtrnrdtvjrpmzqmljzzrtpzsrhnjrsdmpnsgdhvqchcfqjqdncjqfnscwjqvszpzzfhpjljmvsqnjzmrsgsbzlvrddtdmwbwwgprlvdfflrpztdzrhtmlzrrtdmpmcprqzzwlnmfjvsrltfjgcnnfllnzmbjcbthvbffczsspmczrpgpdjmvrvfmprfmnqdcnfwwvgdrwvrbtlqmhrrjvtrmmgrlprtnzdlszgbtbwztdrmpmlfblshzcnsczlblgwzrpnlccwhmcqhssmpznbdnnqgzzmjprjttdjhmjbmgqvzblsjwmplzsthrswhsdbvtqgrfzmbpqtpqgqdqcvzlgjrtvrhvzgmcmrwdmfpdvjddsmmsnvrdgnsbsdzcbprbqchqcgnwmfsrmqtrcdhdtzztbvmpblftwqlmlmmjcjhhjlgnnhljnncvbnjhgbjrltlwscswgvqmcnssbcdrtbgnhgmpmvjwtrbrbrdbdqfrncvhdstwztwcpbjrjwzmdlwvlvmsrhghjwjnjstbcqjqtjrgcvhzjdhdgbgdlhvjmztwvhgzzggwwhhhzvtrldchztmwfjvnqnvhnwpfvzzvnlvsccmvsngzgtnttssmdmhwzlhtpnfhczsdfnrstbwvwpqmslcvpvhfzttzhsgzpbhqdtswshljpncznjhzmgvvbcllmzprhrvwljwcjpcdqmwbzvsdcgtmwnrhswsgqhwpwhbjpnhnpjvgsqcjltzrqvqfflcdcvpwnznvtqbfbtlpmtdgbbwdwncqsqnbtgfdzzqzzvjnwmzdmlgstmnjwznjqghglvmwjzlqrnddcqhgndlhlbmqdhrqgrjqztnhpzssnwmrqclmwpgbvfrvgqqvtthznsqwgndjrprbgrhcvhpzbfhdmgnhsrqjvjstbtmnltsbjfzczvjqnhtldqclsflbhvvlzjwrqqgbgpwqwpfjctqpzdqwcfstmwbzgrgrtzngljjnvtggrqcbgjwtqsdgwmfjqppnzgfsfdmlctztbhnntnntdlvrsdvnllvmpggjzspqfhzwrttwzpqrnqjhmpjnmrzrpnqzshcqgctbtflqflcrzpmnphgbbghhwzplljwngbtffwmrwggdztvtfgwldlswqvjptvbfvnbpglhgrdgcfmvrslqldmwjqvjpvwgpjddvglllvpqwvbchqsmjrncgvgmqbsbcwfbsbpqcqzjfpcdzszgmvqgqjlflpfzbsrhsrzrdbpssrjbcfhvztftlzqpsglpwhbscgwdlbgghzsbwznnbgnnsgjghmmpmmrmqmdhnflgvgprqfcbpzbcpjscvnpfrmtvzsbflmffvcfsvdsggzdqtppcjzphcqwrqtrczqmwcdmdqndzmhdpnfqsbndnvjlzrsjzmpcrfgjwccsdtzvslccwhlvzjwjgvwpsnsggmqgsjfbwmjstsgnqmtjhljvfnflnngdrqvscwlqqdsglhghczhjdvgrjcqblmncdbjvsbwgptgpvvzhcjgjnvttrgzrjnqlvfbrmpzdcbbnnrqptpzpssznbsrstdphbgdrsnrhcjwwgsncdzvqfnmnvqcmcgdgjdbqjzdrvvbvhjdfcqndmqwscmsvppclzrhgbldqtwctbdhpbbwfvwpcpsvddmrhqbhlrrmrblnmqqqbwvcwwbwprlmhtdncmjhmjgphmrrhcdrqgmcrzwsznqzpngbtsvjgglrddhjflbrhvqwmmhmqzhphwnvqwzczdvqjsnlhfqbcgddtwgnlcgbfqmzfpqmnbpvfhdhjlnwtrlmggtbfnfvmqrzjvjjvffctsrwgfcpghhnzqmwtlsfhjrvqpwqhngrhpswslsvtgnbvbmwsfwmpntfsfpshrjzvghhpvnlbmnrhltfpmqdwzfhztvhlmbnmhnbvdzbbtczvwbvwtvjghhjjrtgbrqrhmbgvssstdwztdmdsqtctghjhsnpslqttdlvndmjfnmdzwrblfjqcwptfttvlcgsvwcbmfzbdlmrtchgqlfspwznbzfjthjtfwshqgfsfdsmzsmpptzschlzjshvfwtmpszvrvlggbrgpcnqwndhjjprztdfddblhfljbvttfvhchhdfsftrhccrbncmhwpcpwfqthngcqptmvsmpcswdrdlcbqvvhwmcqqwbzlblrgfcrrndwdvlvnpjvwchzjzmgrqhzzmgqqdsdflpclpdtlhvhcthzjfbvjvzsnbvwfsnglvbnwnbgrqwpbgclhjhztttbjwvmlmmgmzncbwswncqhmcfjfnwnpbrmchhpgwngrfwgdfdqmblwlghdjvdhjftdblrtcvvgbvpmbjhfwgpmghqbqrcpgfvhtvqtlbjdblggcpjzlrhpbsqwntfhbhwwszpdlsgbpfqhvrjrhsldcgvqhqmwdfcrcmhrvvwvbrfsrrcvwzhqqvgltlnhwhdrhrdqsvmdzjwgmqdsccwhcgwltfhdfqpsltjccwsttmrc

51
2022/day06/main.go Normal file
View File

@ -0,0 +1,51 @@
package main
import (
"fmt"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToString()
fmt.Println("# Part 1")
fmt.Printf("First Start of Packet @ %d\n", findFirstStartOfPacket(inp))
fmt.Println("")
fmt.Println("# Part 2")
fmt.Printf("First Start of Message @ %d\n", findFirstStartOfMessage(inp))
}
func findFirstStartOfPacket(inp string) int {
return findFirstWithXUnique(inp, 4)
}
func findFirstStartOfMessage(inp string) int {
return findFirstWithXUnique(inp, 14)
}
func findFirstWithXUnique(inp string, x int) int {
for i := range inp {
if i < x {
continue
}
if findUniqueCountBefore(inp, i) >= x {
return i + 1
}
}
return -1
}
func findUniqueCountBefore(inp string, pos int) int {
if len(inp) < pos {
return -1
}
counts := make(map[byte]int)
for i := pos; i >= 0; i-- {
if _, ok := counts[inp[i]]; !ok {
counts[inp[i]]++
} else {
return len(counts)
}
}
return pos
}

112
2022/day06/problem Normal file
View File

@ -0,0 +1,112 @@
Advent of Code
br0xen (AoC++) 12*
{ʼyearʼ:2022}
--- Day 6: Tuning Trouble ---
The preparations are finally complete; you and the Elves leave camp on
foot and begin to make your way toward the star fruit grove.
As you move through the dense undergrowth, one of the Elves gives you a
handheld device. He says that it has many fancy features, but the most
important one to set up right now is the communication system.
However, because he's heard you have significant experience dealing with
signal-based systems, he convinced the other Elves that it would be okay
to give you their one malfunctioning device - surely you'll have no
problem fixing it.
As if inspired by comedic timing, the device emits a few colorful sparks.
To be able to communicate with the Elves, the device needs to lock on to
their signal. The signal is a series of seemingly-random characters that
the device receives one at a time.
To fix the communication system, you need to add a subroutine to the
device that detects a start-of-packet marker in the datastream. In the
protocol being used by the Elves, the start of a packet is indicated by a
sequence of four characters that are all different.
The device will send your subroutine a datastream buffer (your puzzle
input); your subroutine needs to identify the first position where the
four most recently received characters were all different. Specifically,
it needs to report the number of characters from the beginning of the
buffer to the end of the first such four-character marker.
For example, suppose you receive the following datastream buffer:
mjqjpqmgbljsphdztnvjfqwrcgsmlb
After the first three characters (mjq) have been received, there haven't
been enough characters received yet to find the marker. The first time a
marker could occur is after the fourth character is received, making the
most recent four characters mjqj. Because j is repeated, this isn't a
marker.
The first time a marker appears is after the seventh character arrives.
Once it does, the last four characters received are jpqm, which are all
different. In this case, your subroutine should report the value 7,
because the first start-of-packet marker is complete after 7 characters
have been processed.
Here are a few more examples:
 bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 5
 nppdvjthqldpwncqszvftbrmjlhg: first marker after character 6
 nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 10
 zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 11
How many characters need to be processed before the first start-of-packet
marker is detected?
Your puzzle answer was 1544.
--- Part Two ---
Your device's communication system is correctly detecting packets, but
still isn't working. It looks like it also needs to look for messages.
A start-of-message marker is just like a start-of-packet marker, except it
consists of 14 distinct characters rather than 4.
Here are the first positions of start-of-message markers for all of the
above examples:
 mjqjpqmgbljsphdztnvjfqwrcgsmlb: first marker after character 19
 bvwbjplbgvbhsrlpgdmjqwftvncz: first marker after character 23
 nppdvjthqldpwncqszvftbrmjlhg: first marker after character 23
 nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg: first marker after character 29
 zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw: first marker after character 26
How many characters need to be processed before the first start-of-message
marker is detected?
Your puzzle answer was 2145.
Both parts of this puzzle are complete! They provide two gold stars: **
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2022/about
. https://adventofcode.com/2022/events
. https://adventofcode.com/2022/settings
. https://adventofcode.com/2022/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2022/support
. https://adventofcode.com/2022
. https://adventofcode.com/2022
. https://adventofcode.com/2022/support
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022/leaderboard
. https://adventofcode.com/2022/stats
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2016/day/6
. https://adventofcode.com/2016/day/25
. https://adventofcode.com/2019/day/7
. https://adventofcode.com/2019/day/9
. https://adventofcode.com/2019/day/16
. https://adventofcode.com/2021/day/25
. https://adventofcode.com/2022
. https://adventofcode.com/2022/day/6/input

1
2022/day06/testinput Normal file
View File

@ -0,0 +1 @@
mjqjpqmgbljsphdztnvjfqwrcgsmlb

1091
2022/day07/input Normal file

File diff suppressed because it is too large Load Diff

212
2022/day07/main.go Normal file
View File

@ -0,0 +1,212 @@
package main
import (
"errors"
"fmt"
"log"
"sort"
"strings"
h "git.bullercodeworks.com/brian/adventofcode/helpers"
)
func main() {
inp := h.StdinToStringSlice()
part1(inp)
fmt.Println("")
part2(inp)
}
func buildFileSystem(inp []string) *dir {
root := NewDir(nil, []string{}, "/")
cwd := root
for i := range inp {
if i == 0 {
// We've already done this
continue
}
pts := strings.Fields(inp[i])
switch pts[0] {
case "$":
if pts[1] == "cd" {
if pts[2] == ".." {
cwd = cwd.Parent()
} else {
ch, err := cwd.ChangeDir(pts[2])
if err != nil {
log.Fatalf("Failed to change directories: %s/%s, %s", strings.Join(cwd.Path(), "/"), pts[2], err.Error())
}
cwd = ch
}
}
case "dir":
cwd.AddDir(pts[1])
default:
cwd.AddFile(pts[1], h.Atoi(pts[0]))
}
}
return root
}
func part1(inp []string) {
fmt.Println("# Part 1")
root := buildFileSystem(inp)
smallDirs := root.FindDirectoriesMaxSize(100000)
var sum int
for i := range smallDirs {
sum += smallDirs[i].Size()
}
fmt.Printf("Naive Size of Small Dirs: %d\n", sum)
}
func part2(inp []string) {
fmt.Println("# Part 2")
root := buildFileSystem(inp)
total := 70000000
required := 30000000
used := root.Size()
needed := required - (total - used)
dir := root.FindSmallestDirectoryAtLeast(needed)
if dir == nil {
log.Fatal("No suitable directories found")
}
fmt.Printf("Disk Space %d / %d. Need %d more for update.\n", (total - used), total, needed)
fmt.Println("Auto-deleting smallest directory that will permit update... Don't panic.")
fmt.Printf("Freed space by deleting %s: %d\n", dir.FullPath(), dir.Size())
}
const (
TpFile = iota
TpDir
)
type node interface {
Parent() *dir
Path() []string
Name() string
Type() int
Size() int
FullPath() []string
Print(depth int)
}
type file struct {
parent *dir
path []string
name string
size int
}
func NewFile(parent *dir, path []string, nm string, size int) *file {
return &file{
parent: parent,
path: path,
name: nm,
size: size,
}
}
func (f file) Parent() *dir { return f.parent }
func (f file) Path() []string { return f.path }
func (f file) Name() string { return f.name }
func (f file) Type() int { return TpFile }
func (f file) Size() int { return f.size }
func (f file) FullPath() []string { return append(f.path, f.name) }
func (f file) Print(depth int) {
fmt.Printf("%s- %s (file, size=%d)\n", strings.Repeat(" ", depth), f.Name(), f.Size())
}
type dir struct {
parent *dir
path []string
name string
contents map[string]node
contentsList []string
}
func NewDir(parent *dir, path []string, name string) *dir {
return &dir{
parent: parent,
path: path,
name: name,
contents: make(map[string]node),
}
}
func (d dir) Parent() *dir { return d.parent }
func (d dir) Path() []string { return d.path }
func (d dir) Name() string { return d.name }
func (d dir) Type() int { return TpDir }
func (d dir) Size() int {
var size int
for i := range d.contents {
size += d.contents[i].Size()
}
return size
}
func (d dir) FullPath() []string { return append(d.path, d.name) }
func (d *dir) ChangeDir(to string) (*dir, error) {
if v, ok := d.contents[to]; !ok {
return nil, errors.New("Directory not found")
} else {
switch d := v.(type) {
case *dir:
return d, nil
default:
return nil, errors.New("Not a directory")
}
}
}
func (d *dir) AddFile(nm string, size int) {
d.contents[nm] = NewFile(d, d.FullPath(), nm, size)
d.contentsList = append(d.contentsList, nm)
sort.Strings(d.contentsList)
}
func (d *dir) AddDir(nm string) {
d.contents[nm] = NewDir(d, d.FullPath(), nm)
d.contentsList = append(d.contentsList, nm)
sort.Strings(d.contentsList)
}
func (d dir) Print(depth int) {
fmt.Printf("%s- %s (dir)\n", strings.Repeat(" ", depth), d.Name())
for _, nm := range d.contentsList {
n, ok := d.contents[nm]
if !ok {
log.Fatalf("Error printing directory %s: %s not found.", d.FullPath(), nm)
}
n.Print(depth + 1)
}
}
func (d *dir) FreeSpace(driveSize int) int { return driveSize - d.Size() }
func (d *dir) FindDirectoriesMaxSize(size int) []*dir {
var ret []*dir
for _, node := range d.contents {
switch child := node.(type) {
case *dir:
if child.Size() <= size {
ret = append(ret, child)
}
ret = append(ret, child.FindDirectoriesMaxSize(size)...)
}
}
return ret
}
func (d *dir) FindSmallestDirectoryAtLeast(needed int) *dir {
var ret *dir
retSize := d.Size()
for _, node := range d.contents {
switch child := node.(type) {
case *dir:
sz := child.Size()
if sz >= needed && sz < retSize {
ret = child
retSize = sz
rec := child.FindSmallestDirectoryAtLeast(needed)
if rec != nil {
ret = rec
retSize = rec.Size()
}
}
}
}
return ret
}

151
2022/day07/problem Normal file
View File

@ -0,0 +1,151 @@
Advent of Code
br0xen (AoC++) 14*
--- Day 7: No Space Left On Device ---
You can hear birds chirping and raindrops hitting leaves as the expedition proceeds. Occasionally, you can even hear
much louder sounds in the distance; how big do the animals get out here, anyway?
The device the Elves gave you has problems with more than just its communication system. You try to run a system update:
$ system-update --please --pretty-please-with-sugar-on-top
Error: No space left on device
Perhaps you can delete some files to make space for the update?
You browse around the filesystem to assess the situation and save the resulting terminal output (your puzzle input). For
example:
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k
The filesystem consists of a tree of files (plain data) and directories (which can contain other directories or files).
The outermost directory is called /. You can navigate around the filesystem, moving into or out of directories and
listing the contents of the directory you're currently in.
Within the terminal output, lines that begin with $ are commands you executed, very much like some modern computers:
 cd means change directory. This changes which directory is the current directory, but the specific result depends on
the argument:
 cd x moves in one level: it looks in the current directory for the directory named x and makes it the current
directory.
 cd .. moves out one level: it finds the directory that contains the current directory, then makes that
directory the current directory.
 cd / switches the current directory to the outermost directory, /.
 ls means list. It prints out all of the files and directories immediately contained by the current directory:
 123 abc means that the current directory contains a file named abc with size 123.
 dir xyz means that the current directory contains a directory named xyz.
Given the commands and output in the example above, you can determine that the filesystem looks visually like this:
- / (dir)
- a (dir)
- e (dir)
- i (file, size=584)
- f (file, size=29116)
- g (file, size=2557)
- h.lst (file, size=62596)
- b.txt (file, size=14848514)
- c.dat (file, size=8504156)
- d (dir)
- j (file, size=4060174)
- d.log (file, size=8033020)
- d.ext (file, size=5626152)
- k (file, size=7214296)
Here, there are four directories: / (the outermost directory), a and d (which are in /), and e (which is in a). These
directories also contain files of various sizes.
Since the disk is full, your first step should probably be to find directories that are good candidates for deletion. To
do this, you need to determine the total size of each directory. The total size of a directory is the sum of the sizes
of the files it contains, directly or indirectly. (Directories themselves do not count as having any intrinsic size.)
The total sizes of the directories above can be found as follows:
 The total size of directory e is 584 because it contains a single file i of size 584 and no other directories.
 The directory a has total size 94853 because it contains files f (size 29116), g (size 2557), and h.lst (size
62596), plus file i indirectly (a contains e which contains i).
 Directory d has total size 24933642.
 As the outermost directory, / contains every file. Its total size is 48381165, the sum of the size of every file.
To begin, find all of the directories with a total size of at most 100000, then calculate the sum of their total sizes.
In the example above, these directories are a and e; the sum of their total sizes is 95437 (94853 + 584). (As in this
example, this process can count files more than once!)
Find all of the directories with a total size of at most 100000. What is the sum of the total sizes of those
directories?
Your puzzle answer was 1642503.
--- Part Two ---
Now, you're ready to choose a directory to delete.
The total disk space available to the filesystem is 70000000. To run the update, you need unused space of at least
30000000. You need to find a directory you can delete that will free up enough space to run the update.
In the example above, the total size of the outermost directory (and thus the total amount of used space) is 48381165;
this means that the size of the unused space must currently be 21618835, which isn't quite the 30000000 required by the
update. Therefore, the update still requires a directory with total size of at least 8381165 to be deleted before it can
run.
To achieve this, you have the following options:
 Delete directory e, which would increase unused space by 584.
 Delete directory a, which would increase unused space by 94853.
 Delete directory d, which would increase unused space by 24933642.
 Delete directory /, which would increase unused space by 48381165.
Directories e and a are both too small; deleting them would not free up enough space. However, directories d and / are
both big enough! Between these, choose the smallest: d, increasing unused space by 24933642.
Find the smallest directory that, if deleted, would free up enough space on the filesystem to run the update. What is
the total size of that directory?
Your puzzle answer was 6999588.
Both parts of this puzzle are complete! They provide two gold stars: **
References
Visible links
. https://adventofcode.com/
. https://adventofcode.com/2022/about
. https://adventofcode.com/2022/events
. https://adventofcode.com/2022/settings
. https://adventofcode.com/2022/auth/logout
. Advent of Code Supporter
https://adventofcode.com/2022/support
. https://adventofcode.com/2022
. https://adventofcode.com/2022
. https://adventofcode.com/2022/support
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022/leaderboard
. https://adventofcode.com/2022/stats
. https://adventofcode.com/2022/sponsors
. https://adventofcode.com/2022
. https://adventofcode.com/2022/day/7/input

23
2022/day07/testinput Normal file
View File

@ -0,0 +1,23 @@
$ cd /
$ ls
dir a
14848514 b.txt
8504156 c.dat
dir d
$ cd a
$ ls
dir e
29116 f
2557 g
62596 h.lst
$ cd e
$ ls
584 i
$ cd ..
$ cd ..
$ cd d
$ ls
4060174 j
8033020 d.log
5626152 d.ext
7214296 k

View File

@ -148,7 +148,7 @@ func Atoi(i string) int {
var ret int
var err error
if ret, err = strconv.Atoi(i); err != nil {
log.Fatal("Invalid Atoi:", i)
log.Fatalf("Invalid Atoi: %s\n%v", i, err)
}
return ret
}