Skip to content

Commit fb2f90e

Browse files
committed
completed day04 (ugly!)
1 parent feeac38 commit fb2f90e

File tree

12 files changed

+1527
-3
lines changed

12 files changed

+1527
-3
lines changed

2020/day01.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en-us">
33
<head>
4-
<title>C:\Users\ppeterlongo\repos\adventofnim\2020\day01.html</title>
4+
<title>c:\Users\ppeterlongo\repos\adventofnim\2020\day01.html</title>
55
<!-- https://css-tricks.com/emojis-as-favicons/ changed font-size to 80 to fit whale-->
66
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>🐳</text></svg>">
77
<meta content="text/html; charset=utf-8" http-equiv="content-type">

2020/day02.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en-us">
33
<head>
4-
<title>C:\Users\ppeterlongo\repos\adventofnim\2020\day02.html</title>
4+
<title>c:\Users\ppeterlongo\repos\adventofnim\2020\day02.html</title>
55
<!-- https://css-tricks.com/emojis-as-favicons/ changed font-size to 80 to fit whale-->
66
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>🐳</text></svg>">
77
<meta content="text/html; charset=utf-8" http-equiv="content-type">

2020/day03.html

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!DOCTYPE html>
22
<html lang="en-us">
33
<head>
4-
<title>c:\Users\ppeterlongo\repos\adventofnim\2020\day03.html</title>
4+
<title>C:\Users\ppeterlongo\repos\adventofnim\2020\day03.html</title>
55
<!-- https://css-tricks.com/emojis-as-favicons/ changed font-size to 80 to fit whale-->
66
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>🐳</text></svg>">
77
<meta content="text/html; charset=utf-8" http-equiv="content-type">
@@ -198,6 +198,46 @@ <h2>--- Part 2 ---</h2>
198198
echo "part2: ", part2</code></pre>
199199
<pre><samp>part2: 3952146825</samp></pre>
200200
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
201+
<h1>Visualizations</h1>
202+
<p>I want to try to take advantage of the fact that I output html.</p>
203+
<p>I will reproduce the explantory design showing the ride through the example.</p>
204+
<pre><code class="nim">proc show(m: StrideMap; r: Ride; repeat = 6) =
205+
var line = ""
206+
var
207+
i = 0
208+
j = 0
209+
p: Point = r.path[j]
210+
c: char
211+
for y in 0 ..< m.height:
212+
for x in 0 ..< m.stride:
213+
line.add m.data[i]
214+
inc i
215+
line = line.repeat(repeat)
216+
if y == p.y:
217+
c = if m[p] == '.':
218+
'O' else:
219+
'X'
220+
line = line[0 ..< p.x] & "<em>" & $c & "</em>" & line[p.x + 1 .. ^1]
221+
inc j
222+
if j < r.path.len:
223+
p = r.path[j]
224+
echo line
225+
line = ""
226+
227+
show(map0, ride0)
228+
echo map1.shape</code></pre>
229+
<pre><samp>..##.........##.........##.........##.........##.........##.......
230+
#..<em>O</em>#...#..#...#...#..#...#...#..#...#...#..#...#...#..#...#...#..
231+
.#....<em>X</em>..#..#....#..#..#....#..#..#....#..#..#....#..#..#....#..#.
232+
..#.#...#<em>O</em>#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#..#.#...#.#
233+
.#...##..#..<em>X</em>...##..#..#...##..#..#...##..#..#...##..#..#...##..#.
234+
..#.##.......#.<em>X</em>#.......#.##.......#.##.......#.##.......#.##.....
235+
.#.#.#....#.#.#.#.<em>O</em>..#.#.#.#....#.#.#.#....#.#.#.#....#.#.#.#....#
236+
.#........#.#........<em>X</em>.#........#.#........#.#........#.#........#
237+
#.##...#...#.##...#...#.<em>X</em>#...#...#.##...#...#.##...#...#.##...#...
238+
#...##....##...##....##...#<em>X</em>....##...##....##...##....##...##....#
239+
.#..#...#.#.#..#...#.#.#..#...<em>X</em>.#.#..#...#.#.#..#...#.#.#..#...#.#
240+
(stride: 31, height: 323)</samp></pre>
201241

202242
</main>
203243
</body>

2020/day03.nim

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,37 @@ nbCode:
121121

122122
gotTheStar
123123

124+
nbText: "# Visualizations"
125+
nbText: """I want to try to take advantage of the fact that I output html.
126+
127+
I will reproduce the explantory design showing the ride through the example.
128+
"""
129+
130+
nbCode:
131+
proc show(m: StrideMap, r: Ride, repeat=6) =
132+
var line = ""
133+
var
134+
i = 0
135+
j = 0
136+
p: Point = r.path[j]
137+
c: char
138+
for y in 0 ..< m.height:
139+
for x in 0 ..< m.stride:
140+
line.add m.data[i]
141+
inc i
142+
143+
line = line.repeat(repeat)
144+
if y == p.y:
145+
c = if m[p] == '.': 'O' else: 'X'
146+
line = line[0 ..< p.x] & "<em>" & $c & "</em>" & line[p.x + 1 .. ^1]
147+
inc j
148+
if j < r.path.len:
149+
p = r.path[j]
150+
echo line
151+
line = ""
152+
show(map0, ride0)
153+
echo map1.shape
154+
# hey: nbBlock is not available! I need to inject it!!
155+
# nbBlock.output = "<span style=\"\">" & nbBlock.output & "</span>"
156+
124157
nbSave

2020/day04.html

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
<!DOCTYPE html>
2+
<html lang="en-us">
3+
<head>
4+
<title>C:\Users\ppeterlongo\repos\adventofnim\2020\day04.html</title>
5+
<!-- https://css-tricks.com/emojis-as-favicons/ changed font-size to 80 to fit whale-->
6+
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>🐳</text></svg>">
7+
<meta content="text/html; charset=utf-8" http-equiv="content-type">
8+
<meta content="width=device-width, initial-scale=1" name="viewport">
9+
<link rel='stylesheet' href='https://unpkg.com/normalize.css/'>
10+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/light.min.css">
11+
<link href="https://fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext" rel="stylesheet" type="text/css">
12+
<style>
13+
body, code, samp {
14+
font-family: "Source Code Pro", monospace;
15+
}
16+
17+
body {
18+
background: #0f0f23;
19+
color: #cccccc;
20+
}
21+
22+
h1, h2, h3, h4, h5, h6, strong {
23+
color: #ffffff;
24+
font-size: 1em;
25+
font-weight: normal;
26+
27+
}
28+
29+
pre code {
30+
background: #10101a;
31+
color: #cccccc;
32+
border-radius: 0px;
33+
border-left: 1px solid #ffc200;
34+
padding-top: 0px;
35+
padding-bottom: 0px;
36+
position: relative;
37+
left: -2px;
38+
}
39+
40+
samp {
41+
position: relative;
42+
display: inline-block;
43+
margin: 0;
44+
padding: 0;
45+
}
46+
47+
samp:before {
48+
z-index: -1;
49+
content: "";
50+
position: absolute;
51+
display: block;
52+
left: -2px;
53+
right: -2px;
54+
top: 3px;
55+
bottom: 0px;
56+
border: 1px solid #333340;
57+
background: #10101a;
58+
}
59+
60+
a {
61+
text-decoration: none;
62+
color: #009900;
63+
}
64+
65+
a:hover {
66+
color: #99ff99;
67+
}
68+
69+
em {
70+
color: #ffffff;
71+
font-style: normal;
72+
text-shadow: 0 0 5px #ffffff;
73+
}
74+
75+
em.star {
76+
color: #ffff66;
77+
text-shadow: 0 0 5px #ffff66;
78+
}
79+
80+
code {
81+
border-radius: 0px;
82+
border-left: 1px solid #ffc200;
83+
}
84+
85+
pre code {
86+
padding-top: 0px;
87+
padding-bottom: 0px;
88+
position: relative;
89+
left: -2px;
90+
}
91+
</style>
92+
</head>
93+
<body>
94+
<header><nav><em class="star"><a href="https://pietroppeter.github.io/adventofnim/index.html">🎄👑 adventofnim</a></em></header><main>
95+
<h1>--- Day 4: Passport Processing ---</h1>
96+
<p>I came with such an ugly solution that now I really <em>have to learn regexs</em> for good</p>
97+
<pre><code class="nim">type
98+
PassField = enum
99+
byr, iyr, eyr, hgt, hcl, ecl, pid, cid
100+
PassPort = Table[PassField, string]
101+
Input = distinct string
102+
let
103+
input = "2020/input04.txt".readFile.Input
104+
example = """ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
105+
byr:1937 iyr:2017 cid:147 hgt:183cm
106+
107+
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
108+
hcl:#cfa07d byr:1929
109+
110+
hcl:#ae17e1 iyr:2013
111+
eyr:2024
112+
ecl:brn pid:760753108 byr:1931
113+
hgt:179cm
114+
115+
hcl:#cfa07d eyr:2025 pid:166559648
116+
iyr:2011 ecl:brn hgt:59in""".Input
117+
proc parse(line: string): seq[tuple[f: PassField, s: string]] =
118+
for s in line.split:
119+
if s == "":
120+
continue
121+
let t = s.split(':')
122+
result.add (parseEnum[PassField](t[0]), t[1])
123+
124+
proc parse(batch: Input): seq[PassPort] =
125+
var p: PassPort
126+
p = initTable[PassField, string]()
127+
for line in batch.string.splitLines:
128+
if line.len == 0:
129+
result.add p
130+
p = initTable[PassField, string]()
131+
for v in line.strip.parse:
132+
p[v.f] = v.s
133+
result.add p
134+
135+
proc isValid(p: PassPort): bool =
136+
p.len == 8 or (p.len == 7 and cid notIn p)
137+
138+
proc countValid(s: seq[PassPort]): int =
139+
for p in s:
140+
if p.isValid:
141+
inc result
142+
143+
let
144+
pass0 = example.parse
145+
pass1 = input.parse
146+
echo pass0.countValid
147+
echo pass1.countValid</code></pre>
148+
<pre><samp>2
149+
216</samp></pre>
150+
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
151+
<pre><code class="nim">const
152+
colors = "amb blu brn gry grn hzl oth".split()
153+
proc validHgt(s: string): bool =
154+
var num: BiggestInt
155+
if s.endsWith("cm"):
156+
parseBiggestInt(s[0 .. ^3], num) != 0 and num >= 150 and num <= 193
157+
elif s.endsWith("in"):
158+
parseBiggestInt(s[0 .. ^3], num) != 0 and num >= 59 and num <= 76
159+
else:
160+
false
161+
162+
template myEcho(s: string) =
163+
discard
164+
165+
proc isGood(p: PassPort): bool =
166+
if not p.isValid:
167+
return false
168+
var num: BiggestInt
169+
myEcho $p
170+
if parseBiggestInt(p[byr], num) != 4 or num < 1920 or num > 2002:
171+
myEcho "invalid p[byr] " & p[byr]
172+
return false
173+
myEcho "valid p[byr] " & p[byr]
174+
if parseBiggestInt(p[iyr], num) != 4 or num < 2010 or num > 2020:
175+
myEcho "invalid p[iyr] " & p[iyr]
176+
return false
177+
myEcho "valid p[iyr] " & p[iyr]
178+
if parseBiggestInt(p[eyr], num) != 4 or num < 2020 or num > 2030:
179+
myEcho "invalid p[eyr] " & p[eyr]
180+
return false
181+
myEcho "valid p[eyr] " & p[eyr]
182+
if p[ecl] notIn colors:
183+
myEcho "invalid p[ecl] " & p[ecl]
184+
return false
185+
myEcho "valid p[ecl] " & p[ecl]
186+
var color: string
187+
if not p[hcl].startsWith("#") or p[hcl].len != 7 or
188+
p[hcl].parseWhile(color, {'0' .. '9', 'a' .. 'f'}, start = 1) != 6:
189+
myEcho "invalid p[hcl] " & p[hcl]
190+
return false
191+
myEcho "valid p[hcl] " & p[hcl]
192+
if not (p[hgt].validHgt):
193+
myEcho "invalid p[hgt] " & p[hgt]
194+
return false
195+
myEcho "valid p[hgt] " & p[hgt]
196+
var digits: string
197+
if p[pid].parseWhile(digits, {'0' .. '9'}) != 9:
198+
myEcho "invalid p[pid] " & p[pid]
199+
return false
200+
myEcho "valid p[pid] " & p[pid]
201+
myEcho "ALLGOOD"
202+
return true
203+
204+
proc countGood(s: seq[PassPort]): int =
205+
for p in s:
206+
if p.isGood:
207+
inc result
208+
209+
echo pass0.countGood
210+
echo pass1.countGood</code></pre>
211+
<pre><samp>2
212+
150</samp></pre>
213+
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
214+
215+
</main>
216+
</body>
217+
</html>

0 commit comments

Comments
 (0)