-
Notifications
You must be signed in to change notification settings - Fork 1
/
day04.html
167 lines (152 loc) Β· 4.84 KB
/
day04.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2020\day04.nim</title>
<!-- https://css-tricks.com/emojis-as-favicons/ changed font-size to 80 to fit whale-->
<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>">
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel='stylesheet' href='https://unpkg.com/normalize.css/'>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
<link rel='stylesheet' href='../static/androidstudio.css'>
<script src="../static/highlight.nim.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<style>
a {
text-decoration: none;
color: #009900;
}
a:hover {
color: #99ff99;
}
em {
color: #ffffff;
font-style: normal;
text-shadow: 0 0 5px #ffffff;
}
em.star {
font-style: normal;
color: #ffff66;
text-shadow: 0 0 5px #ffff66;
}
</style>
</head>
<body>
<header><nav><em class="star"><a href="https://pietroppeter.github.io/adventofnim/index.html">ππ adventofnim</a></em></header>
<hr><main>
<h1>--- Day 4: Passport Processing ---</h1>
<p>I came with such an ugly solution that now I really <em>have to learn regexs</em> for good</p>
<pre><code class="nim">type
PassField = enum
byr, iyr, eyr, hgt, hcl, ecl, pid, cid
PassPort = Table[PassField, string]
Input = distinct string
let
input = "2020/input04.txt".readFile.Input
example = """ecl:gry pid:860033327 eyr:2020 hcl:#fffffd
byr:1937 iyr:2017 cid:147 hgt:183cm
iyr:2013 ecl:amb cid:350 eyr:2023 pid:028048884
hcl:#cfa07d byr:1929
hcl:#ae17e1 iyr:2013
eyr:2024
ecl:brn pid:760753108 byr:1931
hgt:179cm
hcl:#cfa07d eyr:2025 pid:166559648
iyr:2011 ecl:brn hgt:59in""".Input
proc parse(line: string): seq[tuple[f: PassField, s: string]] =
for s in line.split:
if s == "":
continue
let t = s.split(':')
result.add (parseEnum[PassField](t[0]), t[1])
proc parse(batch: Input): seq[PassPort] =
var p: PassPort
p = initTable[PassField, string]()
for line in batch.string.splitLines:
if line.len == 0:
result.add p
p = initTable[PassField, string]()
for v in line.strip.parse:
p[v.f] = v.s
result.add p
proc isValid(p: PassPort): bool =
p.len == 8 or (p.len == 7 and cid notIn p)
proc countValid(s: seq[PassPort]): int =
for p in s:
if p.isValid:
inc result
let
pass0 = example.parse
pass1 = input.parse
echo pass0.countValid
echo pass1.countValid</code></pre>
<pre><samp>2
216</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
<pre><code class="nim">const
colors = "amb blu brn gry grn hzl oth".split()
proc validHgt(s: string): bool =
var num: BiggestInt
if s.endsWith("cm"):
parseBiggestInt(s[0 .. ^3], num) != 0 and num >= 150 and num <= 193
elif s.endsWith("in"):
parseBiggestInt(s[0 .. ^3], num) != 0 and num >= 59 and num <= 76
else:
false
template myEcho(s: string) =
discard
proc isGood(p: PassPort): bool =
if not p.isValid:
return false
var num: BiggestInt
myEcho $p
if parseBiggestInt(p[byr], num) != 4 or num < 1920 or num > 2002:
myEcho "invalid p[byr] " & p[byr]
return false
myEcho "valid p[byr] " & p[byr]
if parseBiggestInt(p[iyr], num) != 4 or num < 2010 or num > 2020:
myEcho "invalid p[iyr] " & p[iyr]
return false
myEcho "valid p[iyr] " & p[iyr]
if parseBiggestInt(p[eyr], num) != 4 or num < 2020 or num > 2030:
myEcho "invalid p[eyr] " & p[eyr]
return false
myEcho "valid p[eyr] " & p[eyr]
if p[ecl] notIn colors:
myEcho "invalid p[ecl] " & p[ecl]
return false
myEcho "valid p[ecl] " & p[ecl]
var color: string
if not p[hcl].startsWith("#") or p[hcl].len != 7 or
p[hcl].parseWhile(color, {'0' .. '9', 'a' .. 'f'}, start = 1) != 6:
myEcho "invalid p[hcl] " & p[hcl]
return false
myEcho "valid p[hcl] " & p[hcl]
if not (p[hgt].validHgt):
myEcho "invalid p[hgt] " & p[hgt]
return false
myEcho "valid p[hgt] " & p[hgt]
var digits: string
if p[pid].parseWhile(digits, {'0' .. '9'}) != 9:
myEcho "invalid p[pid] " & p[pid]
return false
myEcho "valid p[pid] " & p[pid]
myEcho "ALLGOOD"
return true
proc countGood(s: seq[PassPort]): int =
for p in s:
if p.isGood:
inc result
echo pass0.countGood
echo pass1.countGood</code></pre>
<pre><samp>2
150</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
</main>
<hr>
<small>
<p>github repo for all my solutions: ππ<a href="https://github.com/pietroppeter/adventofnim">adventofnim</a>;</p>
<p>html output powered by π³<a href="https://github.com/pietroppeter/nimib">nimib</a>.</p>
</small>
</body>
</html>