From 34028fa131caafbea667326e1c492fb7e25710b2 Mon Sep 17 00:00:00 2001 From: AKP Date: Sat, 27 Nov 2021 14:40:09 +0000 Subject: [PATCH] Add 2020 day one --- challenges/2020/01-reportRepair/README.md | 2 + .../2020/01-reportRepair/go/challenge.go | 55 +++++++++++++++++++ challenges/2020/01-reportRepair/info.json | 17 ++++++ .../2020/01-reportRepair/py/__init__.py | 32 +++++++++++ 4 files changed, 106 insertions(+) create mode 100644 challenges/2020/01-reportRepair/README.md create mode 100644 challenges/2020/01-reportRepair/go/challenge.go create mode 100644 challenges/2020/01-reportRepair/info.json create mode 100644 challenges/2020/01-reportRepair/py/__init__.py diff --git a/challenges/2020/01-reportRepair/README.md b/challenges/2020/01-reportRepair/README.md new file mode 100644 index 0000000..51a41d8 --- /dev/null +++ b/challenges/2020/01-reportRepair/README.md @@ -0,0 +1,2 @@ +# [Day 1: Report Repair](https://adventofcode.com/2020/day/1) + diff --git a/challenges/2020/01-reportRepair/go/challenge.go b/challenges/2020/01-reportRepair/go/challenge.go new file mode 100644 index 0000000..bd0bb94 --- /dev/null +++ b/challenges/2020/01-reportRepair/go/challenge.go @@ -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 +} diff --git a/challenges/2020/01-reportRepair/info.json b/challenges/2020/01-reportRepair/info.json new file mode 100644 index 0000000..0728889 --- /dev/null +++ b/challenges/2020/01-reportRepair/info.json @@ -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" + } + ] + } +} \ No newline at end of file diff --git a/challenges/2020/01-reportRepair/py/__init__.py b/challenges/2020/01-reportRepair/py/__init__.py new file mode 100644 index 0000000..32f398d --- /dev/null +++ b/challenges/2020/01-reportRepair/py/__init__.py @@ -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")]