Completed both parts of Day 1 puzzle
parent
7966166942
commit
aa7a4df79b
@ -0,0 +1,111 @@
|
|||||||
|
package day1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func calibrate(lines []string) int {
|
||||||
|
result := 0
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
first, second := -1, -1
|
||||||
|
|
||||||
|
for _, c := range line {
|
||||||
|
if c >= '1' && c <= '9' {
|
||||||
|
|
||||||
|
val, _ := strconv.Atoi(string(c))
|
||||||
|
|
||||||
|
if first == -1 {
|
||||||
|
first = int(val)
|
||||||
|
} else {
|
||||||
|
second = int(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if second == -1 {
|
||||||
|
second = first
|
||||||
|
}
|
||||||
|
|
||||||
|
result += first*10 + second
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func calibrate2(lines []string) int {
|
||||||
|
result := 0
|
||||||
|
|
||||||
|
for _, line := range lines {
|
||||||
|
first, second := -1, -1
|
||||||
|
|
||||||
|
for i := 0; i < len(line); i++ {
|
||||||
|
c := line[i]
|
||||||
|
if c >= '1' && c <= '9' {
|
||||||
|
|
||||||
|
val, _ := strconv.Atoi(string(c))
|
||||||
|
|
||||||
|
if first == -1 {
|
||||||
|
first = int(val)
|
||||||
|
} else {
|
||||||
|
second = int(val)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// parse one, two, three, four, five, six, seven, eight, and nine
|
||||||
|
val := -1
|
||||||
|
|
||||||
|
switch c {
|
||||||
|
case 'o':
|
||||||
|
if i+2 < len(line) && line[i+1] == 'n' && line[i+2] == 'e' {
|
||||||
|
val = 1
|
||||||
|
}
|
||||||
|
case 't':
|
||||||
|
if i+2 < len(line) && line[i+1] == 'w' && line[i+2] == 'o' {
|
||||||
|
val = 2
|
||||||
|
} else if i+4 < len(line) && line[i+1] == 'h' && line[i+2] == 'r' && line[i+3] == 'e' && line[i+4] == 'e' {
|
||||||
|
val = 3
|
||||||
|
}
|
||||||
|
case 'f':
|
||||||
|
if i+3 < len(line) && line[i+1] == 'i' && line[i+2] == 'v' && line[i+3] == 'e' {
|
||||||
|
val = 5
|
||||||
|
} else if i+3 < len(line) && line[i+1] == 'o' && line[i+2] == 'u' && line[i+3] == 'r' {
|
||||||
|
val = 4
|
||||||
|
}
|
||||||
|
case 's':
|
||||||
|
if i+4 < len(line) && line[i+1] == 'e' && line[i+2] == 'v' && line[i+3] == 'e' && line[i+4] == 'n' {
|
||||||
|
val = 7
|
||||||
|
} else if i+2 < len(line) && line[i+1] == 'i' && line[i+2] == 'x' {
|
||||||
|
val = 6
|
||||||
|
}
|
||||||
|
case 'e':
|
||||||
|
if i+4 < len(line) && line[i+1] == 'i' && line[i+2] == 'g' && line[i+3] == 'h' && line[i+4] == 't' {
|
||||||
|
val = 8
|
||||||
|
}
|
||||||
|
case 'n':
|
||||||
|
if i+3 < len(line) && line[i+1] == 'i' && line[i+2] == 'n' && line[i+3] == 'e' {
|
||||||
|
val = 9
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if val > -1 {
|
||||||
|
if first == -1 {
|
||||||
|
first = int(val)
|
||||||
|
} else {
|
||||||
|
second = int(val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if second == -1 {
|
||||||
|
second = first
|
||||||
|
}
|
||||||
|
|
||||||
|
if first > -1 {
|
||||||
|
lineVal := first*10 + second
|
||||||
|
result += lineVal
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
@ -0,0 +1,70 @@
|
|||||||
|
package day1
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"gitea.paas.celticinfo.fr/oabrivard/aoc2023/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCalibrate(t *testing.T) {
|
||||||
|
lines := []string{"1abc2", "pqr3stu8vwx", "a1b2c3d4e5f", "treb7uchet"}
|
||||||
|
|
||||||
|
result := calibrate(lines)
|
||||||
|
|
||||||
|
if result != 142 {
|
||||||
|
t.Fatalf("expected 142, got %d", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalibrateWithInput(t *testing.T) {
|
||||||
|
lines := utils.ReadLines("input.txt")
|
||||||
|
result := calibrate(lines)
|
||||||
|
|
||||||
|
if result != 53921 {
|
||||||
|
t.Fatalf("expected 53921, got %d", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalibrate2(t *testing.T) {
|
||||||
|
lines := strings.Split(`two1nine
|
||||||
|
eightwothree
|
||||||
|
abcone2threexyz
|
||||||
|
xtwone3four
|
||||||
|
4nineeightseven2
|
||||||
|
zoneight234
|
||||||
|
7pqrstsixteen`, "\n")
|
||||||
|
|
||||||
|
result := calibrate2(lines)
|
||||||
|
|
||||||
|
if result != 281 {
|
||||||
|
t.Fatalf("expected 281, got %d", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalibrate2tmp(t *testing.T) {
|
||||||
|
lines := strings.Split(`one
|
||||||
|
two
|
||||||
|
three
|
||||||
|
four
|
||||||
|
five
|
||||||
|
six
|
||||||
|
seven
|
||||||
|
eight
|
||||||
|
nine`, "\n")
|
||||||
|
|
||||||
|
result := calibrate2(lines)
|
||||||
|
|
||||||
|
if result != 495 {
|
||||||
|
t.Fatalf("expected 495, got %d", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCalibrate2WithInput(t *testing.T) {
|
||||||
|
lines := utils.ReadLines("input.txt")
|
||||||
|
result := calibrate2(lines)
|
||||||
|
|
||||||
|
if result != 54676 {
|
||||||
|
t.Fatalf("expected 54676, got %d", result)
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,3 @@
|
|||||||
|
module gitea.paas.celticinfo.fr/oabrivard/aoc2023
|
||||||
|
|
||||||
|
go 1.21.4
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
package utils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ReadLines(fileName string) []string {
|
||||||
|
result := []string{}
|
||||||
|
file, err := os.Open(fileName)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
result = append(result, scanner.Text())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := scanner.Err(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue