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