Add 2021-02 in Python and Go
This commit is contained in:
parent
c551f3fad1
commit
05fbcd0ce0
6 changed files with 207 additions and 0 deletions
2
challenges/2021/02-dive/README.md
Normal file
2
challenges/2021/02-dive/README.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
# [Day 2: Dive!](https://adventofcode.com/2021/day/2)
|
||||
|
23
challenges/2021/02-dive/benchmark.txt
Normal file
23
challenges/2021/02-dive/benchmark.txt
Normal file
|
@ -0,0 +1,23 @@
|
|||
Day 2 (Dive) benchmark
|
||||
|
||||
Dir: challenges/2021/02-dive
|
||||
Runs per part: 500
|
||||
--------------------------------------------------------------------------------
|
||||
Golang
|
||||
|
||||
benchmark.part.1.avg: 0.000200 seconds
|
||||
benchmark.part.1.min: 0.000111 seconds
|
||||
benchmark.part.1.max: 0.004197 seconds
|
||||
benchmark.part.2.avg: 0.000204 seconds
|
||||
benchmark.part.2.min: 0.000112 seconds
|
||||
benchmark.part.2.max: 0.003940 seconds
|
||||
--------------------------------------------------------------------------------
|
||||
Python
|
||||
|
||||
benchmark.part.1.avg: 0.000851 seconds
|
||||
benchmark.part.1.min: 0.000526 seconds
|
||||
benchmark.part.1.max: 0.007797 seconds
|
||||
benchmark.part.2.avg: 0.000865 seconds
|
||||
benchmark.part.2.min: 0.000549 seconds
|
||||
benchmark.part.2.max: 0.004964 seconds
|
||||
--------------------------------------------------------------------------------
|
98
challenges/2021/02-dive/go/challenge.go
Normal file
98
challenges/2021/02-dive/go/challenge.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
package challenge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/codemicro/adventOfCode/lib/aocgo"
|
||||
)
|
||||
|
||||
const (
|
||||
FORWARD = "forward"
|
||||
DOWN = "down"
|
||||
UP = "up"
|
||||
)
|
||||
|
||||
type instruction struct {
|
||||
Direction string
|
||||
Magnitude int
|
||||
}
|
||||
|
||||
func parse(instr string) ([]*instruction, error) {
|
||||
var o []*instruction
|
||||
for _, line := range strings.Split(strings.TrimSpace(instr), "\n") {
|
||||
splitLine := strings.Split(line, " ")
|
||||
if len(splitLine) != 2 {
|
||||
return nil, fmt.Errorf("malformed instruction %#v", line)
|
||||
}
|
||||
|
||||
magnitudeInt, err := strconv.Atoi(splitLine[1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
o = append(o, &instruction{
|
||||
Direction: splitLine[0],
|
||||
Magnitude: magnitudeInt,
|
||||
})
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
|
||||
type Challenge struct {
|
||||
aocgo.BaseChallenge
|
||||
}
|
||||
|
||||
func (c Challenge) One(instr string) (interface{}, error) {
|
||||
var depth, horizontal int
|
||||
|
||||
instructions, err := parse(instr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, instruction := range instructions {
|
||||
|
||||
switch instruction.Direction {
|
||||
case FORWARD:
|
||||
horizontal += instruction.Magnitude
|
||||
case UP:
|
||||
depth -= instruction.Magnitude
|
||||
case DOWN:
|
||||
depth += instruction.Magnitude
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown direction %#v", instruction.Direction)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return depth * horizontal, nil
|
||||
}
|
||||
|
||||
func (c Challenge) Two(instr string) (interface{}, error) {
|
||||
var depth, horizontal, aim int
|
||||
|
||||
instructions, err := parse(instr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, instruction := range instructions {
|
||||
|
||||
switch instruction.Direction {
|
||||
case FORWARD:
|
||||
horizontal += instruction.Magnitude
|
||||
depth += instruction.Magnitude * aim
|
||||
case UP:
|
||||
aim -= instruction.Magnitude
|
||||
case DOWN:
|
||||
aim += instruction.Magnitude
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown direction %#v", instruction.Direction)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return depth * horizontal, nil
|
||||
}
|
17
challenges/2021/02-dive/info.json
Normal file
17
challenges/2021/02-dive/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "forward 5\ndown 5\nforward 8\nup 3\ndown 8\nforward 2\n",
|
||||
"expected": "150"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "forward 5\ndown 5\nforward 8\nup 3\ndown 8\nforward 2\n",
|
||||
"expected": "900"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
66
challenges/2021/02-dive/py/__init__.py
Normal file
66
challenges/2021/02-dive/py/__init__.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
from typing import List
|
||||
from aocpy import BaseChallenge
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
FORWARD = "forward"
|
||||
UP = "up"
|
||||
DOWN = "down"
|
||||
|
||||
|
||||
@dataclass
|
||||
class Instruction:
|
||||
direction: str
|
||||
magnitude: int
|
||||
|
||||
|
||||
def parse(instr: str) -> List[Instruction]:
|
||||
o = []
|
||||
for line in instr.strip().splitlines():
|
||||
direction, magnitude = line.split(" ")
|
||||
o.append(
|
||||
Instruction(direction, int(magnitude)),
|
||||
)
|
||||
return o
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
depth = 0
|
||||
horizontal = 0
|
||||
|
||||
instructions = parse(instr)
|
||||
for instruction in instructions:
|
||||
|
||||
if instruction.direction == FORWARD:
|
||||
horizontal += instruction.magnitude
|
||||
elif instruction.direction == UP:
|
||||
depth -= instruction.magnitude
|
||||
elif instruction.direction == DOWN:
|
||||
depth += instruction.magnitude
|
||||
else:
|
||||
raise ValueError(f"unknown direction {instruction.direction}")
|
||||
|
||||
return depth * horizontal
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> int:
|
||||
depth = 0
|
||||
horizontal = 0
|
||||
aim = 0
|
||||
|
||||
instructions = parse(instr)
|
||||
for instruction in instructions:
|
||||
|
||||
if instruction.direction == FORWARD:
|
||||
horizontal += instruction.magnitude
|
||||
depth += instruction.magnitude * aim
|
||||
elif instruction.direction == UP:
|
||||
aim -= instruction.magnitude
|
||||
elif instruction.direction == DOWN:
|
||||
aim += instruction.magnitude
|
||||
else:
|
||||
raise ValueError(f"unknown direction {instruction.direction}")
|
||||
|
||||
return depth * horizontal
|
|
@ -11,5 +11,6 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
|
|||
| Day | Status | Solutions | Notes |
|
||||
| ----------------------------------- | ------------------ | ---------------------------------------------------------------------------- | -------------------- |
|
||||
| 01 - Sonar Sweep | Complete | [Python](01-sonarSweep/py), [Go](01-sonarSweep/go), [Nim](01-sonarSweep/nim) | |
|
||||
| 02 - Dive! | Complete | [Python](02-dive/py), [Go](02-dive/go) | |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue