Add 2020 day one

This commit is contained in:
akp 2021-11-27 14:40:09 +00:00
parent ac2f6599a5
commit 34028fa131
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
4 changed files with 106 additions and 0 deletions

View file

@ -0,0 +1,2 @@
# [Day 1: Report Repair](https://adventofcode.com/2020/day/1)

View 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
}

View 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"
}
]
}
}

View 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")]