Add code generator for 2021 and update README

This commit is contained in:
2021-12-04 18:07:25 +00:00
parent f6862dc302
commit ec04a04547
2 changed files with 147 additions and 1 deletions

View File

@@ -1,3 +1,56 @@
# Advent of Code 2021
The 2021 iteration of Advent of Code. This year I am going to try and accomplish it all on my iPad using GitPod
The 2021 iteration of Advent of Code. This year I am going to try and accomplish it all on my iPad using GitPod
## Adding a new day
When starting to solve for a new day run the following command in the root of the repo where `<day>` is the go lang compatible package name (i.e. `four` or `five`) and `<noun>` represents the item the day's problem revolves around (i.e. `radar` or `bingo`).
```sh
./setup.sh <day> <noun>
```
After generating a new package update [`main.go`](main.go) to include a `<day>` flag in the switch statement on line 28
```go
case "<day>":
day = <day>.Init("<day>/input.txt")
```
## My Solutions
### Day 1
```sh
$ ./aoc2021 --one
The solution for "one" is:
There were 1715 increases in depth
There were 1739 increases in depth when sampling a window
```
### Day 2
```sh
$ ./aoc2021 --two
The solution for "two" is:
Horizontal is 1832, depth is 1172, answer is 2147104
Horizontal is 1832, depth is 1116059, answer is 2044620088
```
### Day 3
```sh
$ ./aoc2021 --three
The solution for "three" is:
The power rates are gamma: 1143, epsilon: 2952, for 3374136 total
The oxygen rating is 1909 and CO2 rating is 2322, for a life support rating of 4432698
```
### Day 4
```sh
$ ./aoc2021 --four
The solution for "four" is:
The winning score is 668 on call 66. Final score 44088
The last winning score is 263 on call 90. Final score 23670
```

93
setup.sh Executable file
View File

@@ -0,0 +1,93 @@
#!/bin/bash
mkdir $1
name=$(echo $1 | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2)) }')
abbr=$(echo $2 | awk '{print substr($0,1,1) }')
# Create day implementation
echo "package $1
type $name struct {
$2 $2
}
func Init(filepath string) *$name {
$1 := &$name{
$2: $2{},
}
$1.$2.load(filepath)
return $1
}
func (d *$name) Answer() string {
return \"$1 part 1 answer for $2\"
}
func (d *$name) FollowUp() string {
return \"$1 part 2 answer for $2\"
}
" >> $1/main.go
# Create file for the day
echo "package $1
import (
\"bufio\"
\"os\"
)
type $2 struct {
input []string
}
func ($abbr *$2) load(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
$abbr.input = append($abbr.input, scanner.Text())
}
return nil
}
" >> $1/$2.go
# Create the test file
echo "package $1
import \"testing\"
func Test_read(t *testing.T) {
$abbr := $2{}
if err := $abbr.load(\"test_input.txt\"); err != nil {
t.Log(err)
t.FailNow()
}
if len($abbr.input) != 1000 {
t.Logf(\"Expected 1000 inputs, found %d\", len($abbr.input))
t.Fail()
}
}
" >> $1/$2_test.go
# Create files for test input
touch $1/test_input.txt
touch $1/input.txt
# Append Solution to Readme
echo "
### Day $1
\`\`\`sh
$ ./aoc2021 --$1
The solution for \"$1\" is:
$1 part 1 answer for $2
$1 part 2 answer for $2
\`\`\`
" >> README.md