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
Run
> 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."
Run
> input.txt
\.....................\.......................|..../......-.........../.|.....\.............|..........|..\...
.../...................\..................../...|.......|........||...................../...........\.........
././....|......./................\............|..\...........-....../............../|........-................
....\..\...........-/..|......|......\............|............../.................-........./...-...|.......\
..................................|.\.......|...../.........................-...../......\..|...............\.
.....\.............................................|../.\............../--.......................\............
|.......-.|........\.................\..............|........./....\.........|...................\-.-...|.....
....\........./....-.........................--.....\..../......................|.\......................\...-
\..-..../.....|..........\......................-|......|...................../...../........./...........-...
........................\.\|......../........../......................\.............|./../....................
.....\...................|.............................|......-........|.........-./.../.|....................
..-................./.........................................\..\........-..-........-.......\../............
-................-............................\................./..--................-............../.|.......
..-.....|.............|................./.......-.-........................-............-...........\.........
..........|..........................-...|..\...............................................\-....\...........
/................../..........|....\..-......................../....\...\..........-....../.........../.....\.
................\....-................-|.\...-...........-.....-........\..........................\..........
...................................\../..|.../../...........................\.........../.\-......|.........-.
/....|....|..........................\............./.....|/................./....-......................-.....
....-.............../.....|...........|../......\../.............../........................|............./...
......./..............|./../.................-.\.........../.....................|.........../........\.......
...................-...............\/.......|........|....................../.......................-/......\.
................\...../....-/......-.........-.....\/.\...............\....../...-....|......|.........\......
....-.........-.........................-......................../.......|...............................\....
......../..................|...../......\..\..-|-..../..............|......../............|.-.......-.........
......./............-..........-............................../.........................|\..-..-\.\......-..\.
.\-.....|......................-..\................/.-................|.\..............\..................|...
-..........................-............../.|.-......\......-..-...../......./................................
....|.................................................\....|..........\...................-......|........./..
................../...........-..............\......................................|.-........|..............
...-............|..............-...........-.\.......................|..|..../......................../.|....-
.......-..-......./...../.........-.......\.....-.../............-./......../.../............-.....|...|......
...............|.........../..........|/..........|..........-.-...../...../..|..........\.....-./.|./....|...
.........|......................\.|.....|......................-.-........|.............................-...|.
.-...|..\...-...............\..............\......./........-............/.-..\........../...../....-....\....
...........\.-.......|.......|....-.............|-.|........../..../...\....\............|.|..|\..............
.....................|.\....|......|../...\......|........................\......|.......-./..../.............
......................\--................................\.................|................-.................
-...............\...-.....................................-.\...../....\...........................|..........
.....|.......\.......................-/...\..-.........-..../\|..................\........-...................
..|........./....|..-./..\..\................-|..........\../......\...............\............\............|
......-................../.............|.............\................................-..\...../........-.....
....................../......../..\.-.........................|....\..|...\...../../.....|............../.....
...\................................/..............................\........|.........\.-............\........
/.................../................-...........-.|.............\.................................|.....-....
.|...-..|/./../.........-./.............................................../..........-.........-.|./..........
..../..................|......................|../..........\........-......../.........\...|.................
........./..................|..-..........|\............\......-..-..\...|....|.....................|....-....
.....|/....|........\.........-................../..........|............|...........-.....|......./......//..
..........\.|../...|........................\.........\.|......||....-...............\\.....|.|..............|
............-................|.....-....||......../.../.......................|..\...........\................
...............-..../......-..../.......|-.....-.....\..................................../..|................
/......-...............\/...././..................\..................\..........-......................|......
...................\.....\.................\...|.................-............/........-..............\./.....
.-.\....\........\........-../.-............../...../..-..|...................../.-.....\.....-........../..\.
|...|...............................|............../.............../....................|...../...............
....../....../...|..............-.-........-..|.../....................\|.....\......................|....../.
...\\.......|...|-............|..........-...........................................\/...../..-.-............
.........|.............|..|......\............\...............|......\......\.............\...\-.......\.....\
...........\..-.|.......|\..\..................-........\/...............................-....................
...\.................\..............................................\...............\.......-.../.............
..\.............\....---....................//.......................................|./.............../\.....
.\...........|..................................\........-......|.........\.................-........-........
..............-...............-.../....-........|.....-../......................./..../........../../--..\....
.........-......\......-.........../...\./.|.................\...................--..-........|..\......\....|
....../.........................................|...../........../..............\\...-/.......-......\..\.....
.........../...\.|..................../.\.................|......\.\..|.........../...../.....\.\...........|.
..../\..-...../.............................../..../............................-...-......\...........\......
..-..\.............................\...........|..........|.........|..........................-......./......
-..|.................-\.....\...............\/.....|...........................././...\......\.......\...-....
......\..............|..|..............-............../....................-....\\........\....|..............
|.............\....\./.....-|-....\.....\..............\/........-.|..|...........-..\......-..../.......|....
............................-.......................|........................../................|.............
/................|../............\/.....\/....-.....\......\...../|..\..........|............../..............
.|....../...../....................../..|.....................-................./...../-.........-............
......................\|........../......./..\........\.........-......../.........../.|/.......\.../.........
...............\......-|............\..\....../.........\.....|...|\............/............\.\..|.......\...
......|...........\....../....\./......./-....\../.......\...-...............\./...\.....-.......-..../.......
...|..|.............................\.....-.|........./......../......................-......./............\..
.....|........\.........|...-.-.........\..../....../..-.......-...............\..../.....-..../..............
........./.\/........................-..........\.....................|.......-..............\./.\............
..../................/................................|.....||..............\........-...............|........
....\./.........../\-.....-...\............//.\..............\...../................|.....\..-................
..../|........./............|.....................|......................-..|...............................\.
..................\............................\.......\.........-.............|...../.-.............-......|.
..............|.-...........................\.\./.......\........../...\................/....-................
.......|................/-............\............/...........-..../...........\......../...\.......\........
..............|..../......................-...........-..\.................................................-..
..............................-..-................-.......-.................-..........\.../...............|..
......-.........-........................./.....................\....|........................................
.....\........./.....-.................../................|...................|........................|......
........../.../........................./.|................-...........\...........-....|.....................
.....-.......\././/.......|..............|......../..\...........\.............................-......|../-.-.
........|-....-....................\.....|\...\/.....\........\......./..........|.\-..|......................
../...-...-...|...../............\.......................-..........\...\.....-...\...............|....../...|
............|..\....|..................\/...................../../....|...................|.............../...
.............-................./............|.......|............\....................................\.-.....
-...../|..............-......\..../..................|..\....-............................................\...
........|.........-........\................/....\..................../.|...................................|.
.....|................./.........-|\...-...................|....-................-......../...............|..-
.|.........|...........|...-.............................\............................|................\......
....|.....-......|......./.-.|...........\..-........|.........................-....-..-.....|................
........................................\.....................-.../........................../../..........|..
-................/....\.......//....-........................../.\.........-......./..|\..........\...........
-............................./......................................../-...........................\|........
........................................../-.....//......\......\...........\........|./....../.......--......
../..............-....../......../................../......./....-...........|../.......\.\.......-......./..\
..............-............../......|.........-...............|................................-..........\...
.....................\..........|..\......|...............................................-...................
...../..|.../.........|.../.......-...................................\.....................................|.
Run
> 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.