Today, Friday December 1st, at 5 a.m. UTC started the ninth incarnation of Advent of Code, popular programming contest started back in 2015. The author describes Advent of Code (AoC) as "a series of small programming puzzles for a variety of skill sets and skill levels in any programming language you like".
The rules of AoC are quite simple. Starting from December 1st until Christmas, every day at 5 a.m. UTC a new task is released. The tasks consist of two parts, where second part is revealed after you solve the first part, and it is a continuation and/or variation of the first part. You don't submit your code, just the result of your calculation.
The participation in AoC is free (although, if you like it, consider donating), all you need to do is log in with your Github, Google, Twitter, or Reddit account.
If you have never participated in AoC before or you want to prepare yourself for the start of the competition by solving some tasks, take a look at the previous events. To bring you up to speed, we recommend solving any of first 10 tasks of any year, as those are usually easier and can be solved relatively fast.
Advent of Code offers private leadearboards, and Nim has not only one but two of them.
The original Nim private leaderboard has previously filled up to the maximum of 200 users. We have opened a new private leaderboard, which you can join by using 681448-60235f8f code on this link.
If you have joined one of these leaderboards in previous years, there's no need to do it again — you're already in.
In this thread you can post links to your AoC repositories, share your solutions, ask for help, discuss the tasks, etc.
People usually share their solutions on r/adventofcode subreddit and we encourage you to share your Nim solutions there too and showcase the beauty of Nim.
If you're sharing your solutions via Twitter or other platforms, use #AdventOfNim hashtag and/or mention @nim_lang.
You can also use Nim IRC/Gitter/Discord channel (https://discord.gg/7RsJ5uEN, #aoc channel) if you have some Nim-related problem, but have in mind that your snippets might contain spoilers for other who haven't solved the task yet — not everybody will be able to solve the tasks at 5 a.m. UTC. Consider waiting at least couple of hours before asking for help (in that time, try it some more to see if you can solve it by yourself :)).
Have fun!
That's my first live Advent of Code. Let's see how long I will last.
I'll be posting my solutions here: codeberg.org
my attempts here. I'm not a professional programmer and think in VBA. https://github.com/FullValueRider/AoC2023
Thus far, as a totally unfair comparison (because I have no idea about setting things up to be comparable) Day 01 nim 38 ms, twinBasic 19.5 ms Day 02 nim 47 ms, twinBasic 7.8 MS
My solutions, all written in Nim, can be found here:
https://github.com/lscrd/AdventOfCode2023
Solutions for years 2016 to 2022 can also be found on https://github.com/lscrd.
Wow. Thanks for taking the time to look at my code. The video was very interesting and provided lots of points for me to follow up. I must admit to having a red face (embarrassment) in response to some of your comments, but that's the danger of asking for feedback.
Thanks again.
Hello everyone:
I have a problem and I think there is some bug in the compiler or can you help me debug my code, because I am not able to understand why ONLY those entries do not work, the rest without problems but thoses 4 entries goes into a loop, doesn't even show the first entry on the map.
some help, thank you
p2.nim
import std/strutils
import std/sequtils
# import std/setutils
# import std/algorithm
# import std/sets
import std/tables
# import std/deques
# import std/strscans
# import std/os
import ../../denise
let input = readFile("input.txt")
let test1 = """.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|...."""
type
Beam = tuple
pos: Vec2[int]
dir: char
stop: bool
const
dirs = {'^': (0, -1), '>': (1, 0), 'v': (0, 1), '<': (-1, 0)}.toTable
var
beams: seq[Beam]
map: seq[seq[char]]
visited: Table[Vec2[int], string]
proc isInside(v: Vec2[int]): bool =
if v.x < 0 or v.y < 0 or v.x > map[0].high or v.y > map.high:
return false
true
proc update(b: var Beam, u: var seq[Beam]) =
if b.stop: return
if not isInside(b.pos):
b.stop = true
return
if not visited.hasKey(b.pos):
visited[b.pos] = ""
visited[b.pos].add b.dir
case map[b.pos.y][b.pos.x]:
of '.':
map[b.pos.y][b.pos.x] = b.dir
of '\\':
case b.dir:
of '>': b.dir = 'v'
of '<': b.dir = '^'
of '^': b.dir = '<'
of 'v': b.dir = '>'
else: discard
of '/':
case b.dir:
of '>': b.dir = '^'
of '<': b.dir = 'v'
of '^': b.dir = '>'
of 'v': b.dir = '<'
else: discard
of '|':
case b.dir:
of '<', '>':
u.add (b.pos, '^', false)
u.add (b.pos, 'v', false)
b.stop = true
return
else: discard
of '-':
case b.dir:
of '^', 'v':
u.add (b.pos, '<', false)
u.add (b.pos, '>', false)
b.stop = true
return
else: discard
of '^':
if b.dir == '^': ## loop
b.stop = true
return
map[b.pos.y][b.pos.x] = chr(visited[b.pos].len + 48)
of '>':
if b.dir == '>': ## loop
b.stop = true
return
map[b.pos.y][b.pos.x] = chr(visited[b.pos].len + 48)
of 'v':
if b.dir == 'v': ## loop
b.stop = true
return
map[b.pos.y][b.pos.x] = chr(visited[b.pos].len + 48)
of '<':
if b.dir == '<': ## loop
b.stop = true
return
map[b.pos.y][b.pos.x] = chr(visited[b.pos].len + 48)
of '2' .. '9': discard
else:
echo "Error: ", b
assert false
b.pos += dirs[b.dir]
proc printMap() =
echo ""
for l in map:
echo l.join
proc process(bs: var seq[Beam]): bool =
var
a: seq[Beam]
for b in bs.mitems:
b.update(a)
if a.len > 0:
beams.add a
bs.anyIt(it.stop == false)
proc shootBeam(b: Beam, m: seq[seq[char]]): int =
var
isRunning = true
frame = 0
visited.clear
beams.setLen(0)
beams.add b
map = m
while isRunning:
isRunning = process(beams)
frame.inc
# printMap()
# echo frame
visited.len
proc p2(input: string): int =
var
shoots: seq[int]
let
mapcp = input.strip.split('\n').mapIt(it.toSeq)
for x in 0 .. mapcp[0].high:
if x == 5 or x == 54 or x == 58 or x == 70: continue ## (5,0) (54,0) (58,0) (70,0)
shoots.add ((x, 0), 'v', false).shootBeam(mapcp)
for x in 0 .. mapcp[0].high:
shoots.add ((x, mapcp.high), '^', false).shootBeam(mapcp)
for y in 0 .. mapcp.high:
shoots.add ((0, y), '>', false).shootBeam(mapcp)
shoots.add ((mapcp[0].high, y), '<', false).shootBeam(mapcp)
shoots.max
bench:
p2(input)
block:
assert p2(test1) == 51
discard
denise.nim
import std/times
type
Vec2*[T] = tuple
x: T
y: T
proc `+`*[T](a, b: Vec2[T]): Vec2[T] =
result.x = a.x + b.x
result.y = a.y + b.y
proc `-`*[T](a, b: Vec2[T]): Vec2[T] =
result.x = a.x - b.x
result.y = a.y - b.y
proc `*`*[T](a: Vec2[T], n: int): Vec2[T] =
result.x = a.x * n
result.y = a.y * n
proc `\`*[T](a: Vec2[T], n: int): Vec2[T] =
result.x = a.x div n
result.y = a.y div n
proc `+=`*[T](a: var Vec2[T], b: Vec2[T]) =
a.x += b.x
a.y += b.y
proc `-=`*[T](a: var Vec2[T], b: Vec2[T]) =
a.x -= b.x
a.y -= b.y
proc `*=`*[T](a: var Vec2[T], n: int) =
a.x *= n
a.y *= n
proc `\=`*[T](a: var Vec2[T], n: int) =
a.x = a.x div n
a.y = a.y div n
type
Vec3*[T] = tuple
x: T
y: T
z: T
proc `+`*[T](a, b: Vec3[T]): Vec3[T] =
result.x = a.x + b.x
result.y = a.y + b.y
result.z = a.z + b.z
proc `-`*[T](a, b: Vec3[T]): Vec3[T] =
result.x = a.x - b.x
result.y = a.y - b.y
result.z = a.z - b.z
proc `*`*[T](a: Vec3[T], n: int): Vec3[T] =
result.x = a.x * n
result.y = a.y * n
result.z = a.z * n
proc `\`*[T](a: Vec3[T], n: int): Vec3[T] =
result.x = a.x div n
result.y = a.y div n
result.z = a.z div n
proc `+=`*[T](a: var Vec3[T], b: Vec3[T]) =
a.x += b.x
a.y += b.y
a.z += b.z
proc `-=`*[T](a: var Vec3[T], b: Vec3[T]) =
a.x -= b.x
a.y -= b.y
a.z -= b.z
proc `*=`*[T](a: var Vec3[T], n: int) =
a.x *= n
a.y *= n
a.z *= n
proc `\=`*[T](a: var Vec3[T], n: int) =
a.x = a.x div n
a.y = a.y div n
a.z = a.z div n
template bench*(body: untyped) =
block:
let t1 = cpuTime()
let z = body
echo z, "\nTime: ", (cpuTime() - t1) * 1000, " ms."
input.txt
\.....................\.......................|..../......-.........../.|.....\.............|..........|..\...
.../...................\..................../...|.......|........||...................../...........\.........
././....|......./................\............|..\...........-....../............../|........-................
....\..\...........-/..|......|......\............|............../.................-........./...-...|.......\
..................................|.\.......|...../.........................-...../......\..|...............\.
.....\.............................................|../.\............../--.......................\............
|.......-.|........\.................\..............|........./....\.........|...................\-.-...|.....
....\........./....-.........................--.....\..../......................|.\......................\...-
\..-..../.....|..........\......................-|......|...................../...../........./...........-...
........................\.\|......../........../......................\.............|./../....................
.....\...................|.............................|......-........|.........-./.../.|....................
..-................./.........................................\..\........-..-........-.......\../............
-................-............................\................./..--................-............../.|.......
..-.....|.............|................./.......-.-........................-............-...........\.........
..........|..........................-...|..\...............................................\-....\...........
/................../..........|....\..-......................../....\...\..........-....../.........../.....\.
................\....-................-|.\...-...........-.....-........\..........................\..........
...................................\../..|.../../...........................\.........../.\-......|.........-.
/....|....|..........................\............./.....|/................./....-......................-.....
....-.............../.....|...........|../......\../.............../........................|............./...
......./..............|./../.................-.\.........../.....................|.........../........\.......
...................-...............\/.......|........|....................../.......................-/......\.
................\...../....-/......-.........-.....\/.\...............\....../...-....|......|.........\......
....-.........-.........................-......................../.......|...............................\....
......../..................|...../......\..\..-|-..../..............|......../............|.-.......-.........
......./............-..........-............................../.........................|\..-..-\.\......-..\.
.\-.....|......................-..\................/.-................|.\..............\..................|...
-..........................-............../.|.-......\......-..-...../......./................................
....|.................................................\....|..........\...................-......|........./..
................../...........-..............\......................................|.-........|..............
...-............|..............-...........-.\.......................|..|..../......................../.|....-
.......-..-......./...../.........-.......\.....-.../............-./......../.../............-.....|...|......
...............|.........../..........|/..........|..........-.-...../...../..|..........\.....-./.|./....|...
.........|......................\.|.....|......................-.-........|.............................-...|.
.-...|..\...-...............\..............\......./........-............/.-..\........../...../....-....\....
...........\.-.......|.......|....-.............|-.|........../..../...\....\............|.|..|\..............
.....................|.\....|......|../...\......|........................\......|.......-./..../.............
......................\--................................\.................|................-.................
-...............\...-.....................................-.\...../....\...........................|..........
.....|.......\.......................-/...\..-.........-..../\|..................\........-...................
..|........./....|..-./..\..\................-|..........\../......\...............\............\............|
......-................../.............|.............\................................-..\...../........-.....
....................../......../..\.-.........................|....\..|...\...../../.....|............../.....
...\................................/..............................\........|.........\.-............\........
/.................../................-...........-.|.............\.................................|.....-....
.|...-..|/./../.........-./.............................................../..........-.........-.|./..........
..../..................|......................|../..........\........-......../.........\...|.................
........./..................|..-..........|\............\......-..-..\...|....|.....................|....-....
.....|/....|........\.........-................../..........|............|...........-.....|......./......//..
..........\.|../...|........................\.........\.|......||....-...............\\.....|.|..............|
............-................|.....-....||......../.../.......................|..\...........\................
...............-..../......-..../.......|-.....-.....\..................................../..|................
/......-...............\/...././..................\..................\..........-......................|......
...................\.....\.................\...|.................-............/........-..............\./.....
.-.\....\........\........-../.-............../...../..-..|...................../.-.....\.....-........../..\.
|...|...............................|............../.............../....................|...../...............
....../....../...|..............-.-........-..|.../....................\|.....\......................|....../.
...\\.......|...|-............|..........-...........................................\/...../..-.-............
.........|.............|..|......\............\...............|......\......\.............\...\-.......\.....\
...........\..-.|.......|\..\..................-........\/...............................-....................
...\.................\..............................................\...............\.......-.../.............
..\.............\....---....................//.......................................|./.............../\.....
.\...........|..................................\........-......|.........\.................-........-........
..............-...............-.../....-........|.....-../......................./..../........../../--..\....
.........-......\......-.........../...\./.|.................\...................--..-........|..\......\....|
....../.........................................|...../........../..............\\...-/.......-......\..\.....
.........../...\.|..................../.\.................|......\.\..|.........../...../.....\.\...........|.
..../\..-...../.............................../..../............................-...-......\...........\......
..-..\.............................\...........|..........|.........|..........................-......./......
-..|.................-\.....\...............\/.....|...........................././...\......\.......\...-....
......\..............|..|..............-............../....................-....\\........\....|..............
|.............\....\./.....-|-....\.....\..............\/........-.|..|...........-..\......-..../.......|....
............................-.......................|........................../................|.............
/................|../............\/.....\/....-.....\......\...../|..\..........|............../..............
.|....../...../....................../..|.....................-................./...../-.........-............
......................\|........../......./..\........\.........-......../.........../.|/.......\.../.........
...............\......-|............\..\....../.........\.....|...|\............/............\.\..|.......\...
......|...........\....../....\./......./-....\../.......\...-...............\./...\.....-.......-..../.......
...|..|.............................\.....-.|........./......../......................-......./............\..
.....|........\.........|...-.-.........\..../....../..-.......-...............\..../.....-..../..............
........./.\/........................-..........\.....................|.......-..............\./.\............
..../................/................................|.....||..............\........-...............|........
....\./.........../\-.....-...\............//.\..............\...../................|.....\..-................
..../|........./............|.....................|......................-..|...............................\.
..................\............................\.......\.........-.............|...../.-.............-......|.
..............|.-...........................\.\./.......\........../...\................/....-................
.......|................/-............\............/...........-..../...........\......../...\.......\........
..............|..../......................-...........-..\.................................................-..
..............................-..-................-.......-.................-..........\.../...............|..
......-.........-........................./.....................\....|........................................
.....\........./.....-.................../................|...................|........................|......
........../.../........................./.|................-...........\...........-....|.....................
.....-.......\././/.......|..............|......../..\...........\.............................-......|../-.-.
........|-....-....................\.....|\...\/.....\........\......./..........|.\-..|......................
../...-...-...|...../............\.......................-..........\...\.....-...\...............|....../...|
............|..\....|..................\/...................../../....|...................|.............../...
.............-................./............|.......|............\....................................\.-.....
-...../|..............-......\..../..................|..\....-............................................\...
........|.........-........\................/....\..................../.|...................................|.
.....|................./.........-|\...-...................|....-................-......../...............|..-
.|.........|...........|...-.............................\............................|................\......
....|.....-......|......./.-.|...........\..-........|.........................-....-..-.....|................
........................................\.....................-.../........................../../..........|..
-................/....\.......//....-........................../.\.........-......./..|\..........\...........
-............................./......................................../-...........................\|........
........................................../-.....//......\......\...........\........|./....../.......--......
../..............-....../......../................../......./....-...........|../.......\.\.......-......./..\
..............-............../......|.........-...............|................................-..........\...
.....................\..........|..\......|...............................................-...................
...../..|.../.........|.../.......-...................................\.....................................|.
> nim --version
Nim Compiler Version 2.0.2 [Linux: amd64] Compiled at 2023-12-15 Copyright (c) 2006-2023 by Andreas Rumpf git hash: c4c44d10df8a14204a75c34e499def200589cb7c active boot switches: -d:release > PD. 7741 es valid 7741 Time: 226.47787 ms.
fixed
the fault was in the management of the crosses, it only controlled a maximum of 9
...
of '2' .. '9': discard
...
Very late to the AoC party. My attempts if anyone is interested: https://github.com/ColF/AdventOfNim2023/tree/master
I didn't know about AoC so hopefully next year I can get into it properly.