Add 2020 day one
This commit is contained in:
parent
ac2f6599a5
commit
34028fa131
4 changed files with 106 additions and 0 deletions
2
challenges/2020/01-reportRepair/README.md
Normal file
2
challenges/2020/01-reportRepair/README.md
Normal file
|
@ -0,0 +1,2 @@
|
|||
# [Day 1: Report Repair](https://adventofcode.com/2020/day/1)
|
||||
|
55
challenges/2020/01-reportRepair/go/challenge.go
Normal file
55
challenges/2020/01-reportRepair/go/challenge.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
package challenge
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/codemicro/adventOfCode/lib/aocgo"
|
||||
)
|
||||
|
||||
type Challenge struct {
|
||||
aocgo.BaseChallenge
|
||||
}
|
||||
|
||||
func (c Challenge) One(instr string) (interface{}, error) {
|
||||
values := parse(instr)
|
||||
|
||||
for _, i := range values {
|
||||
for _, v := range values {
|
||||
if v+i == 2020 {
|
||||
return v * i, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("no combinations found")
|
||||
}
|
||||
|
||||
func (c Challenge) Two(instr string) (interface{}, error) {
|
||||
values := parse(instr)
|
||||
|
||||
for _, i := range values {
|
||||
for _, v := range values {
|
||||
for _, x := range values {
|
||||
if v+i+x == 2020 {
|
||||
return v * i * x, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, errors.New("no combinations found")
|
||||
}
|
||||
|
||||
func parse(instr string) []int {
|
||||
inputSlice := strings.Split(strings.TrimSpace(instr), "\n")
|
||||
|
||||
var values []int
|
||||
for _, v := range inputSlice {
|
||||
str, _ := strconv.Atoi(v)
|
||||
values = append(values, str)
|
||||
}
|
||||
|
||||
return values
|
||||
}
|
17
challenges/2020/01-reportRepair/info.json
Normal file
17
challenges/2020/01-reportRepair/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "1721\n979\n366\n299\n675\n1456",
|
||||
"expected": "514579"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "1721\n979\n366\n299\n675\n1456",
|
||||
"expected": "241861950"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
32
challenges/2020/01-reportRepair/py/__init__.py
Normal file
32
challenges/2020/01-reportRepair/py/__init__.py
Normal file
|
@ -0,0 +1,32 @@
|
|||
from typing import Any, List
|
||||
from aocpy import BaseChallenge
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
values = parse(instr)
|
||||
|
||||
for i in values:
|
||||
for v in values:
|
||||
if v + i == 2020:
|
||||
return v * i
|
||||
|
||||
raise ValueError("no macthing combinations")
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> Any:
|
||||
values = parse(instr)
|
||||
|
||||
for i in values:
|
||||
for v in values:
|
||||
for x in values:
|
||||
if v + i + x == 2020:
|
||||
return v * i * x
|
||||
|
||||
raise ValueError("no matching combinations")
|
||||
|
||||
|
||||
def parse(instr: str) -> List:
|
||||
return [int(x) for x in instr.strip().split("\n")]
|
Loading…
Add table
Add a link
Reference in a new issue