Add 2021-20 in Nim
Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
parent
16914cc28d
commit
30a4fd6fff
4 changed files with 116 additions and 7 deletions
|
@ -2,13 +2,21 @@
|
|||
"day": 20,
|
||||
"dir": "challenges/2021/20-trenchMap",
|
||||
"implementations": {
|
||||
"Nim": {
|
||||
"part.1.avg": 0.019850478899999996,
|
||||
"part.1.max": 0.048351829,
|
||||
"part.1.min": 0.008844087,
|
||||
"part.2.avg": 1.18194288576,
|
||||
"part.2.max": 2.369594795,
|
||||
"part.2.min": 0.635431931
|
||||
},
|
||||
"Python": {
|
||||
"part.1.avg": 0.0832568073272705,
|
||||
"part.1.max": 0.11255383491516113,
|
||||
"part.1.min": 0.07309865951538086,
|
||||
"part.2.avg": 4.895450177192688,
|
||||
"part.2.max": 7.253676652908325,
|
||||
"part.2.min": 4.276515483856201
|
||||
"part.1.avg": 0.18487148761749267,
|
||||
"part.1.max": 0.3699030876159668,
|
||||
"part.1.min": 0.09880661964416504,
|
||||
"part.2.avg": 10.631744318008423,
|
||||
"part.2.max": 18.34923243522644,
|
||||
"part.2.min": 5.976168155670166
|
||||
}
|
||||
},
|
||||
"numRuns": 50
|
||||
|
|
101
challenges/2021/20-trenchMap/nim/challenge.nim
Normal file
101
challenges/2021/20-trenchMap/nim/challenge.nim
Normal file
|
@ -0,0 +1,101 @@
|
|||
import std/tables
|
||||
import std/strutils
|
||||
import std/sequtils
|
||||
import std/enumerate
|
||||
|
||||
const
|
||||
LIT = '#'
|
||||
UNLIT = '.'
|
||||
|
||||
type
|
||||
Point = (int, int)
|
||||
Image = Table[Point, char]
|
||||
|
||||
proc parse(instr: string): (string, Image) =
|
||||
let
|
||||
sp = instr.strip.split("\n\n")
|
||||
algo = sp[0]
|
||||
image = sp[1]
|
||||
|
||||
var imageTable = Image()
|
||||
|
||||
for y, line in enumerate(image.splitLines):
|
||||
for x, pixel in line[0..^1]:
|
||||
if pixel == LIT:
|
||||
imageTable[(x, y)] = pixel
|
||||
|
||||
return (algo, imageTable)
|
||||
|
||||
|
||||
proc getAdjacentPoints(centerPoint: Point): array[0..8, Point] =
|
||||
let (x, y) = centerPoint
|
||||
return [
|
||||
(x - 1, y - 1),
|
||||
(x, y - 1),
|
||||
(x + 1, y - 1),
|
||||
(x - 1, y),
|
||||
(x, y),
|
||||
(x + 1, y),
|
||||
(x - 1, y + 1),
|
||||
(x, y + 1),
|
||||
(x + 1, y + 1),
|
||||
]
|
||||
|
||||
|
||||
proc enhanceN(image: Image, algorithm: string, n: int): Image =
|
||||
result = image
|
||||
for i in 0..<n:
|
||||
let
|
||||
keysSeq = toSeq(result.keys)
|
||||
xVals = map(keysSeq, (proc(p: Point): int = p[0]))
|
||||
yVals = map(keysSeq, (proc(p: Point): int = p[1]))
|
||||
|
||||
minX = min(xVals)
|
||||
maxX = max(xVals)
|
||||
minY = min(yVals)
|
||||
maxY = max(yVals)
|
||||
|
||||
var changes = Image()
|
||||
|
||||
for y in minY - 2 ..< maxY + 2:
|
||||
for x in minX - 2 ..< maxX + 2:
|
||||
let p = (x, y)
|
||||
|
||||
var n: int
|
||||
|
||||
for point in getAdjacentPoints(p):
|
||||
let (px, py) = point
|
||||
var isLit: bool
|
||||
|
||||
if algorithm[0] == LIT and not (minX <= px and px <= maxX and minY <= py and py <= maxY):
|
||||
isLit = i mod 2 != 0
|
||||
else:
|
||||
isLit = result.getOrDefault(point, UNLIT) == LIT
|
||||
|
||||
n = n shl 1
|
||||
|
||||
if isLit:
|
||||
n = n or 0b1
|
||||
|
||||
changes[p] = algorithm[n]
|
||||
|
||||
for point, change in changes.pairs:
|
||||
if change == UNLIT and point in result:
|
||||
result.del(point)
|
||||
elif change == LIT:
|
||||
result[point] = LIT
|
||||
|
||||
proc core(instr: string, n: int): int =
|
||||
let
|
||||
parsed = parse(instr)
|
||||
algorithm = parsed[0]
|
||||
var image = parsed[1]
|
||||
|
||||
image = enhanceN(image, algorithm, n)
|
||||
return len(image)
|
||||
|
||||
proc partOne*(instr: string): int =
|
||||
return core(instr, 2)
|
||||
|
||||
proc partTwo*(instr: string): int =
|
||||
return core(instr, 50)
|
|
@ -31,7 +31,7 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
|
|||
| 17 - Trick Shot | Unattempted | | |
|
||||
| 18 - Snailfish | Could not solve | | |
|
||||
| 19 - Beacon Scanner | Unattempted | | |
|
||||
| 20 - Trench Map | ★ ★ | [Python](20-trenchMap/py) | Took a moment to realise that the infinite grid alternates between lit and unlit, and even then I had to look at someone else's solution to realise it. |
|
||||
| 20 - Trench Map | ★ ★ | [Python](20-trenchMap/py), [Nim](20-trenchMap/nim) | Took a moment to realise that the infinite grid alternates between lit and unlit, and even then I had to look at someone else's solution to realise it. |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 38 KiB After Width: | Height: | Size: 35 KiB |
Loading…
Add table
Add a link
Reference in a new issue