diff --git a/01/main.go b/01/main.go new file mode 100644 index 0000000..ed01b5f --- /dev/null +++ b/01/main.go @@ -0,0 +1,118 @@ +package main + +import ( + "bufio" + "fmt" + "os" + "time" +) + +func lookahead(line string, i, peek int) string { + if len(line) > i+peek-1 { + return line[i : i+peek] + } + return "" +} + +func main() { + start := time.Now() + partOne() + duration := time.Since(start) + partTwo() + duration2 := time.Since(start) + fmt.Printf("p1: %s, p2: %s\n", duration, duration2-duration) +} + +func makeScanner(test bool) *bufio.Scanner { + var f *os.File + if test { + f, _ = os.Open("inputs/testinput") + } else { + f, _ = os.Open("inputs/input") + } + reader := bufio.NewReader(f) + return bufio.NewScanner(reader) +} + +func partOne() { + scanner := makeScanner(false) + + sum := 0 + for scanner.Scan() { + line := scanner.Text() + var first, last rune + for _, c := range line { + switch c { + case '1', '2', '3', '4', '5', '6', '7', '8', '9': + if first == 0 { + first = c + } + last = c + } + } + sum += 10*(int(first)-48) + int(last) - 48 // direct ASCII translation + } + fmt.Println(sum) +} + +func partTwo() { + scanner := makeScanner(false) + + sum := 0 + for scanner.Scan() { + line := scanner.Text() + var first, last rune + for i, c := range line { + switch { + case c == '1' || lookahead(line, i, 3) == "one": + if first == 0 { + first = '1' + } + last = '1' + case c == '2' || lookahead(line, i, 3) == "two": + if first == 0 { + first = '2' + } + last = '2' + case c == '3' || lookahead(line, i, 5) == "three": + if first == 0 { + first = '3' + } + last = '3' + + case c == '4' || lookahead(line, i, 4) == "four": + if first == 0 { + first = '4' + } + last = '4' + case c == '5' || lookahead(line, i, 4) == "five": + if first == 0 { + first = '5' + } + last = '5' + case c == '6' || lookahead(line, i, 3) == "six": + if first == 0 { + first = '6' + } + last = '6' + case c == '7' || lookahead(line, i, 5) == "seven": + if first == 0 { + first = '7' + } + last = '7' + case c == '8' || lookahead(line, i, 5) == "eight": + if first == 0 { + first = '8' + } + last = '8' + case c == '9' || lookahead(line, i, 4) == "nine": + if first == 0 { + first = '9' + } + last = '9' + } + } + sum += 10*(int(first)-48) + int(last) - 48 + } + fmt.Println(sum) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..a3f7ee9 --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module git.yetaga.in/alazyreader/AdventOfCode2023 diff --git a/new.sh b/new.sh old mode 100644 new mode 100755 diff --git a/readme.md b/readme.md index f59c1fc..7c4fddb 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,5 @@ # AOC 2023 -In Chez Scheme? Perhaps. Go as a fallback. +In Chicken Scheme? Perhaps. Go as a fallback. diff --git a/run.sh b/run.sh old mode 100644 new mode 100755