Initial commit
This commit is contained in:
commit
d7a3348bfc
8 changed files with 1224 additions and 0 deletions
1000
2023/day-1/files/input
Normal file
1000
2023/day-1/files/input
Normal file
File diff suppressed because it is too large
Load diff
49
2023/day-1/part-1/main.go
Normal file
49
2023/day-1/part-1/main.go
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
file, err := os.Open("2023/day-1/files/input")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
fmt.Printf("Sum total: %d\n", calculateSumCalibrationValues(file))
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateSumCalibrationValues(r io.Reader) int {
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
|
||||||
|
sum := 0
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
sum = sum + calibrationValue(line)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func calibrationValue(line string) int {
|
||||||
|
var digits []int
|
||||||
|
|
||||||
|
for _, r := range []rune(line) {
|
||||||
|
if (r >= '0') && (r <= '9') {
|
||||||
|
digits = append(digits, int(r - '0'))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(digits) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return (10*digits[0]) + digits[len(digits)-1]
|
||||||
|
}
|
24
2023/day-1/part-1/main_test.go
Normal file
24
2023/day-1/part-1/main_test.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCalculateSumCalibrationValues(t *testing.T) {
|
||||||
|
testStr := `
|
||||||
|
1abc2
|
||||||
|
pqr3stu8vwx
|
||||||
|
a1b2c3d4e5f
|
||||||
|
treb7uchet
|
||||||
|
`
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer([]byte(testStr))
|
||||||
|
|
||||||
|
want := 142
|
||||||
|
got := calculateSumCalibrationValues(buf)
|
||||||
|
|
||||||
|
if want != got {
|
||||||
|
t.Errorf("Unexpected value returned from calculateSumCalibrationValues(); want %d, got %d", want, got)
|
||||||
|
}
|
||||||
|
}
|
93
2023/day-1/part-2/main.go
Normal file
93
2023/day-1/part-2/main.go
Normal file
|
@ -0,0 +1,93 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
one = "one"
|
||||||
|
two = "two"
|
||||||
|
three = "three"
|
||||||
|
four = "four"
|
||||||
|
five = "five"
|
||||||
|
six = "six"
|
||||||
|
seven = "seven"
|
||||||
|
eight = "eight"
|
||||||
|
nine = "nine"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
file, err := os.Open("2023/day-1/files/input")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("ERROR: %v\n", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
fmt.Printf("Sum total: %d\n", calculateSumCalibrationValues(file))
|
||||||
|
}
|
||||||
|
|
||||||
|
func calculateSumCalibrationValues(r io.Reader) int {
|
||||||
|
scanner := bufio.NewScanner(r)
|
||||||
|
|
||||||
|
var (
|
||||||
|
digits []int
|
||||||
|
sum int
|
||||||
|
)
|
||||||
|
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := scanner.Text()
|
||||||
|
sum = sum + calibrationValue(line, digits)
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func calibrationValue(line string, digits []int) int {
|
||||||
|
if len(line) > 0 {
|
||||||
|
switch {
|
||||||
|
case (line[0] >= '0') && (line[0] <= '9'):
|
||||||
|
digits = append(digits, int(line[0]-'0'))
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, one):
|
||||||
|
digits = append(digits, 1)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, two):
|
||||||
|
digits = append(digits, 2)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, three):
|
||||||
|
digits = append(digits, 3)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, four):
|
||||||
|
digits = append(digits, 4)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, five):
|
||||||
|
digits = append(digits, 5)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, six):
|
||||||
|
digits = append(digits, 6)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, seven):
|
||||||
|
digits = append(digits, 7)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, eight):
|
||||||
|
digits = append(digits, 8)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
case strings.HasPrefix(line, nine):
|
||||||
|
digits = append(digits, 9)
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
default:
|
||||||
|
return calibrationValue(line[1:], digits)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(digits) == 0 {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return (10*digits[0] + digits[len(digits)-1])
|
||||||
|
}
|
27
2023/day-1/part-2/main_test.go
Normal file
27
2023/day-1/part-2/main_test.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCalculateSumCalibrationValues(t *testing.T) {
|
||||||
|
testStr := `
|
||||||
|
two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen
|
||||||
|
`
|
||||||
|
|
||||||
|
buf := bytes.NewBuffer([]byte(testStr))
|
||||||
|
|
||||||
|
want := 281
|
||||||
|
got := calculateSumCalibrationValues(buf)
|
||||||
|
|
||||||
|
if want != got {
|
||||||
|
t.Errorf("Unexpected value returned from calculateSumCalibrationValues(); want %d, got %d", want, got)
|
||||||
|
}
|
||||||
|
}
|
20
LICENSE
Normal file
20
LICENSE
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2023 Dan Anglin
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||||
|
software and associated documentation files (the “Software”), to deal in the Software
|
||||||
|
without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||||
|
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||||
|
to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all copies or
|
||||||
|
substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||||
|
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||||
|
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||||
|
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
8
README.asciidoc
Normal file
8
README.asciidoc
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
= Advent of Code
|
||||||
|
|
||||||
|
My solutions to the https://adventofcode.com[Advent of Code] challenges.
|
||||||
|
|
||||||
|
== Mirrors
|
||||||
|
|
||||||
|
**Code Flow (Forgejo)**: https://codeflow.dananglin.me.uk/apollo/advent-of-code
|
||||||
|
**GitHub**: https://github.com/dananglin/advent-of-code
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
module codeflow.dananglin.me.uk/apollo/advent-of-code
|
||||||
|
|
||||||
|
go 1.21.4
|
Loading…
Reference in a new issue