And so it begins (day 1)

Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-01 06:56:14 +00:00
parent 06e0572819
commit 745b85aeaf
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
6 changed files with 102 additions and 0 deletions

View file

@ -0,0 +1 @@
# [Day 1: Calorie Counting](https://adventofcode.com/2022/day/1)

View file

@ -0,0 +1,23 @@
{
"day": 1,
"dir": "challenges/2022/01-calorieCounting",
"implementations": {
"Nim": {
"part.1.avg": 0.001049538246,
"part.1.max": 0.010881062,
"part.1.min": 0.000266989,
"part.2.avg": 0.0011080301140000003,
"part.2.max": 0.003602068,
"part.2.min": 0.000278025
},
"Python": {
"part.1.avg": 0.001036302089691162,
"part.1.max": 0.0028290748596191406,
"part.1.min": 0.00023937225341796875,
"part.2.avg": 0.0011057097911834717,
"part.2.max": 0.003584623336791992,
"part.2.min": 0.0002548694610595703
}
},
"numRuns": 1000
}

View file

@ -0,0 +1,17 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{
"input": "1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000\n",
"expected": "24000"
}
],
"two": [
{
"input": "1000\n2000\n3000\n\n4000\n\n5000\n6000\n\n7000\n8000\n9000\n\n10000\n",
"expected": "45000"
}
]
}
}

View file

@ -0,0 +1,39 @@
import std/strutils
import std/sequtils
import std/algorithm
proc parseInput(instr: string): seq[seq[int]] =
result = instr.
strip.
split("\n\n").
map(proc(x: string): seq[int] = x.splitLines.map(parseInt))
proc sum(x: openArray[int]): int =
for i in 0..<x.len:
result += x[i]
proc partOne*(instr: string): int =
let input = parseInput(instr)
var x: seq[int]
for i in 0..<input.len:
var sum: int
for y in 0..<input[i].len:
sum += input[i][y]
x.add(sum)
return max(x)
proc partTwo*(instr: string): int =
let input = parseInput(instr)
var x: seq[int]
for i in 0..<input.len:
var sum: int
for y in 0..<input[i].len:
sum += input[i][y]
x.add(sum)
x.sort(SortOrder.Descending)
return sum(x[0..2])

View file

@ -0,0 +1,21 @@
from typing import *
from aocpy import BaseChallenge
def parse(instr: str) -> List[List[int]]:
return [map(int, x.split("\n")) for x in instr.strip().split("\n\n")]
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> Any:
parsed = parse(instr)
x = [sum(y) for y in parsed]
return max(x)
@staticmethod
def two(instr: str) -> Any:
parsed = parse(instr)
x = [sum(y) for y in parsed]
x = list(sorted(x, reverse=True))
return sum(x[:3])

View file

@ -10,6 +10,7 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
| Day | Status | Solutions | Notes |
| ----------------------------------- | ------------------ | ---------- | ------ |
| 01 - Calorie Counting | ★ ★ | [Python](01-calorieCounting/py), [Nim](01-calorieCounting/nim) | Summing numbers |
<!-- PARSE END -->