Add Nim solutions for day 2 and 3
Signed-off-by: AKP <tom@tdpain.net>
This commit is contained in:
parent
be0ebf4c28
commit
0f6d020d3c
6 changed files with 150 additions and 14 deletions
|
@ -2,13 +2,21 @@
|
|||
"day": 2,
|
||||
"dir": "challenges/2022/02-rockPaperScissors",
|
||||
"implementations": {
|
||||
"Nim": {
|
||||
"part.1.avg": 0.0006056322499999992,
|
||||
"part.1.max": 0.002023021,
|
||||
"part.1.min": 0.000160608,
|
||||
"part.2.avg": 0.000544890418,
|
||||
"part.2.max": 0.002167248,
|
||||
"part.2.min": 0.000143214
|
||||
},
|
||||
"Python": {
|
||||
"part.1.avg": 0.004535415172576904,
|
||||
"part.1.max": 0.013599634170532227,
|
||||
"part.1.min": 0.0014605522155761719,
|
||||
"part.2.avg": 0.002712693929672241,
|
||||
"part.2.max": 0.009427785873413086,
|
||||
"part.2.min": 0.0008611679077148438
|
||||
"part.1.avg": 0.005041266918182373,
|
||||
"part.1.max": 0.049498796463012695,
|
||||
"part.1.min": 0.001485586166381836,
|
||||
"part.2.avg": 0.002977017641067505,
|
||||
"part.2.max": 0.0347900390625,
|
||||
"part.2.min": 0.0008869171142578125
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
|
|
81
challenges/2022/02-rockPaperScissors/nim/challenge.nim
Normal file
81
challenges/2022/02-rockPaperScissors/nim/challenge.nim
Normal file
|
@ -0,0 +1,81 @@
|
|||
import std/strutils
|
||||
import std/tables
|
||||
import std/sequtils
|
||||
|
||||
type Match = (char, char)
|
||||
|
||||
const
|
||||
rock = 'A'
|
||||
paper = 'B'
|
||||
scissors = 'C'
|
||||
|
||||
lose = 'X'
|
||||
draw = 'Y'
|
||||
win = 'Z'
|
||||
|
||||
scores = {
|
||||
rock: 1,
|
||||
paper: 2,
|
||||
scissors: 3,
|
||||
|
||||
win: 6,
|
||||
lose: 0,
|
||||
draw: 3,
|
||||
}.toTable
|
||||
|
||||
magicBeans = [scissors, rock, paper, scissors, rock].toSeq
|
||||
|
||||
outcomeOffsets = {
|
||||
win: 1,
|
||||
lose: -1,
|
||||
draw: 0,
|
||||
}.toTable
|
||||
|
||||
proc indexOf[T](x: seq[T], v: T): int = indexOf(x, v, 0, x.len)
|
||||
|
||||
proc indexOf[T](x: seq[T], v: T, start: int): int = indexOf(x, v, start, x.len)
|
||||
|
||||
proc indexOf[T](x: seq[T], v: T, start: int, stop: int): int =
|
||||
for i in start..<stop:
|
||||
if x[i] == v:
|
||||
return i
|
||||
raise newException(ValueError, "item not found in supplied sequence")
|
||||
|
||||
proc parse(instr: string): seq[Match] =
|
||||
for x in instr.strip.splitlines():
|
||||
result.add((x[0], x[2]))
|
||||
|
||||
proc partOne*(instr: string): int =
|
||||
var inp = parse(instr)
|
||||
|
||||
for i in 0..<inp.len:
|
||||
inp[i][1] = case inp[i][1]:
|
||||
of 'X':
|
||||
rock
|
||||
of 'Y':
|
||||
paper
|
||||
of 'Z':
|
||||
scissors
|
||||
else:
|
||||
raise newException(ValueError, "unknown value " & $inp[i][1])
|
||||
|
||||
for m in inp:
|
||||
result += scores[m[1]]
|
||||
|
||||
if m[0] == m[1]:
|
||||
result += scores[draw]
|
||||
else:
|
||||
let opponent = magicBeans.indexOf(m[1], 1)
|
||||
let ours = magicBeans.indexOf(m[0], opponent - 1, opponent + 2)
|
||||
|
||||
if opponent - ours == outcomeOffsets[win]:
|
||||
result += scores[win]
|
||||
|
||||
proc partTwo*(instr: string): int =
|
||||
let inp = parse(instr)
|
||||
|
||||
for m in inp:
|
||||
result += scores[m[1]]
|
||||
let our_move = magicBeans[magicBeans.indexOf(m[0], 1) + outcomeOffsets[
|
||||
m[1]]]
|
||||
result += scores[our_move]
|
|
@ -2,13 +2,21 @@
|
|||
"day": 3,
|
||||
"dir": "challenges/2022/03-rucksackReorganization",
|
||||
"implementations": {
|
||||
"Nim": {
|
||||
"part.1.avg": 0.001724590531000001,
|
||||
"part.1.max": 0.004034478,
|
||||
"part.1.min": 0.000354275,
|
||||
"part.2.avg": 0.001641240340999998,
|
||||
"part.2.max": 0.003942411,
|
||||
"part.2.min": 0.000368459
|
||||
},
|
||||
"Python": {
|
||||
"part.1.avg": 0.0015598664283752442,
|
||||
"part.1.max": 0.0057833194732666016,
|
||||
"part.1.min": 0.00038242340087890625,
|
||||
"part.2.avg": 0.0011923382282257081,
|
||||
"part.2.max": 0.003597736358642578,
|
||||
"part.2.min": 0.00029277801513671875
|
||||
"part.1.avg": 0.0012067508697509765,
|
||||
"part.1.max": 0.0058939456939697266,
|
||||
"part.1.min": 0.0003886222839355469,
|
||||
"part.2.avg": 0.000901190996170044,
|
||||
"part.2.max": 0.0058858394622802734,
|
||||
"part.2.min": 0.0002906322479248047
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
|
|
39
challenges/2022/03-rucksackReorganization/nim/challenge.nim
Normal file
39
challenges/2022/03-rucksackReorganization/nim/challenge.nim
Normal file
|
@ -0,0 +1,39 @@
|
|||
import std/sets
|
||||
import std/strutils
|
||||
|
||||
proc parse(instr: string): seq[string] = instr.strip.splitlines
|
||||
|
||||
proc getPriority(ch: char): int =
|
||||
let co = ch.ord
|
||||
if ord('a') <= co and co <= ord('z'):
|
||||
return (co - ord('a')) + 1
|
||||
return (co - ord('A')) + 27
|
||||
|
||||
proc partOne*(instr: string): int =
|
||||
let inp = parse(instr)
|
||||
|
||||
var sigma = 0
|
||||
for x in inp:
|
||||
assert x.len mod 2 == 0
|
||||
let l = x.len div 2
|
||||
var y = intersection(
|
||||
toHashSet(x[0..<l]),
|
||||
toHashSet(x[l..<x.len]),
|
||||
)
|
||||
sigma += getPriority(y.pop())
|
||||
|
||||
return sigma
|
||||
|
||||
proc partTwo*(instr: string): int =
|
||||
let inp = parse(instr)
|
||||
assert inp.len mod 3 == 0
|
||||
|
||||
var sigma = 0
|
||||
for i in countup(0, inp.len - 1, 3):
|
||||
var x = toHashSet(inp[i])
|
||||
x = intersection(x, toHashSet(inp[i + 1]))
|
||||
x = intersection(x, toHashSet(inp[i + 2]))
|
||||
|
||||
sigma += getPriority(x.pop())
|
||||
|
||||
return sigma
|
|
@ -11,8 +11,8 @@ 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), [Java](01-calorieCounting/java/src) | Summing numbers |
|
||||
| 02 - Rock Paper Scissors | ★ ★ | [Python](02-rockPaperScissors/py) | Programmatically playing Rock Paper Scissors |
|
||||
| 03 - Rucksack Reorganization | ★ ★ | [Python](03-rucksackReorganization/py) | Sets and intersections |
|
||||
| 02 - Rock Paper Scissors | ★ ★ | [Python](02-rockPaperScissors/py), [Nim](03-rockPaperScissors/nim) | Programmatically playing Rock Paper Scissors |
|
||||
| 03 - Rucksack Reorganization | ★ ★ | [Python](03-rucksackReorganization/py), [Nim](03-rucksackReorganization/nim) | Sets and intersections |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 53 KiB After Width: | Height: | Size: 57 KiB |
Loading…
Add table
Add a link
Reference in a new issue