AdventOfCode – 2023 – Challenge #15 min read
Recently, I got to know about the Advent Of Code challenge which happens on every december before everyone could go on a long vacation leaves and I found it to be really interesting because the questions are unique and so interesting to work upon. So I thought of doing a bit of solving on those challenges and sharing it as well.
Here is the advent of code homepage and it right away shows the challenges of 2023, but you can still navigate to older problems as well. I can see this is happenening from 2015. Once you have the challenge, click on the input puzzle link to get the actual input file (download it any format as needed) and save it. You can also find the Answer text box where you need to punch in the final puzzle answer to get a star for your efforts.
Try the challenge for yourself it will be fun for sure. Need some help, get a peek at logic here 😉
Here goes the question:
— Day 1: Trebuchet?! —
Something is wrong with global snow production, and you’ve been selected to take a look. The Elves have even given you a map; on it, they’ve used stars to mark the top fifty locations that are likely to be having problems.
You’ve been doing this long enough to know that to restore snow operations, you need to check 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!
You try to ask why they can’t just use a weather machine (“not powerful enough”) and where they’re even sending you (“the sky”) and why your map looks mostly blank (“you sure ask a lot of questions”) and hang on did you just say the sky (“of course, where do you think snow comes from”) when you realize that the Elves are already loading you into a trebuchet (“please hold still, we need to strap you in”).
As they’re making the final adjustments, they discover that their calibration document (your puzzle input) has been amended by a very young Elf who was apparently just excited to show off her art skills. Consequently, the Elves are having trouble reading the values on the document.
The newly-improved calibration document consists of lines of text; each line originally contained a specific calibration value that the Elves now need to recover. On each line, the calibration value can be found by combining the first digit and the last digit (in that order) to form a single two-digit number.
For example:
1abc2
pqr3stu8vwx
a1b2c3d4e5f
treb7uchet
In this example, the calibration values of these four lines are 12
, 38
, 15
, and 77
. Adding these together produces 142
.
Consider your entire calibration document. What is the sum of all of the calibration values?
Part 1 to the solution (literally updated after submitted the part 1 solution 😀 )
This is an straight forward one to take up, you just have to pick up the first digit and last digit from the string and convert it to to number. Finally concat two number and give a final sum of the numbers.
package main
import (
"bufio"
"fmt"
"os"
"unicode"
)
func calculateCalibrationValue(line string) int {
var firstDigit, lastDigit rune
for _, char := range line {
if unicode.IsDigit(char) {
if firstDigit == 0 {
firstDigit = char
}
lastDigit = char
}
}
if firstDigit == 0 || lastDigit == 0 {
return 0
}
firstDigitInt := int(firstDigit - '0')
lastDigitInt := int(lastDigit - '0')
calibrationValue := firstDigitInt*10 + lastDigitInt
return calibrationValue
}
func main() {
file, err := os.Open("inputs.txt")
if err != nil {
fmt.Println("Error opening file:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
sum := 0
for scanner.Scan() {
line := scanner.Text()
calibrationValue := calculateCalibrationValue(line)
sum += calibrationValue
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading file:", err)
return
}
fmt.Println("Sum of Calibration Values:", sum)
}
Once you solved, you will receive a star and indeed, part 2 of the same question opens up.
Part 2 of the question
— Part Two —
Your calculation isn’t quite right. It looks like some of the digits are actually spelled out with letters: one
, two
, three
, four
, five
, six
, seven
, eight
, and nine
also count as valid “digits”.
Equipped with this new information, you now need to find the real first and last digit on each line. For example:
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
In this example, the calibration values are 29
, 83
, 13
, 24
, 42
, 14
, and 76
. Adding these together produces 281
.
What is the sum of all of the calibration values?
Walkthrough:
Here is the trick is to find and keep track of the numbers which we found(Could be world or just number) and concat like in above answer.
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
type Match struct {
Index int
Value int
}
func main() {
f, err := os.Open("./aoc/aoc1.1/input.txt")
if err != nil {
fmt.Println("File open error:", err)
}
defer f.Close()
finalSum := 0
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
findFirst := Match{Index: len(line)}
findLast := Match{Index: -1}
if i := strings.IndexAny(line, "123456789"); i != -1 && i < findFirst.Index {
findFirst = Match{
Index: i,
Value: int(line[i] - '0'),
}
}
if i := strings.LastIndexAny(line, "123456789"); i != -1 && i > findLast.Index {
findLast = Match{
Index: i,
Value: int(line[i] - '0'),
}
}
for i, digit := range []string{"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"} {
if j := strings.Index(line, digit); j != -1 && j < findFirst.Index {
findFirst = Match{
Index: j,
Value: i + 1,
}
}
if j := strings.LastIndex(line, digit); j != -1 && j > findLast.Index {
findLast = Match{
Index: j,
Value: i + 1,
}
}
}
finalSum += findFirst.Value*10 + findLast.Value
}
fmt.Println(finalSum)
}
Have you guys heard about the Advent of Code before? Certainly I haven’t and day 1 is certainly a interesting one, it took lot of time to get the solution correct. It will certainly test your problem solving skills. Have fun!!