2022-04 py,nim
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
bdab5c23c6
commit
24d4ace97e
7 changed files with 144 additions and 0 deletions
1
challenges/2022/04-campCleanup/README.md
Normal file
1
challenges/2022/04-campCleanup/README.md
Normal file
|
@ -0,0 +1 @@
|
|||
# [Day 4: Camp Cleanup](https://adventofcode.com/2022/day/4)
|
23
challenges/2022/04-campCleanup/benchmark.json
Normal file
23
challenges/2022/04-campCleanup/benchmark.json
Normal file
|
@ -0,0 +1,23 @@
|
|||
{
|
||||
"day": 4,
|
||||
"dir": "challenges/2022/04-campCleanup",
|
||||
"implementations": {
|
||||
"Nim": {
|
||||
"part.1.avg": 0.0054562872569999974,
|
||||
"part.1.max": 0.021320901,
|
||||
"part.1.min": 0.002667073,
|
||||
"part.2.avg": 0.005487259290999989,
|
||||
"part.2.max": 0.019597329,
|
||||
"part.2.min": 0.002672798
|
||||
},
|
||||
"Python": {
|
||||
"part.1.avg": 0.0058808038234710695,
|
||||
"part.1.max": 0.022945165634155273,
|
||||
"part.1.min": 0.0029861927032470703,
|
||||
"part.2.avg": 0.0059101243019104,
|
||||
"part.2.max": 0.023639202117919922,
|
||||
"part.2.min": 0.002870798110961914
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
}
|
17
challenges/2022/04-campCleanup/info.json
Normal file
17
challenges/2022/04-campCleanup/info.json
Normal file
|
@ -0,0 +1,17 @@
|
|||
{
|
||||
"inputFile": "input.txt",
|
||||
"testCases": {
|
||||
"one": [
|
||||
{
|
||||
"input": "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8\n",
|
||||
"expected": "2"
|
||||
}
|
||||
],
|
||||
"two": [
|
||||
{
|
||||
"input": "2-4,6-8\n2-3,4-5\n5-7,7-9\n2-8,3-7\n6-6,4-6\n2-6,4-8\n",
|
||||
"expected": "4"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
54
challenges/2022/04-campCleanup/nim/challenge.nim
Normal file
54
challenges/2022/04-campCleanup/nim/challenge.nim
Normal file
|
@ -0,0 +1,54 @@
|
|||
import std/strutils
|
||||
import std/sequtils
|
||||
import std/enumerate
|
||||
import std/re
|
||||
import std/sets
|
||||
|
||||
|
||||
type
|
||||
ElfTaskRange = (int, int)
|
||||
ElfPair = (ElfTaskRange, ElfTaskRange)
|
||||
|
||||
let parseRe = re(r"(\d+)-(\d+),(\d+)-(\d+)")
|
||||
|
||||
|
||||
proc parse(instr: string): seq[ElfPair] =
|
||||
for ep in instr.strip().splitlines():
|
||||
var matches: array[4, string]
|
||||
if not match(ep, parseRe, matches):
|
||||
raise newException(ValueError, "badly formed input line '" & ep & "'")
|
||||
|
||||
let intMatches = matches.map(parseInt)
|
||||
result.add((
|
||||
(intMatches[0], intMatches[1]),
|
||||
(intMatches[2], intMatches[3]),
|
||||
))
|
||||
|
||||
|
||||
proc makeSet(tr: ElfTaskRange): HashSet[int] =
|
||||
return (tr[0]..tr[1]).toSeq.toHashSet
|
||||
|
||||
|
||||
proc count(inp: seq[ElfPair], pred: proc(a, b: HashSet[int], lenIntersection: int): bool): int =
|
||||
for pair in inp:
|
||||
let
|
||||
a = makeSet(pair[0])
|
||||
b = makeSet(pair[1])
|
||||
intsn = intersection(a, b)
|
||||
|
||||
if pred(a, b, intsn.len):
|
||||
result += 1
|
||||
|
||||
|
||||
|
||||
proc partOne*(instr: string): int =
|
||||
let inp = parse(instr)
|
||||
return count(inp, proc (a, b: HashSet[int], i: int): bool =
|
||||
i == len(a) or i == len(b)
|
||||
)
|
||||
|
||||
proc partTwo*(instr: string): int =
|
||||
let inp = parse(instr)
|
||||
return count(inp, proc (a, b: HashSet[int], i: int): bool =
|
||||
return i != 0
|
||||
)
|
48
challenges/2022/04-campCleanup/py/__init__.py
Normal file
48
challenges/2022/04-campCleanup/py/__init__.py
Normal file
|
@ -0,0 +1,48 @@
|
|||
from typing import *
|
||||
from aocpy import BaseChallenge
|
||||
import re
|
||||
|
||||
|
||||
ElfTaskRange = Tuple[int, int]
|
||||
ElfPair = Tuple[ElfTaskRange, ElfTaskRange]
|
||||
|
||||
parse_re = re.compile(r"(\d+)-(\d+),(\d+)-(\d+)")
|
||||
|
||||
|
||||
def parse(instr: str) -> List[ElfPair]:
|
||||
x = instr.strip().splitlines()
|
||||
for i, ep in enumerate(x):
|
||||
a, b, c, d = map(int, parse_re.match(ep).groups())
|
||||
x[i] = ((a, b), (c, d))
|
||||
return x
|
||||
|
||||
|
||||
def make_set(tr: ElfTaskRange) -> Set[int]:
|
||||
return set(range(tr[0], tr[1] + 1))
|
||||
|
||||
|
||||
def count(inp: List[ElfPair], pred: Callable[[Set[int], Set[int], int], bool]) -> int:
|
||||
# pred(first set, second set, length of intersection)
|
||||
|
||||
n = 0
|
||||
for pair in inp:
|
||||
a = make_set(pair[0])
|
||||
b = make_set(pair[1])
|
||||
intersection = a.intersection(b)
|
||||
|
||||
if pred(a, b, len(intersection)):
|
||||
n += 1
|
||||
|
||||
return n
|
||||
|
||||
|
||||
class Challenge(BaseChallenge):
|
||||
@staticmethod
|
||||
def one(instr: str) -> int:
|
||||
inp = parse(instr)
|
||||
return count(inp, lambda a, b, i: i == len(a) or i == len(b))
|
||||
|
||||
@staticmethod
|
||||
def two(instr: str) -> int:
|
||||
inp = parse(instr)
|
||||
return count(inp, lambda _, __, i: i != 0)
|
|
@ -13,6 +13,7 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
|
|||
| 01 - Calorie Counting | ★ ★ | [Python](01-calorieCounting/py), [Nim](01-calorieCounting/nim), [Java](01-calorieCounting/java/src) | Summing numbers |
|
||||
| 02 - Rock Paper Scissors | ★ ★ | [Python](02-rockPaperScissors/py), [Nim](02-rockPaperScissors/nim) | Programmatically playing Rock Paper Scissors |
|
||||
| 03 - Rucksack Reorganization | ★ ★ | [Python](03-rucksackReorganization/py), [Nim](03-rucksackReorganization/nim) | Sets and intersections |
|
||||
| 04 - Camp Cleanup | ★ ★ | [Python](04-campCleanup/py), [Nim](04-campCleanup/nim) | More sets and more intersections! |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 57 KiB After Width: | Height: | Size: 66 KiB |
Loading…
Add table
Add a link
Reference in a new issue