Add 2021-11 in Nim
Signed-off-by: AKU <tom@tdpain.net>
This commit is contained in:
parent
12fa83b625
commit
9ce1e05bfa
4 changed files with 94 additions and 7 deletions
|
@ -2,13 +2,21 @@
|
|||
"day": 11,
|
||||
"dir": "challenges/2021/11-dumboOctopus",
|
||||
"implementations": {
|
||||
"Nim": {
|
||||
"part.1.avg": 0.002156735096000002,
|
||||
"part.1.max": 0.006734169,
|
||||
"part.1.min": 0.001474201,
|
||||
"part.2.avg": 0.008079303889999999,
|
||||
"part.2.max": 0.038707332,
|
||||
"part.2.min": 0.005537803
|
||||
},
|
||||
"Python": {
|
||||
"part.1.avg": 0.007964067935943604,
|
||||
"part.1.max": 0.022200822830200195,
|
||||
"part.1.min": 0.0058345794677734375,
|
||||
"part.2.avg": 0.03082272505760193,
|
||||
"part.2.max": 0.07726001739501953,
|
||||
"part.2.min": 0.023880720138549805
|
||||
"part.1.avg": 0.008022420167922973,
|
||||
"part.1.max": 0.022809743881225586,
|
||||
"part.1.min": 0.005808591842651367,
|
||||
"part.2.avg": 0.031094204664230348,
|
||||
"part.2.max": 0.08413910865783691,
|
||||
"part.2.min": 0.023587465286254883
|
||||
}
|
||||
},
|
||||
"numRuns": 1000
|
||||
|
|
79
challenges/2021/11-dumboOctopus/nim/challenge.nim
Normal file
79
challenges/2021/11-dumboOctopus/nim/challenge.nim
Normal file
|
@ -0,0 +1,79 @@
|
|||
import std/tables
|
||||
import std/strutils
|
||||
|
||||
type
|
||||
Point = (int, int)
|
||||
Cave = Table[Point, int]
|
||||
|
||||
proc parse(instr: string): Cave =
|
||||
for y, line in instr.strip.splitLines[0 .. ^1]:
|
||||
for x, digitChar in line.pairs:
|
||||
result[(x, y)] = parseInt($digitChar)
|
||||
|
||||
proc getAdjacentPoints(point: Point): seq[Point] =
|
||||
let (x, y) = point
|
||||
return @[
|
||||
(x, y - 1),
|
||||
(x - 1, y),
|
||||
(x + 1, y),
|
||||
(x, y + 1),
|
||||
(x - 1, y - 1),
|
||||
(x + 1, y + 1),
|
||||
(x - 1, y + 1),
|
||||
(x + 1, y - 1),
|
||||
]
|
||||
|
||||
proc iterate(cave: Cave): (Cave, int, bool) =
|
||||
var cave = cave
|
||||
|
||||
for point in cave.keys:
|
||||
cave[point] = cave[point] + 1
|
||||
|
||||
var updates = Cave()
|
||||
var hasFlashed: seq[Point]
|
||||
|
||||
proc handleNine(point: Point) =
|
||||
updates[point] = 0
|
||||
hasFlashed.add(point)
|
||||
for adjacent in getAdjacentPoints(point):
|
||||
if not (adjacent in cave):
|
||||
continue
|
||||
|
||||
var previousValue = cave[adjacent]
|
||||
if adjacent in updates:
|
||||
previousValue = updates[adjacent]
|
||||
|
||||
updates[adjacent] = previousValue + 1
|
||||
|
||||
if previousValue + 1 > 9 and not (adjacent in hasFlashed):
|
||||
handleNine(adjacent)
|
||||
|
||||
for point in cave.keys:
|
||||
if cave[point] > 9 and not (point in hasFlashed):
|
||||
handleNine(point)
|
||||
|
||||
for point in updates.keys:
|
||||
cave[point] = updates[point]
|
||||
|
||||
for point in hasFlashed:
|
||||
cave[point] = 0
|
||||
|
||||
return (cave, hasFlashed.len, hasFlashed.len == cave.len)
|
||||
|
||||
|
||||
proc partOne*(instr: string): int =
|
||||
var cave = parse(instr)
|
||||
|
||||
for _ in countup(1, 100):
|
||||
let (c, n, _) = iterate(cave)
|
||||
cave = c
|
||||
result += n
|
||||
|
||||
proc partTwo*(instr: string): int =
|
||||
var cave = parse(instR)
|
||||
while true:
|
||||
result += 1
|
||||
let (c, _, allFlashed) = iterate(cave)
|
||||
if allFlashed:
|
||||
return
|
||||
cave = c
|
|
@ -20,7 +20,7 @@ Solutions to the [2021 Advent of Code](https://adventofcode.com/2021).
|
|||
| 08 - Seven Segment Search | Complete | [Python](08-sevenSegmentSearch/py), [Go](08-sevenSegmentSearch) | I may have taken the easy way out for part two, but it does work! No-one ever said the smart solution is the best solution, anyway. |
|
||||
| 09 - Smoke Basin \* | Complete | [Python](09-smokeBasin/py) | Schmokey! Also, as it turns out, I struggle to implement basic logic. Fun. |
|
||||
| 10 - Syntax Scoring | Complete | [Python](10-syntaxScoring/py) | I can't say I've ever broken something so thoroughly that it has a syntax error on *every* line... |
|
||||
| 11 - Dumbo Octopus | Complete | [Python](11-dumboOctopus/py) | Cellular automata my beloved <3 |
|
||||
| 11 - Dumbo Octopus | Complete | [Python](11-dumboOctopus/py), [Nim](11-dumboOctopus/nim) | Cellular automata my beloved <3 |
|
||||
|
||||
<!-- PARSE END -->
|
||||
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 48 KiB After Width: | Height: | Size: 48 KiB |
Loading…
Add table
Add a link
Reference in a new issue