2022-06 py,nim & 2022-05 nim

Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
akp 2022-12-06 09:43:49 +00:00
parent 823e6ebe40
commit 5c61b3acfb
No known key found for this signature in database
GPG key ID: AA5726202C8879B7
9 changed files with 174 additions and 7 deletions

View file

@ -2,13 +2,21 @@
"day": 5, "day": 5,
"dir": "challenges/2022/05-supplyStacks", "dir": "challenges/2022/05-supplyStacks",
"implementations": { "implementations": {
"Nim": {
"part.1.avg": 0.0011273949949999996,
"part.1.max": 0.006086964,
"part.1.min": 0.000284679,
"part.2.avg": 0.0012850724640000013,
"part.2.max": 0.006825169,
"part.2.min": 0.000331679
},
"Python": { "Python": {
"part.1.avg": 0.0073598167896270755, "part.1.avg": 0.007537457227706909,
"part.1.max": 0.03172612190246582, "part.1.max": 0.030069828033447266,
"part.1.min": 0.005206584930419922, "part.1.min": 0.005114555358886719,
"part.2.avg": 0.00753035569190979, "part.2.avg": 0.007375564098358154,
"part.2.max": 0.04000711441040039, "part.2.max": 0.0339961051940918,
"part.2.min": 0.005285978317260742 "part.2.min": 0.0051727294921875
} }
}, },
"numRuns": 1000 "numRuns": 1000

View file

@ -0,0 +1,85 @@
import std/deques
import std/strutils
import std/sequtils
import std/re
type
MoveInstruction = object
n: int
source: int
destination: int
Stack[T] = Deque[T]
let instructionRe = re(r"move (\d+) from (\d+) to (\d+)")
proc parse(instr: string): (seq[Stack[char]], seq[MoveInstruction]) =
let
x = instr.split("\n\n")
rawInitialState = x[0]
rawInstructions = x[1]
var stateLines = rawInitialState.splitlines()
stateLines = stateLines[0..<stateLines.len] # the last line only contains
# digits that we don't need to look at
var groupedStateLines: seq[seq[string]]
for line in stateLines:
var x: seq[string]
for i in countup(0, line.len, 4):
x.add(line[i..<i+3])
groupedStateLines.add(x)
var state: seq[Stack[char]]
for _ in countup(0, groupedStateLines[0].len - 1):
state.add(Stack[char]())
for lineNumber in countdown(groupedStateLines.len - 1, 0):
for i, item in groupedStateLines[lineNumber].pairs():
if item != " ":
state[i].addLast(item[1])
var instructions: seq[MoveInstruction]
for line in rawInstructions.strip().splitlines():
var matches: array[3, string]
if not match(line, instructionRe, matches):
raise newException(ValueError, "badly formed input line '" & line & "'")
let intMatches = matches.map(parseInt)
instructions.add(MoveInstruction(
n: intMatches[0],
source: intMatches[1],
destination: intMatches[2]
))
return (state, instructions)
proc getTopChars(state: seq[Stack[char]]): string =
for i in countup(0, state.len-1):
result = result & $state[i].peekLast()
proc partOne*(instr: string): string =
var (state, instructions) = parse(instr)
for instruction in instructions:
for _ in countup(0, instruction.n-1):
let x = state[instruction.source - 1].popLast()
state[instruction.destination - 1].addLast(x)
return getTopChars(state)
proc partTwo*(instr: string): string =
var (state, instructions) = parse(instr)
for instruction in instructions:
var items: seq[char]
for _ in countup(0, instruction.n-1):
items.add(state[instruction.source - 1].popLast())
for i in countdown(items.len - 1, 0):
state[instruction.destination - 1].addLast(items[i])
return getTopChars(state)

View file

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

View file

@ -0,0 +1,23 @@
{
"day": 6,
"dir": "challenges/2022/06-tuningTrouble",
"implementations": {
"Nim": {
"part.1.avg": 0.0007432498970000007,
"part.1.max": 0.002544188,
"part.1.min": 0.000169445,
"part.2.avg": 0.003928926078999996,
"part.2.max": 0.008833809,
"part.2.min": 0.000939222
},
"Python": {
"part.1.avg": 0.0014611027240753174,
"part.1.max": 0.0042591094970703125,
"part.1.min": 0.0003712177276611328,
"part.2.avg": 0.006137934684753418,
"part.2.max": 0.014965057373046875,
"part.2.min": 0.0015811920166015625
}
},
"numRuns": 1000
}

View file

@ -0,0 +1,19 @@
{
"inputFile": "input.txt",
"testCases": {
"one": [
{"input": "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "expected": "7"},
{"input": "bvwbjplbgvbhsrlpgdmjqwftvncz", "expected": "5"},
{"input": "nppdvjthqldpwncqszvftbrmjlhg", "expected": "6"},
{"input": "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "expected": "10"},
{"input": "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", "expected": "11"}
],
"two": [
{"input": "mjqjpqmgbljsphdztnvjfqwrcgsmlb", "expected": "19"},
{"input": "bvwbjplbgvbhsrlpgdmjqwftvncz", "expected": "23"},
{"input": "nppdvjthqldpwncqszvftbrmjlhg", "expected": "23"},
{"input": "nznrnfrfntjfmvfwmzdfjlvtqnbhcprsg", "expected": "29"},
{"input": "zcfzfwzzqfrljwzlrfnpqdbhtmscgvjw", "expected": "26"}
]
}
}

View file

@ -0,0 +1,11 @@
import std/sets
proc find_unique_sequence(instr: string, length: int): int =
for i in countup(0, instr.len - length):
if toHashSet(instr[i..<i+length]).len == length:
return i+length
raise newException(ValueError, "cannot solve")
proc partOne*(instr: string): int = find_unique_sequence(instr, 4)
proc partTwo*(instr: string): int = find_unique_sequence(instr, 14)

View file

@ -0,0 +1,19 @@
from typing import *
from aocpy import BaseChallenge
def find_unique_sequence(instr: str, length: int) -> int:
for i in range(len(instr) - length):
if len(set(instr[i : i + length])) == length:
return i + length
raise ValueError("cannot solve")
class Challenge(BaseChallenge):
@staticmethod
def one(instr: str) -> int:
return find_unique_sequence(instr, 4)
@staticmethod
def two(instr: str) -> int:
return find_unique_sequence(instr, 14)

View file

@ -14,7 +14,8 @@ Solutions to the [2022 Advent of Code](https://adventofcode.com/2022).
| 02 - Rock Paper Scissors | ★ ★ | [Python](02-rockPaperScissors/py), [Nim](02-rockPaperScissors/nim) | Programmatically playing Rock Paper Scissors | | 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 | | 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! | | 04 - Camp Cleanup | ★ ★ | [Python](04-campCleanup/py), [Nim](04-campCleanup/nim) | More sets and more intersections! |
| 05 - Supply Stacks | ★ ★ | [Python](05-supplyStacks/py) | Believe it or not, this one involved stacks. | | 05 - Supply Stacks | ★ ★ | [Python](05-supplyStacks/py), [Nim](05-supplyStacks/nim) | Believe it or not, this one involved stacks. |
| 05 - Tuning Trouble | ★ ★ | [Python](06-tuningTrouble/py), [Nim](06-tuningTrouble/nim) | This is the first year I've not repeatedly forgotten about the existence of sets, and it's coming in quite handy. |
<!-- PARSE END --> <!-- PARSE END -->

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

After

Width:  |  Height:  |  Size: 72 KiB

Before After
Before After